Don't build full subparsers unless needed.  Don't double load modules.
@@ -66,15 +66,17 @@ def cli_entry(*args):
metavar='command',
dest='cmd'
)
+ subparser_lookup = { }
sub_parsers.required = True
for h in handlers:
mod = importlib.import_module('.{}'.format(h), 'kgtk.cli')
- sub_parser = sub_parsers.add_parser(h, **mod.parser())
+ subp = mod.parser()
+ sub_parser = sub_parsers.add_parser(h, **subp)
add_default_arguments(sub_parser) # call this before adding other arguments
- if hasattr(mod, "add_arguments_extended"):
- mod.add_arguments_extended(sub_parser, parsed_shared_args)
- else:
- mod.add_arguments(sub_parser)
+ subparser_lookup[h] = [mod, sub_parser]
+ if "aliases" in subp:
+ for alias in subp["aliases"]:
+ subparser_lookup[alias] = [mod, sub_parser]
# add root level usage after sub-parsers are created
# this won't pollute help info in sub-parsers
@@ -87,6 +89,17 @@ def cli_entry(*args):
parser.exit(KGTKArgumentParseException.return_code)
elif len(pipe) == 1: # single command
cmd_args = pipe[0]
+
+ cmd_name = cmd_args[0]
+ if cmd_name not in subparser_lookup:
+ parser.parse_args(cmd_args) # This will generate the right error message
+ sys.exit(1)
+ mod, sub_parser = subparser_lookup[cmd_name]
+ if hasattr(mod, "add_arguments_extended"):
+ mod.add_arguments_extended(sub_parser, parsed_shared_args)
+ else:
+ mod.add_arguments(sub_parser)
+
kwargs = {}
parsed_args = parser.parse_args(cmd_args)
@@ -94,7 +107,6 @@ def cli_entry(*args):
func = None
if parsed_args.cmd:
h = parsed_args.cmd
- mod = importlib.import_module('.{}'.format(h), 'kgtk.cli')
func = mod.run
# remove sub-command
