Python Argparse严格变量名

时间:2015-08-28 12:11:51

标签: python argparse

我试着理解python的argparse的功能(或错误?)。

这里,我的简单代码:

import argparse

parser = argparse.ArgumentParser(usage="%(prog)s [--start]", add_help=False)
parser.add_argument("--start", help="Start prog", action="store_true")

arguments  = parser.parse_args()
start_fpc  = arguments.start

print arguments

当我执行此脚本时,接受startstar个参数:

[ rsenet  2015-08-28 12:39:50] /tmp
$ python test.py --star
Namespace(start=True)

[ rsenet  2015-08-28 13:59:16] /tmp
$ python test.py --start
Namespace(start=True)

知道为什么吗?如果是,是否可以禁用此功能?

1 个答案:

答案 0 :(得分:4)

您需要停用{vn.5 {/ 3}}中的allow_abbrev选项。

摘自was introduced

  
      
  • allow_abbrev_ - 允许缩写长选项    缩写是明确的。 (默认值:True
  •   

这应该有所帮助:

parser = argparse.ArgumentParser(usage="%(prog)s [--start]",
                                 allow_abbrev=False,
                                 add_help=False)