Add argument checking for commands

This commit is contained in:
Mike Crute 2020-06-01 23:15:38 -07:00
parent 4df71cdc07
commit 27491bcb20
1 changed files with 14 additions and 1 deletions

View File

@ -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__":