Is there a way to specify two required arguments in argparse
, one that corresponds to a subcommand, and another that is required by all subcommands.
The closest I can manage seems to be with
import argparse
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(help='', dest='command', metavar='COMMAND', title='required arguments',
description='two arguments are required')
parser.add_argument('config', metavar='CONFIG', action='store', help='the config to use')
cmda_parser = subparsers.add_parser('cmdA', help='a first command')
cmdb_parser = subparsers.add_parser('cmdB', help='the second operation')
cmdc_parser = subparsers.add_parser('cmdC', help='yet another thing')
print(parser.parse_args())
which gives
usage: enigma.py [-h] COMMAND ... CONFIG
positional arguments:
CONFIG the config to use
optional arguments:
-h, --help show this help message and exit
required arguments:
two arguments are required
COMMAND
cmdA a first command
cmdB the second operation
cmdC yet another thing
and help for subcommands that does not show CONFIG
; but what I want is
usage: enigma.py [-h] COMMAND CONFIG
required arguments:
two arguments are required
COMMAND
cmdA a first command
cmdB the second operation
cmdC yet another thing
CONFIG the config to use
optional arguments:
-h, --help show this help message and exit
and help for each subcommand that does show CONFIG
, eg.
usage: enigma.py cmdA CONFIG [-h]
required arguments:
CONFIG the config to use
optional arguments:
-h, --help show this help message and exit
is there any way to accomplish this?
How to I specific two required arguments, one of which is a subcommand, with the second "propagated" to each subcommand as a required argument?