argparse
is a great library to write CLI application with clean consistent interface. The main features are
- Sub-parsers
- Positional arguments
- Options arguments
Full docs can found at link
It’s definitely better than DIY parsing sys.argv
.
An example Link to heading
This is an examples that shows the most important features. It even prints nifty help message like this
import argparse
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(help='Super duper commands',dest='command')
parser.add_argument('--verbose', help='be more chatty')
parser_batch = subparsers.add_parser('batch', help='Run in Batch mode')
parser_batch.add_argument('input_file', help='input file')
parser_gui = subparsers.add_parser('gui', help='Run in GUI mode')
args = parser.parse_args()