From 9180a07b8fb33d5ba0b82facf987b51ca7b15dc4 Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Tue, 13 Apr 2021 14:57:40 -0400 Subject: command: make --verbose/--quiet available to all subcommands Add new CommonOptions entry points to move the existing --jobs to, and relocate all --verbose/--quiet options to that. This provides both a consistent interface for users as well as for code. Change-Id: Ifaf83b88872421f4749b073c472b4a67ca6c0437 Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/303224 Reviewed-by: Raman Tenneti Tested-by: Mike Frysinger --- command.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) (limited to 'command.py') diff --git a/command.py b/command.py index f708832e..be2d6a6e 100644 --- a/command.py +++ b/command.py @@ -84,18 +84,34 @@ class Command(object): usage = 'repo %s' % self.NAME epilog = 'Run `repo help %s` to view the detailed manual.' % self.NAME self._optparse = optparse.OptionParser(usage=usage, epilog=epilog) + self._CommonOptions(self._optparse) self._Options(self._optparse) return self._optparse - def _Options(self, p): - """Initialize the option parser. + def _CommonOptions(self, p, opt_v=True): + """Initialize the option parser with common options. + + These will show up for *all* subcommands, so use sparingly. + NB: Keep in sync with repo:InitParser(). """ + g = p.add_option_group('Logging options') + opts = ['-v'] if opt_v else [] + g.add_option(*opts, '--verbose', + dest='output_mode', action='store_true', + help='show all output') + g.add_option('-q', '--quiet', + dest='output_mode', action='store_false', + help='only show errors') + if self.PARALLEL_JOBS is not None: p.add_option( '-j', '--jobs', type=int, default=self.PARALLEL_JOBS, help='number of jobs to run in parallel (default: %s)' % self.PARALLEL_JOBS) + def _Options(self, p): + """Initialize the option parser with subcommand-specific options.""" + def _RegisteredEnvironmentOptions(self): """Get options that can be set from environment variables. @@ -120,6 +136,11 @@ class Command(object): self.OptionParser.print_usage() sys.exit(1) + def CommonValidateOptions(self, opt, args): + """Validate common options.""" + opt.quiet = opt.output_mode is False + opt.verbose = opt.output_mode is True + def ValidateOptions(self, opt, args): """Validate the user options & arguments before executing. -- cgit v1.2.3-54-g00ecf