#122·docopt

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:

parser.add_argument("operation", choices=['add', 'subtract', 'multiply', 'divide'])
And I can get it with:

args.operation
In docopt, we can also add contraints:

calc_docopt.py (add|subtract|multiply|divide)  
But when I get my argument, I will have to check which command is filled in the args dictionary.

{'--help': False,
 '--version': False,
 '': '1',
 '': '2',
 'add': True,
 'divide': False,
 'multiply': False,
 'subtract': False}