首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在python中构造可选参数

如何在python中构造可选参数
EN

Stack Overflow用户
提问于 2018-06-21 03:30:34
回答 1查看 118关注 0票数 0

我是python的新手,我想知道在特定的数据验证案例中构造和构建代码的最好方法是什么。我正在构建cmd行脚本,它接受3个参数并将它们保存到变量中。第三个参数是可选的。我应该如何处理可选参数。当我没有指定第三个参数时,提示我"IndexError: list index out of range“。

为什么用可选参数验证用户输入,最简单、最实用的原因是什么?

user_cat = sys.argv[1]
user_id = sys.argv[2]
user_guid = sys.argv[3]

def validate(user_cat, argv_cat_list, user_guid):
    if len(user_cat) > 10 or user_cat not in argv_cat_list:
        print("Error! Please specify a valid category (AddDevice, GetAccount, c, p, GetDevices, r ,s)")
        sys.exit()
    elif len(user_id) == 36 or user_id.startswith("SAM-") and len(user_guid) == False:
        getInfo()
        sys.exit()
    elif len(user_cat) >= 10 and len(user_guid) == 36 and user_id.startswith("FRA-"):
        print ("Test-hest!")
    else:
        print("Error! Please specify a valid input")
        sys.exit()
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50955730

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档