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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:55:45  来源:igfitidea点击:

What does metavar and action mean in argparse in Python?

pythonactionargparse

提问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 metavarand 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 FOOis a default metavarhere:

metavar用于帮助消息中预期参数的位置。SeeFOOmetavar这里的默认值:

>>> 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

http://docs.python.org/dev/library/argparse.html#metavar

http://docs.python.org/dev/library/argparse.html#metavar

回答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()。遇到参数时可以触发六个内置操作:

  1. store: Save the value, after optionally converting it to a different type. This is the default action taken if none is specified explicitly.

  2. store_true/store_false: Save the appropriate boolean value.

  3. 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.

  4. append: Save the value to a list. Multiple values are saved if the argument is repeated.

  5. append_const: Save a value defined in the argument specification to a list.

  6. version: Prints version details about the program and then exits.

  1. store: 保存值,可选择将其转换为其他类型。如果没有明确指定,这是默认的操作。

  2. store_true/ store_false:保存适当的布尔值。

  3. store_const:保存定义为参数规范一部分的值,而不是来自正在解析的参数的值。这通常用于实现不是布尔值的命令行标志。

  4. append:将值保存到列表中。如果参数重复,则保存多个值。

  5. append_const:将参数规范中定义的值保存到列表中。

  6. 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