我的代码是:
parser.add_argument("-c","config", action='store_true',help='help text here')
如果我发出命令:python myprog.py -c myconfig.txt
,则该值将存储在myconfig.txt
中。如何将此值存储到变量中?
答案 0 :(得分:0)
如果您想参加辩论,请不要使用action='store_true'
。这用于设置布尔标志,例如--verbose
。相反,您需要指定要采用的参数的类型。这里我们使用type=str
(这是默认值):
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-c","--config", type=str, help='help text here')
args = parser.parse_args()
if args.config:
print "the value of config is {}".format(args.config)