Linux

Python Argparse

The argparse module allows writing user-friendly command-line interfaces. The argparse module also automatically generates help and usage messages and issues errors when users give the program invalid arguments.
Let us explorer the argparse with basic syntaxes and positional arguments. Let us see the basic code of Python Argparse.
import argparse
parser = argparse.ArgumentParser()
parser.parse_args()
Click to view code
Let us see the output
Python basic argparse
Python basic argparse
  1. Running the script without option does not display anything
  2. With option -h, help the message get printed
  3. With an unknown option, the program gives an error

Positional Arguments

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("mohit")
arg =parser.parse_args()
print arg.mohit
Click to view code
Let us see the output
Python argparse Positional
Python argparse Positional
  1. The add_argument() method has been added, which is used to specify which command-line options the program is willing to accept. In this case, "mohit" is used.
  2. Calling our program now requires us to specify an option.
  3. The parse_args() method actually returns some data from the options specified,

Positional Arguments with Help message and type

Let us add help message and type.
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("Square", help="Get the square of the number ", type = int)
arg =parser.parse_args()
print arg.Square**2
Click to view code
Let us see the output
Python argparse Positional
Python argparse Positional
As you can see the program only accepts the int as optional arguments.

Argparse Optional Argument


You have seen the positional argument. Now you will how to add custom arguments.
ArgumentParser.add_argument(name or flags...[, action][, nargs][, const][, default][, type][, choices][, required][, help][, metavar][, dest])

Define how a single command-line argument should be parsed. Each parameter has its own more detailed description below, but in short they are:
  1. name or flags - Either a name or a list of option strings, e.g. foo or -f, --foo.
  2. action - The basic type of action to be taken when this argument is encountered at the command line.
  3. nargs - The number of command-line arguments that should be consumed.
  4. const - A constant value required by some action and nargs selections.
  5. default - The value produced if the argument is absent from the command line.
  6. type - The type to which the command-line argument should be converted.
  7. choices - A container of the allowable values for the argument.
  8. required - Whether or not the command-line option may be omitted (optionals only).
  9. help - A brief description of what the argument does.
  10. metavar - A name for the argument in usage messages.
  11. dest - The name of the attribute to be added to the object returned by parse_args().
When parse_args() is called, optional arguments will be identified by the - prefix
Let us discuss name or flag with action
Python argparse optional
Python argparse optional
Click to view code
If you use action = 'store_true' it means if -R is specified then it means True. The syntax action='store_const' used to give constant value.
Python argparse optional argument
Python argparse option


nargs

Let us how to supply multiple values with one arguments.
Python argparse optional
Python argparse optional
Click to view code
These are the basic details about the argaprse which are commonly used.
Python argparse optional argument
Python argparse option