What is add_argument in the Python argparse module?
argparse is a Python module to streamline command-line interfaces by providing automation and easy usability. The program implementation defines what arguments it requires, and argparse parses the arguments out of sys.argv. The module also generates help, errors, and usage messages in case a user provides invalid arguments.
Creating a parser
First, we create an ArgumentParser object:
parser = argparse.ArgumentParser(description='Process some integers.')
The parser object in the above example is responsible for parsing the command line arguments into Python data types.
Adding arguments
To add program arguments, we call the add_argument method of the parser object. Generally, these calls instruct the parser object on transforming the string input on the command line into objects. We use the information stored in the parser object when parse_args is called.
Syntax
add_argument(name or flags...[, action][, nargs][, const][, default][, type][, choices][, required][, help][, metavar][, dest])
Parameters
name or flagsis the name or a list of option strings.actionis the action to be taken when this argument is found on the command line.nargsis the number of command-line arguments that should be read.constis a constant value required by somenargsandactionselections.defaultis the value produced when the argument is missing from the command-line and namespace object.typeis the data type to which command-line argument should be converted.choicesis the container of the allowed values for an argument.requiredspecifies if the command-line option may be omitted or not.helpis a brief description of what the argument does.metavaris the name for the argument in usage messages.destis the name of the attribute to be added to the object. This object is returned byparse_args.
Example
The following example shows how we can use the parser's add_argument method:
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)')
In the above example, we first create an argument of a list of integers.
- The argument name is
integers, with data typeint. - We give it the name
Nfor use in help messages. - The
+value fornargsgathers all command-line arguments into a list. - Finally, we label the purpose of the command-line argument in the
helpargument.
Then we create the --sum argument.
-
The argument’s name has
--before it, which means it is an optional argument. -
accumulateis the attribute’s name added to the object returned byparse_args. -
The
store_constaction stores the value as specified by theconstkeyword that issumin our case. -
The
defaultargument whose value ismaxdefines what value should be used if the command-line argument is absent. So, in our case, if--sumis not provided, the program will return the maximum of the input list of integers. -
Finally, we label the purpose of the command-line argument in the
helpargument.
When we call the parse_args method, it returns an object with two attributes, integers and accumulate. The integers attribute is a list of ints, and the accumulate attribute is the function that is executed on the list. If --sum was specified in the command line, then the sum function is executed, else max is executed by default.
Free Resources