Make the command a first tier argument
The command ("info", "reportGen", etc) should be a first tier argument, not just squirreled away as the first entry in arguments (or options.misc after #26 (closed)).
I think it should be as straightforward as adding
parser.add_argument('command', metavar="COMMAND", help="command to invoke", nargs=1, type=str.lower)
(The type=str.lower
makes it case insensitive.)
This will cause glance to automatically fail with an error if no command is provided. The error will look something like:
usage: [-h] COMMAND
glance: error: too few arguments
I think that's a feature.
We'd need to modify
parser.add_argument('misc', metavar='COMMAND [FILE [VARIABLE]]+', nargs='*')
to remove the COMMAND
bit.
The logic in main in compare.py should change from
# if what the user asked for is not one of our existing functions, print the help
if ((not args) or (args[0].lower() not in lower_locals)):
if options.version:
return 0
options.print_help()
help()
return 9
else:
# call the function the user named, given the arguments from the command line, lowercase the request to ignore case
rc = lower_locals[args[0].lower()](*args[1:])
return 0 if rc is None else rc
to
# if what the user asked for is not one of our existing functions, print the help
if ((options.command is None or options.command == "") or (options.command.lower() not in lower_locals)):
if options.version:
return 0
options.print_help()
help()
return 9
else:
# call the function the user named, given the arguments from the command line, lowercase the request to ignore case
rc = lower_locals[options.command.lower()](*args)
return 0 if rc is None else rc
For bonus points, have main()
pass a list of valid commands in to parse_arguments()
, and use that to set a list of choices=
so argparse can automatically filter invalid commands.
Edited by Alan De Smet