What is argparse?
The Python argparse module defines what arguments it requires, and argparse will figure out how to parse those out of sys.argv. It also automatically generates help and usage messages and issues errors when users give the program incalid arguments.
How to use the argparse program?
create an ArgumentParser object
parser = argparse.ArgumentParse(description="Process some integers")
- description: a brief description of what the program does and how it works.
add argument
parser.add_argument('integers', metavar='N', type=int, nargs="+", help='an integer for the accumulator')
parser.add_argument('--sum', const=sum, default=max, help='sum the integers (default: find the max)')
- nargs=”+”: ‘+’ just like ‘*’, all command-line args present are gathered into a list.
- help message: when type
python <name of script> -h, it shows a list of available arguments and its associated help message if the help message is specified inArgumentParserobject.
parse argument
parser.parse_args(['--sum', '7', '-1', '-42'])
=> Namespace(accumulate=<built-in function sum>, integers=[7, -1, 42])
- The
Namespaceobject: it is an object created by theargparse.Namespaceclass used by default byparse_args(). This object holds attributes and its value in theArgumentParserobject.
Example 1:
args = parser.parse_args(['--foo', BAR]) => type(args) is an Namespace object
vars(args) => {'foo': 'BAR'}
Example 2: have an ArgmentPaser assign attributes to an already existing object, rather than a new NameSpace object.
class C:
pass
c = C()
parser = argparse.ArgumentParser()
parser.add_argument('--foo')
parser.parse_args(['--foo', BAR], namespace=c)
c.foo => 'BAR'
Example 3: directly create a Namespace object
from argparse import Namespace
args = Namespace(foo='bar')
- Namespace object can be saved and re-loaded using pickle.