PARSING COMMAND-LINE ARGUMENTS IN PYTHON

Parsing Command-Line Arguments In Python

Date Published:
Last Modified:

Overview

This tutorial uses the built-in Pyton module argparse.

Sub-Commands

Complex command-line programs can be simplified with the concept of sub-commands. Sub-commands are named commands as the first argument passed into a program, where each sub-command has it's own unique set of required and optional arguments.

A well-known program with sub-commands is git. The git command-line program has various sub-commands such as:

  • git clone
  • git add
  • git commit

Each one of these sub-commands has it's own required and optional arguments.

If you find yourself starting to add options that are only applicable when certain other arguments are present, then it might be time to look into sub-commands.

argparse has sub-command support through the use of add_subparsers(). The following working example shows a Python program which has two sub-commands, add and print.


Like this page? Upvote with shurikens!

Tags:

comments powered by Disqus