[英]how to use python argparse optional argument
我的python代码看起来像这样
parser.add_argument("-c","--configFile",action ='store_true',\
help='I am here one travel')
这个想法是使用-c
选项运行时,我可以选择使用特殊的配置文件。 但是,我发出我的命令python mypython.py -c myconfig
,我得到了unrecognized argument: myconfig
。 我想念什么?
“ -c”将存储True
或False
,那么为什么要传递一个值(myconfig)? 您将其称为:
python mypython.py -c
如果希望-c接受参数,则它不应具有动作store_true。
正如Adam所提到的,您将想要更改解析器的“操作”,如docs中所述 。
但这听起来好像您想指出该特殊配置是否处于活动状态。 在这种情况下,使用action='store_true'
或action='store_false'
将是正确的。 您只是不会传递myconfig参数。
parser = argparse.ArgumentParser(description='So something')
parser.add_argument(
'-c',
'--c',
dest='enable_config',
action='store_true',
required=False,
help='Enable special config settings',
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.