Parsing the command line with argparse

The general approach to using argparse involves four steps.

  1. Create ArgumentParser. We can provide you with overall information about the command-line interface here. 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. Define the command-line options and arguments. This is done by adding arguments with the ArgumentParser.add_argument() method function.
  3. Parse the sys.argv command line to create a namespace object that details the options, option arguments and overall command-line arguments.
  4. Use the resulting namespace object to configure ...

Get Mastering Object-oriented Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.