O'Reilly logo

Mastering Object-oriented Python by Steven Lott

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required