From ae6cb08ae52d488a4cc6892f811c1c1acf8c3c12 Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Tue, 27 Aug 2019 01:10:59 -0400 Subject: split out cli validation from execution A common pattern in our subcommands is to verify the arguments & options before executing things. For some subcommands, that check stage is quite long which makes the execution function even bigger. Lets split that logic out of the execute phase so it's easier to manage these. This is most noticeable in the sync subcommand whose Execute func is quite large, and the option checking makes up ~15% of it. The manifest command's Execute can be simplified significantly as the optparse configuration always sets output_file to a string. Change-Id: I7097847ff040e831345e63de6b467ee17609990e Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/234834 Reviewed-by: David Pursehouse Tested-by: Mike Frysinger --- command.py | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'command.py') diff --git a/command.py b/command.py index 8c5e2461..f7d20a22 100644 --- a/command.py +++ b/command.py @@ -98,6 +98,16 @@ class Command(object): self.OptionParser.print_usage() sys.exit(1) + def ValidateOptions(self, opt, args): + """Validate the user options & arguments before executing. + + This is meant to help break the code up into logical steps. Some tips: + * Use self.OptionParser.error to display CLI related errors. + * Adjust opt member defaults as makes sense. + * Adjust the args list, but do so inplace so the caller sees updates. + * Try to avoid updating self state. Leave that to Execute. + """ + def Execute(self, opt, args): """Perform the action, after option parsing is complete. """ -- cgit v1.2.3-54-g00ecf