我正在尝试为Python程序添加一个选项,如下所示:
optionParse= argparse.ArgumentParser(description='Change Number of Locations')
optionParse.add_argument('-n')
-n
选项将允许用户指定将在程序中使用的数字。他们会这样输入:
myProgram.py -n 12
如何在程序本身中访问它们后面的选项?它只是sysarg[1]
吗?
答案 0 :(得分:0)
有很好的记录,但这是一个容易回答的问题。
optionParse.add_argument('-n', result='store', required=True, dest='num_locs', type=int, help='Number of locations')
results = optionParse.parse_args()
num_locs = results.num_locs
我仍然认为你需要在-n和数字之间留一个空格,比如-n 13或者其他什么。如果我理解你的问题,这应该可以解决问题。