我正在阅读argparse模块。我被困在metavar和action的含义上
>>> parser.add_argument('integers', metavar='N', type=int, nargs='+',
... help='an integer for the accumulator')
>>> parser.add_argument('--sum', dest='accumulate', action='store_const',
... const=sum, default=max,
... help='sum the integers (default: find the max)')
我可能错过了,但从我读到的内容来看,我找不到metavar
和
action (action="store_const", etc)
。它们到底是什么意思?
你给我们展示的只是第一个例子。Python文档中的相关部分:
http://docs.python.org/dev/library/argparse.html#action
http://docs.python.org/dev/library/argparse.html#metavar
metavar用于帮助消息中预期参数的位置。请参见这里的
FOO
是默认的metavar
:action定义了如何处理命令行参数:将其存储为常量、追加到列表中、存储布尔值等。有几个内置操作可用,另外编写自定义操作也很容易。
元变量:它为帮助消息中的可选参数提供了不同的名称。为
add_argument()
中的metavar关键字参数提供值。引用:http://www.usatlas.bnl.gov/~caballer/files/argparse/add_argument.html
操作:参数可以触发不同的操作,由操作参数指定给
add_argument()
。当遇到参数时,可以触发六个内置操作:store
:在可选地将值转换为其他类型之后,保存该值。如果未显式指定,则这是默认操作。store_true
/store_false
:保存适当的布尔值。store_const
:保存定义为参数规范一部分的值,而不是来自正在分析的参数的值。这通常用于实现不是布尔型的命令行标志。append
:将值保存到列表中。如果参数重复,将保存多个值。append_const
:将参数规范中定义的值保存到列表中。version
:打印程序的版本详细信息,然后退出。引用:http://bioportal.weizmann.ac.il/course/python/PyMOTW/PyMOTW/docs/argparse/index.html
相关问题 更多 >
编程相关推荐