Argparse

· 2 min read

To use argparse module, we should only know a few functions.

1.Creating a parser

parser = argparse.ArgumentParser(description='Head pose estimation using the Hopenet network.')

1.1.Function Details

  • description: Text to display before the argument help (default: none)

By function:

parser.print_help()

we can get:

usage: test2.py [-h] [--gpu GPU_ID] [--data_dir DATA_DIR]
                [--filename_list FILENAME_LIST] [--snapshot SNAPSHOT]
                [--batch_size BATCH_SIZE] [--save_viz SAVE_VIZ]
                [--dataset DATASET]

Head pose estimation using the Hopenet network.

optional arguments:
  -h, --help            show this help message and exit
  --gpu GPU_ID          GPU device id to use [0]
  --data_dir DATA_DIR   Directory path for data.
  --filename_list FILENAME_LIST
                        Path to text file containing relative paths for every
                        example.
  --snapshot SNAPSHOT   Name of model snapshot.
  --batch_size BATCH_SIZE
                        Batch size.
  --save_viz SAVE_VIZ   Save images with pose cube.
  --dataset DATASET     Dataset type.

2.Adding arguments

2.1.1Fucntion Details

parser.add_argument('--gpu', dest='gpu_id', help='GPU device id to use [0]', default=0, type=int)
parser.add_argument('--data_dir', dest='data_dir', help='Directory path for data.', default='', type=str)
parser.add_argument('--filename_list', dest='filename_list', help='Path to text file containing relative paths for every example.', default='', type=str)
parser.add_argument('--snapshot', dest='snapshot', help='Name of model snapshot.', default='', type=str)
parser.add_argument('--batch_size', dest='batch_size', help='Batch size.', default=1, type=int)
parser.add_argument('--save_viz', dest='save_viz', help='Save images with pose cube.', default=False, type=bool)
parser.add_argument('--dataset', dest='dataset', help='Dataset type.', default='AFLW2000', type=str)
  • name or flags: Either a name or a list of option strings, e.g. foo or -f, --foo.
  • dest: The name of the attribute to be added to the object returned by parse_args().
  • help: A brief description of what the argument does.
  • default: The value produced if the argument is absent from the command line.
  • type: The type to which the command-line argument should be converted.

2.2.2.name or flag

The add_argument() method must know whether an optional argument, like --gpu, or a positional argument, like a list of filenames, is expected. The first arguments passed to add_argument() must therefore be either a series of flags, or a simple argument name. For example, an optional argument could be created like:

parser.add_argument('--gpu', dest='gpu_id', help='GPU device id to use [0]', default=0, type=int)

while a positional argument could be created like: "Sorry, I do not have an example."

2.2.3.default

All optional arguments and some positonal arguments may be ommited at the command line. The default keyword argument of add_argument(), whose value defaults to None, specifies what value should be used if the command-line argument is not present. For optional arguments, the default value is used when the option string was not present at the command line:

parser.add_argument('--gpu', dest='gpu_id', help='GPU device id to use [0]', default=0, type=int)

2.2.4.type

By default, ArgumentParser objects read command-line arguments in as simple strings. However, quite often the command-line string should instead be interpreted as another type, like a float or int. The type keyword argument of add_argument() allows a necessary type-checking and type conversions to be performed. Common built-in types and functions can be used directly as the value of the type argument.

2.2.5.help

The help value is a string containing a brief description of the argument. When a user requests help, these help descriptions will be displayed with each argument.

2.2.6.dest

dest allows a custom attribute name to be provided:

parser.add_argument('--gpu', dest='gpu_id', help='GPU device id to use [0]', default=0, type=int)

3.Parsing arguments

parse_args() will be called with no argument, and the ArgumentParser will automatically determine the command-line arguments from sys.argv.

args = parser.parse_args()

After parse_args() method, the command line will be inspected and each argument will be converted to appropriate type which means a simple Namespace object will be built up from attributes parsed out of the command line.