Book Image

Mastering Object-oriented Python

By : Steven F. Lott, Steven F. Lott
Book Image

Mastering Object-oriented Python

By: Steven F. Lott, Steven F. Lott

Overview of this book

Table of Contents (26 chapters)
Mastering Object-oriented Python
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Some Preliminaries
Index

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 the application and process the arguments. There are a number of alternative approaches to handle this gracefully. It may involve parsing configuration files, as well as command-line options. We'll look at several designs.

An important feature...