0

I was wondering if I could have dependent options in docopt.

example:

"""
Description:
  Flash a system with the manufacturing software from the specifiedx folder.

Usage:
  flash_drop.py (--drop-dir=<DIR>) [--factory-reset=<BOOL>] [--flash-all=<BOOL>] [--flash-system1=<BOOL> | --flash-system2=<BOOL>]
  flash_drop.py -h | --help
  flash_drop.py --version

Options:
  -h --help                 Show this screen.
  --version                 Show version.
  --drop-dir=DIR            Path to the drop directory
  --factory-reset=BOOL          Factory reset the chips on all selected devices. [default: False]
  --flash-all=BOOL              Flash all devices. [default: False]
  --flash-system1=BOOL          Flash first system. [default: False]
  --flash-system2=BOOL          Flash second system. [default: False]
"""

Namely, the value of an option is ignored if a previous option hasn't been selected. So for instance, the value for --flash-system2 is ignored unless --flash-system1 is set

Jack
  • 722
  • 3
  • 8
  • 24

2 Answers2

0

If it's possible to modify the code you can ignore them programmatically for example:

args['flash-system2'] = args['flash-system2'] if args['flash-system1'] else None

or

if ('flash-system2' in args) and ('flash-system1' not in args):
    del args['flash-system2']

just after parsing the arguments

MrMaxPayne
  • 173
  • 5
  • I'm doing it like that for now, but I was just wondering, if docopt had a functionality like this. – Jack Apr 15 '19 at 09:49
  • not as far as I know. there isn't a functionality as such. It would be cool, however, if we can say to the user you can't specify `flash-system2` if `flash-system1` is not given without having to write it. – MrMaxPayne Apr 15 '19 at 10:37
0

Not with a single usage pattern, but you can do it with 2 patterns:

Usage:
  flash_drop.py (--drop-dir=<DIR>) [options] [--flash-system1=<BOOL>]
  flash_drop.py (--drop-dir=<DIR>) [options] --flash-system1=<BOOL> --flash-system2=<BOOL>

But probably better with three pattern, easier to read IMO:

Usage:
  flash_drop.py (--drop-dir=<DIR>) [options]
  flash_drop.py (--drop-dir=<DIR>) [options] --flash-system1=<BOOL>
  flash_drop.py (--drop-dir=<DIR>) [options] --flash-system1=<BOOL> --flash-system2=<BOOL>
  flash_drop.py -h | --help
  flash_drop.py --version

Live demo


P.S.

Well, you can, technically, do it with a single pattern, but it starts to get really long...

Usage:
  flash_drop.py (--drop-dir=<DIR>) [options] [(--flash-system1=<BOOL>) | (--flash-system1=<BOOL> --flash-system2=<BOOL>)]

Lines can be broken, so maybe:

Usage:
  flash_drop.py (--drop-dir=<DIR>) [options] 
                [(--flash-system1=<BOOL>) | (--flash-system1=<BOOL> --flash-system2=<BOOL>)]

Personally I prefer the 3-pattern solution.

frnhr
  • 12,354
  • 9
  • 63
  • 90