I want my program to accept mutually exclusive positional arguments, and the usage to be displayed as a group of arguments.
Currently I could only achieve one or the other, but not both...
This is what I currently have:
def parse_arguments():
arg_parser = argparse.ArgumentParser(description = 'Project Builder')
query_parser = arg_parser.add_argument_group('query', "Query current state")
build_parser = arg_parser.add_argument_group('build', "Build project")
# query arguments
query_parser.add_argument('-s', '--servers',
action = 'store_true',
required = False,
help = 'Display available servers')
query_parser.add_argument('-u', '--users',
action = 'store_true',
required = False,
help = 'Display current users')
# build arguments
build_parser.add_argument('-f', '--force',
action = 'store',
required = False,
metavar = 'SERVER_NAME',
help = 'Force build on SERVER_NAME')
build_parser.add_argument('-c', '--clean',
action = 'store_true',
required = False,
help = 'Clean repo before build')
build_parser.add_argument('-v', '--verbosity',
action = 'store_true',
required = False,
help = 'Print stderr to console')
build_parser.add_argument('-p', '--project',
action = 'store',
required = True,
metavar = 'project_A|project_B|project_C',
type = project_name,
help = 'Project to build (required)')
return vars(arg_parser.parse_args())
args = parse_arguments()
Which gives the following:
usage: test.py [-h] [-s] [-u] [-f SERVER_NAME] [-c] [-v] -p
project_A|project_B|project_C
Project Builder
optional arguments:
-h, --help show this help message and exit
query:
Query current state
-s, --servers Display available servers
-u, --users Display current users
build:
Build project
-f SERVER_NAME, --force SERVER_NAME
Force build on SERVER_NAME
-c, --clean Clean repo before build
-v, --verbosity Print stderr to console
-p project_A|project_B|project_C, --project project_A|project_B|project_C
Project to build (required)
But what I really want is for query and build to be two mutually exclusive positional arguments.
I tried using subparsers as follows:
def parse_arguments():
arg_parser = argparse.ArgumentParser(description = 'Project Builder')
command_parser = arg_parser.add_subparsers(help = "Command")
query_parser = command_parser.add_parser('query', help = "Query current state")
build_parser = command_parser.add_parser('build', help = "Build project")
# query arguments
query_parser.add_argument('-s', '--servers',
action = 'store_true',
required = False,
help = 'Display available servers')
query_parser.add_argument('-u', '--users',
action = 'store_true',
required = False,
help = 'Display current users')
# build arguments
build_parser.add_argument('-f', '--force',
action = 'store',
required = False,
metavar = 'SERVER_NAME',
help = 'Force build on SERVER_NAME')
build_parser.add_argument('-c', '--clean',
action = 'store_true',
required = False,
help = 'Clean repo before build')
build_parser.add_argument('-v', '--verbosity',
action = 'store_true',
required = False,
help = 'Print stderr to console')
build_parser.add_argument('-p', '--project',
action = 'store',
required = True,
metavar = 'project_A|project_B|project_C',
type = project_name,
help = 'Project to build (required)')
return vars(arg_parser.parse_args())
But this produces the following:
usage: test.py [-h] {query,build} ...
Project Builder
positional arguments:
{query,build} Command
query Query current state
build Build project
optional arguments:
-h, --help show this help message and exit
Where what I want is a combination of the two attempts above, namely:
usage: test.py [-h] {query,build} ...
Project Builder
optional arguments:
-h, --help show this help message and exit
query:
Query current state
-s, --servers Display available servers
-u, --users Display current users
build:
Build project
-f SERVER_NAME, --force SERVER_NAME
Force build on SERVER_NAME
-c, --clean Clean repo before build
-v, --verbosity Print stderr to console
-p project_A|project_B|project_C, --project project_A|project_B|project_C
Project to build (required)
Where query and build are mutually exclusive.
I know about the ArgumentParser.add_mutually_exclusive_group(required=False) method, but using it didn't help achieving what I wanted, since 1) the arguments must be optional when using it, and 2) the usage format is not like I want.