Parsing the command line with argparse

The general approach to using argparse involves the following four steps:

  1. First, we create an ArgumentParser instance. We can provide this object with overall information about the command-line interface. This might include a description, format changes for the displayed options and arguments, and whether or not -h is the help option. Generally, we only need to provide the description; the rest of the options have sensible defaults.
  2. Then, we define the command-line options and arguments. This is done by adding arguments with the ArgumentParser.add_argument() method function.
  3. Next, we parse the sys.argv command line to create a namespace object that details the options, option arguments, and overall command-line arguments.
  4. Lastly, we use the namespace object to configure the application and process the arguments. There are a number of alternative approaches to handle this gracefully. This may involve parsing configuration files as well as command-line options. We'll look at several designs in this section.

An important feature of argparse is that it provides us with a unified view of options and arguments. The principle difference between the two is the number of times an option or argument value can occur. Options are—well, optional, and can occur one or no times. Arguments generally occur one or more times.

We can create a parser with code like the following:

parser = argparse.ArgumentParser(
description="Simulate Blackjack")

We provided the description, as there's no good default value for that. Here are some common patterns to define the command-line API for an application:

  • A simple on–off option: We'll often see this as a -v or --verbose option.
  • An option with an argument: This might be a -s ',' or --separator '|' option.
  • Any positional argument: This might be used when we have an input file and an output file as command-line arguments. This is rare, and should be avoided because it's never perfectly clear what the order should be.
  • All other arguments: We'd use these when we have a list of input files.
  • --version: This is a special option to display the version number and exit.
  • --help: This option will display help and exit. This is a default, and so we don't need to do anything to make this happen.

Once the arguments have been defined, we can parse them and use them. Here's how we parse them:

config = parser.parse_args() 

The config object is an argparse.Namespace object; the class is similar to types.SimpleNamespace. It will have a number of attributes, and we can easily add more attributes to this object.

We'll look at each of these six common kinds of arguments individually. There are a lot of clever and sophisticated parsing options available in the ArgumentParser class. Most of them go beyond the simplistic guidelines commonly suggested for command-line argument processing. In general, we should avoid the kind of super-complex options that characterize programs such as find. When command-line options get terribly complex, we may have drifted into creating a domain-specific language on top of Python. This often means we're creating a kind of framework, not simply creating an application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset