113

I am reading through argparse module. I got stuck as what to metavar and action means

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

nbro
  • 15,395
  • 32
  • 113
  • 196
eagertoLearn
  • 9,772
  • 23
  • 80
  • 122

2 Answers2

77

Metavar: It provides a different name for optional argument in help messages. Provide a value for the metavar keyword argument within 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

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:

  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.

Reference: http://bioportal.weizmann.ac.il/course/python/PyMOTW/PyMOTW/docs/argparse/index.html

Michael
  • 8,362
  • 6
  • 61
  • 88
  • 8
    I would add this: `store_true` means if true was specified then set param value but otherwise leave it to None. If `default` was also specified then param is set to that value instead of leaving it to None. – Shital Shah May 08 '18 at 05:37
  • 7. `help`: Print the help and then exits. – Blundell Dec 15 '21 at 16:18
56

metavar is used in help messages in a place of an expected argument. See FOO is a default metavar here:

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

action defines 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.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • 1
    btw, what is the difference between these two? `parser.add_argument('cd_data', metavar='.dat', type = str, nargs='+')` and `parser.add_argument('-cd_data', metavar='.dat', type = str, nargs='+')`. There is only one change which `cd_data` to `-cd_data` – eagertoLearn Oct 01 '13 at 20:52
  • 2
    The one with `-` is an optional argument, see [docs](http://docs.python.org/dev/library/argparse.html#name-or-flags). – alecxe Oct 01 '13 at 20:56
  • would you be able to look at this post and give your suggestion? `http://stackoverflow.com/questions/19126399/why-args-add-argument-works-when-given-in-two-separate-statements-but-not-one-in` – eagertoLearn Oct 01 '13 at 21:38