From 27491bcb200e78ce114e2452524ffd834bc0f022 Mon Sep 17 00:00:00 2001 From: Mike Crute Date: Mon, 1 Jun 2020 23:15:38 -0700 Subject: [PATCH] Add argument checking for commands --- scripts/builder.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/scripts/builder.py b/scripts/builder.py index 270a9f8..2107474 100755 --- a/scripts/builder.py +++ b/scripts/builder.py @@ -777,6 +777,12 @@ def main(): rely on object state as it is not invoked with an instance of the object. + check_args(self, args) (class method, optional) + passed the set of arguments that were parsed before the command was + invoked. This function should return a list of errors or None. + Non-empty lists will cause the command to not be executed and help + being printed. + run(self, args, root, log) (instance method) passed the arguments object as parsed by argparse as well as a string indicating the root of the repository (the folder containing @@ -811,8 +817,15 @@ def main(): command.add_args(subparser) args = parser.parse_args() + command = dispatch[args.command_name] - command.run(args, find_repo_root(), logger) + errors = getattr(command, "check_args", lambda x: [])(args) + if errors: + logger.error("\n".join(errors)) + # Ugly hack, gets the help for the subcommand, no public API for this + parser._actions[1]._name_parser_map[args.command_name].print_help() + else: + command.run(args, find_repo_root(), logger) if __name__ == "__main__":