diff options
author | Jake Hunsaker <jhunsake@redhat.com> | 2020-03-25 14:42:21 -0400 |
---|---|---|
committer | Jake Hunsaker <jhunsake@redhat.com> | 2020-04-08 09:27:16 -0400 |
commit | 7a02048e40dac7707b5e7660a9c2c027ce95cc59 (patch) | |
tree | 66d8202951d3ce017392900c305973a6c0d4f026 | |
parent | 89de73447dd8cfd85650f8778ab683417e194756 (diff) | |
download | sos-7a02048e40dac7707b5e7660a9c2c027ce95cc59.tar.gz |
[sos] Dynamically build argparser usage strings
Now dynamically build the usage string reported by --help for the
top-level parser (I.E. `sos --help`) to print available components.
Additionally, rework the subparser usage strings to accurately report
the syntax for subcommands.
Signed-off-by: Jake Hunsaker <jhunsake@redhat.com>
-rw-r--r-- | sos/__init__.py | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/sos/__init__.py b/sos/__init__.py index 73e60def..3e1aee10 100644 --- a/sos/__init__.py +++ b/sos/__init__.py @@ -47,11 +47,22 @@ class SoSComponent(): Added in 4.0 """ + desc = 'unset' + def __init__(self, parser, parsed_args, cmdline_args): self.parser = parser self.args = parsed_args self.cmdline = cmdline_args + @classmethod + def add_parser_options(cls, parser): + """This should be overridden by each subcommand to add its own unique + options to the parser + """ + pass + + + class SoS(): """Main entrypoint for sos from the command line @@ -63,12 +74,18 @@ class SoS(): def __init__(self, args): self.cmdline = args - usage_string = "%(prog)s component [options]\n\n" # define the local subcommands that exist on the system import sos.report self._components = {'report': sos.report.SoSReport} # build the top-level parser - self.parser = ArgumentParser(usage=usage_string) + _com_string = '' + for com in self._components: + _com_string += "\t%s\t\t\t%s\n" % (com, self._components[com].desc) + usage_string = ("%(prog)s <component> [options]\n\n" + "Available components:\n") + usage_string = usage_string + _com_string + epilog = ("See `sos <component> --help` for more information") + self.parser = ArgumentParser(usage=usage_string, epilog=epilog) self.parser.register('action', 'extend', SosListOption) # set the component subparsers self.subparsers = self.parser.add_subparsers( @@ -81,6 +98,7 @@ class SoS(): # for the component subparsers for comp in self._components: _com_subparser = self.subparsers.add_parser(comp) + _com_subparser.usage = "sos %s [options]" % comp _com_subparser.register('action', 'extend', SosListOption) self._add_common_options(_com_subparser) self._components[comp].add_parser_options(_com_subparser) @@ -118,4 +136,7 @@ class SoS(): print("Could not initialize '%s': %s" % (_com, err)) sys.exit(1) + def execute(self): + self._component.execute() + # vim: set et ts=4 sw=4 : |