我有如下所示的代码:我怎样才能让Python Argparse只列出一次选择?
list_of_choices = ["foo", "bar", "baz"]
parser = argparse.ArgumentParser(description='some description')
parser.add_argument("-n","--name","-o","--othername",dest=name,
choices=list_of_choices
和我所得到的输出如下:
-n {foo,bar,baz}, --name {foo,bar,baz}, -o {foo,bar,baz},
--othername {foo,bar,baz}
我想的是:
-n, --name, -o, --othername {foo,bar,baz}
对于背景下,有历史原因,为什么我们需要两个名称来选择相同的选项,而实际的选择列表是22个元素,所以它看起来比上面差得多。
此问题与Python argparse: Lots of choices results in ugly help output略有不同,因为我没有使用两个单独的选项,并且可以将它全部放在上面。
我不认为这很有效,因为如果我通过-f foo的,后来我得到一个错误说的名字是无。 – coyot
你是想抓住所有参数还是只接受你定义的参数?在这种情况下,你必须为-f做另一个add_argument()。另外我认为dest应该是一个字符串dest ='name'。 http://docs.python.org/dev/library/argparse.html#dest –