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?
metavar is used in help messages in a place of an expected argument. See
FOO
is a defaultmetavar
here: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.
Metavar: It provides a different name for optional argument in help messages. Provide a value for the metavar keyword argument within
add_argument()
.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: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.Reference: http://bioportal.weizmann.ac.il/course/python/PyMOTW/PyMOTW/docs/argparse/index.html
What you showed us is the just the first example. The relevant sections from the Python docs:
http://docs.python.org/dev/library/argparse.html#action
http://docs.python.org/dev/library/argparse.html#metavar