Python 中的 argparse 中的元变量和动作是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19124304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does metavar and action mean in argparse in Python?
提问by eagertoLearn
I am reading through argparsemodule. I got stuck as what to metavar and action means
我正在阅读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)')
I might have missed but from what I read, I could not find definitions for metavar
and
action (action="store_const", etc)
. what do they actually mean?
我可能错过了,但从我阅读的内容来看,我找不到metavar
和 的
定义action (action="store_const", etc)
。它们实际上是什么意思?
采纳答案by alecxe
metavaris used in help messages in a place of an expected argument. See FOO
is a default metavar
here:
metavar用于帮助消息中预期参数的位置。SeeFOO
是metavar
这里的默认值:
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo')
>>> parser.add_argument('bar')
>>> parser.parse_args('X --foo Y'.split())
Namespace(bar='X', foo='Y')
>>> parser.print_help()
usage: [-h] [--foo FOO] bar
...
actiondefines how to handle command-line arguments: store it as a constant, append into a list, store a boolean value etc. There are several built-in actions available, plus it's easy to write a custom one.
action定义了如何处理命令行参数:将其存储为常量、附加到列表中、存储布尔值等。有几个内置操作可用,另外编写自定义操作也很容易。
回答by hpaulj
What you showed us is the just the first example. The relevant sections from the Python docs:
你向我们展示的只是第一个例子。Python 文档中的相关部分:
http://docs.python.org/dev/library/argparse.html#action
http://docs.python.org/dev/library/argparse.html#action
回答by subhadarshi samal
Metavar:It provides a different name for optional argument in help messages. Provide a value for the metavar keyword argument within add_argument()
.
Metavar:它为帮助消息中的可选参数提供不同的名称。为 中的 metavar 关键字参数提供一个值add_argument()
。
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', metavar='YYY')
>>> parser.add_argument('bar', metavar='XXX')
>>> parser.parse_args('X --foo Y'.split())
Namespace(bar='X', foo='Y')
>>> parser.print_help()
usage: [-h] [--foo YYY] XXX
positional arguments:
XXX
optional arguments:
-h, --help show this help message and exit
--foo YYY
Reference: http://www.usatlas.bnl.gov/~caballer/files/argparse/add_argument.html
参考:http: //www.usatlas.bnl.gov/~caballer/files/argparse/add_argument.html
Action:Arguments can trigger different actions, specified by the action argument to add_argument()
. There are six built-in actions that can be triggered when an argument is encountered:
动作:参数可以触发不同的动作,由动作参数指定add_argument()
。遇到参数时可以触发六个内置操作:
store
: Save the value, after optionally converting it to a different type. This is the default action taken if none is specified explicitly.store_true
/store_false
: Save the appropriate boolean value.store_const
: Save a value defined as part of the argument specification, rather than a value that comes from the arguments being parsed. This is typically used to implement command line flags that aren't booleans.append
: Save the value to a list. Multiple values are saved if the argument is repeated.append_const
: Save a value defined in the argument specification to a list.version
: Prints version details about the program and then exits.
store
: 保存值,可选择将其转换为其他类型。如果没有明确指定,这是默认的操作。store_true
/store_false
:保存适当的布尔值。store_const
:保存定义为参数规范一部分的值,而不是来自正在解析的参数的值。这通常用于实现不是布尔值的命令行标志。append
:将值保存到列表中。如果参数重复,则保存多个值。append_const
:将参数规范中定义的值保存到列表中。version
:打印有关程序的版本详细信息,然后退出。
Reference: http://bioportal.weizmann.ac.il/course/python/PyMOTW/PyMOTW/docs/argparse/index.html
参考:http: //bioportal.weizmann.ac.il/course/python/PyMOTW/PyMOTW/docs/argparse/index.html