Library argparse

Library argparse#

argparse is Python’s built-in library that allows the creation of typical command-line interfaces for Python scripts. Findout more in the specific section of the official documentation.

import argparse

Parse arguments#

For experimentation purposes, it’s easier to pass arguments to be parsed as a list - ArgumentParser.parse_args allows this option.


The following cell creates an ArgumentParser, adds an argument to it, and shows the result of parsing.

my_parser = argparse.ArgumentParser(description='Process some integers.')
my_parser.add_argument("--value")
ans = my_parser.parse_args(["--value", "10"])
ans
Namespace(value='10')

It returns the same object as it would if defined in a script.

ans.value
'10'

Pring help#

To print the --help message, use ArgumentParser.print_help.


The following cell shows an example of such usage.

my_parser = argparse.ArgumentParser(description='Process some integers.')
my_parser.add_argument("--value")
my_parser.print_help()
usage: ipykernel_launcher.py [-h] [--value VALUE]

Process some integers.

options:
  -h, --help     show this help message and exit
  --value VALUE

Name with dash#

When you have an argument with a multi-word name, it’s typical to separate the words with a dash (e.g., --my-argument). However, in Python, variable and attribute names cannot contain dashes. Therefore, the ArgumentParser will convert the dashed name to a format that is valid in Python, typically by replacing dashes with underscores. For example, --my-argument would be accessible as args.my_argument in the Python code.


The following example adds an argument that has a dash in its name.

my_parser = argparse.ArgumentParser()
my_parser.add_argument("--value-dash")
ans = my_parser.parse_args(["--value-dash", "10"])
ans.value_dash
'10'

After parsing, in Python, you can access the obtained value using the value_dash name.

metavar#

Official description, to my mind, isn’t really clear so here I present alternative description.

It’s typical for CLI utilities to show in the help how to pass values to them. For example, grep has a --label option that takes a value, and it’s help describes that you should use the syntax --label=<passed value> for it, which is just shown in the cell below. So here LABEL is a value that should be replaced with an argument during the call, and this type of constraint is called metavar.

%%bash
grep --help | grep label
      --label=LABEL         use LABEL as the standard input file name prefix

In argparse, every option that takes an argument has metavar as the upper case of dest, which is typical for cli units. But you can set your own using the metavar argument of the add_argument method. For arguments that take multiple values, you can set metavar as a tuple.


The following cell creates an ArgumentParser that uses metavars.

my_parser = argparse.ArgumentParser(description='Process some integers.')
my_parser.add_argument("--option")
my_parser.add_argument("--set_metavar", metavar="this_is_metavar")
my_parser.add_argument(
    "--double_metavar",
    nargs=2,
    metavar=('value1', 'value2')
)
_StoreAction(option_strings=['--double_metavar'], dest='double_metavar', nargs=2, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=('value1', 'value2'))

This is what the --help of this programme looks like. Pay attention to the metavars of the options.

my_parser.print_help()
usage: ipykernel_launcher.py [-h] [--option OPTION]
                             [--set_metavar this_is_metavar]
                             [--double_metavar value1 value2]

Process some integers.

options:
  -h, --help            show this help message and exit
  --option OPTION
  --set_metavar this_is_metavar
  --double_metavar value1 value2

The following cell shows the results of the parsing.

my_parser.parse_args([
    "--option", "option_value", 
    "--set_metavar", "metavar_value", 
    "--double_metavar", "value1", "value2"
])
Namespace(option='option_value', set_metavar='metavar_value', double_metavar=['value1', 'value2'])