我的python代码看起来像这样
parser.add_argument("-c","--configFile",action ='store_true',\
help='I am here one travel')
这个想法是在使用-c
选项运行时,我可以选择使用我的
特殊配置文件。但是,我发出了我的命令python mypython.py
-c myconfig
,我得到了unrecognized argument: myconfig
。我错过了什么?
答案 0 :(得分:2)
" -c"将存储True
或False
,那么为什么要将值(myconfig)传递给它?你可以称之为:
python mypython.py -c
如果你想让-c接受一个参数,那么就不应该有行动store_true。
答案 1 :(得分:2)
正如亚当提到的那样,你想要改变你的解析器"动作",如in the 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',
)