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 flags is the name or a list of option strings.
  • action is the action to be taken when this argument is found on the command line.
  • nargs is the number of command-line arguments that should be read.
  • const is a constant value required by some nargs and action selections.
  • default is the value produced when the argument is missing from the command-line and namespace object.
  • type is the data type to which command-line argument should be converted.
  • choices is the container of the allowed values for an argument.
  • required specifies if the command-line option may be omitted or not.
  • help is a brief description of what the argument does.
  • metavar is the name for the argument in usage messages.
  • dest is the name of the attribute to be added to the object. This object is returned by parse_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 type int.
  • We give it the name N for use in help messages.
  • The + value for nargs gathers all command-line arguments into a list.
  • Finally, we label the purpose of the command-line argument in the help argument.

Then we create the --sum argument.

  • The argument’s name has -- before it, which means it is an optional argument.

  • accumulate is the attribute’s name added to the object returned by parse_args.

  • The store_const action stores the value as specified by the const keyword that is sum in our case.

  • The default argument whose value is max defines what value should be used if the command-line argument is absent. So, in our case, if --sum is 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 help argument.

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

Copyright ©2025 Educative, Inc. All rights reserved