Better Command Contraints
Author: pauloortinsCreated Jul 6, 2013Updated Jan 26, 2024
In argparse we can add constraints to a command, eg:
In my calculator the command operator has to be add, subtract, multiply or divide.
In argparse, I write it in this way:
And I can get it with:
In docopt, we can also add contraints:
But when I get my argument, I will have to check which command is filled in the args dictionary.
parser.add_argument("operation", choices=['add', 'subtract', 'multiply', 'divide'])
args.operation
calc_docopt.py (add|subtract|multiply|divide)
{'--help': False,
'--version': False,
'': '1',
'': '2',
'add': True,
'divide': False,
'multiply': False,
'subtract': False}
Source: docopt/docopt