From a7eef069d3b7655d9dc1a52d4fd812b49a067ee2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rud=C3=A1=20Moura?= Date: Fri, 15 Aug 2014 11:31:13 -0300 Subject: [PATCH] avocado.cli.app: Fix to display all --help options. Fix the way that we handle argument parsing, to display all options for subcommands when --help is passed. We fix the problem by using parents parse. Plus: cosmetic fixes in avocado.plugins regarding argument parse. Signed-off-by: Ruda Moura --- avocado/cli/app.py | 15 +++-- avocado/plugins/datadir.py | 7 ++- avocado/plugins/jsonresult.py | 9 +-- avocado/plugins/lister.py | 6 +- avocado/plugins/multiplexer.py | 6 +- avocado/plugins/runner.py | 58 +++++++++---------- avocado/plugins/silentresult.py | 6 +- avocado/plugins/xunit.py | 8 +-- .../all/functional/avocado/basic_tests.py | 2 +- 9 files changed, 63 insertions(+), 54 deletions(-) diff --git a/avocado/cli/app.py b/avocado/cli/app.py index e8636c8b..c4be1745 100644 --- a/avocado/cli/app.py +++ b/avocado/cli/app.py @@ -31,12 +31,15 @@ class AvocadoApp(object): """ def __init__(self, external_plugins=None): + # Catch all libc runtime errors to STDERR os.environ['LIBC_FATAL_STDERR_'] = '1' + self.external_plugins = external_plugins self.plugin_manager = None self.app_parser = ArgumentParser(prog='avocado', version=VERSION, + add_help=False, # see parent parse description='Avocado Test Runner') self.app_parser.add_argument('-V', '--verbose', action='store_true', help='print extra debug messages', @@ -50,12 +53,16 @@ class AvocadoApp(object): self.app_parser.add_argument('--plugins', action='store', help='Load extra plugins from directory', dest='plugins_dir', default='') - args, _ = self.app_parser.parse_known_args() - self.cmd_parser = self.app_parser.add_subparsers(title='subcommands', - description='valid subcommands', - help='subcommand help') + # Use parent parsing to avoid break output of --help option + self.app_parser = ArgumentParser(parents=[self.app_parser]) + + # Subparsers where Avocado subcommands are plugged + self.cmd_parser = self.app_parser.add_subparsers( + title='subcommands', + description='valid subcommands', + help='subcommand help') self.load_plugin_manager(args.plugins_dir) args, _ = self.app_parser.parse_known_args() self.plugin_manager.activate(args) diff --git a/avocado/plugins/datadir.py b/avocado/plugins/datadir.py index 82e702b7..9f980ebe 100644 --- a/avocado/plugins/datadir.py +++ b/avocado/plugins/datadir.py @@ -27,9 +27,10 @@ class DataDirList(plugin.Plugin): enabled = True def configure(self, app_parser, cmd_parser): - myparser = cmd_parser.add_parser('datadir', - help='List all relevant dirs used by avocado') - myparser.set_defaults(func=self.list_data_dirs) + parser = cmd_parser.add_parser( + 'datadir', + help='List all relevant directories used by avocado') + parser.set_defaults(func=self.list_data_dirs) self.configured = True def list_data_dirs(self, args): diff --git a/avocado/plugins/jsonresult.py b/avocado/plugins/jsonresult.py index c2414446..570e9284 100644 --- a/avocado/plugins/jsonresult.py +++ b/avocado/plugins/jsonresult.py @@ -95,10 +95,11 @@ class JSON(plugin.Plugin): def configure(self, app_parser, cmd_parser): self.parser = app_parser - self.parser.add_argument('--json', type=str, - dest='json_output', - help='Enable JSON output to the file where the result should be written.' - "Use '-' to redirect to the standard output.") + self.parser.add_argument( + '--json', type=str, + dest='json_output', + help='Enable JSON output to the file where the result should be written.' + "Use '-' to redirect to the standard output.") self.configured = True def activate(self, app_args): diff --git a/avocado/plugins/lister.py b/avocado/plugins/lister.py index bd57091c..d7c2c1a9 100644 --- a/avocado/plugins/lister.py +++ b/avocado/plugins/lister.py @@ -27,9 +27,9 @@ class PluginsList(plugin.Plugin): enabled = True def configure(self, app_parser, cmd_parser): - myparser = cmd_parser.add_parser('plugins', - help='List all plugins loaded') - myparser.set_defaults(func=self.list_plugins) + parser = cmd_parser.add_parser('plugins', + help='List all plugins loaded') + parser.set_defaults(func=self.list_plugins) self.configured = True def list_plugins(self, args): diff --git a/avocado/plugins/multiplexer.py b/avocado/plugins/multiplexer.py index abc9ac49..ce8fbcda 100644 --- a/avocado/plugins/multiplexer.py +++ b/avocado/plugins/multiplexer.py @@ -36,11 +36,11 @@ class Multiplexer(plugin.Plugin): myparser.add_argument('multiplex_file', type=str, help='Path to a multiplex file ', - nargs='?', default=None) + default=None) myparser.add_argument('-c', '--contents', action='store_true', - help=('Keep temporary files generated by tests. ' - 'Default: %(defaults)'), default=False) + help='Keep temporary files generated by tests.', + default=False) myparser.set_defaults(func=self.multiplex) self.configured = True diff --git a/avocado/plugins/runner.py b/avocado/plugins/runner.py index f35c2d0e..4ddeb94d 100644 --- a/avocado/plugins/runner.py +++ b/avocado/plugins/runner.py @@ -41,9 +41,9 @@ class TestLister(plugin.Plugin): :param parser: Main test runner parser. """ - myparser = cmd_parser.add_parser('list', - help='List available test modules') - myparser.set_defaults(func=self.list_tests) + parser = cmd_parser.add_parser('list', + help='List available test modules') + parser.set_defaults(func=self.list_tests) self.configured = True def list_tests(self, args): @@ -91,33 +91,31 @@ class TestRunner(plugin.Plugin): :param parser: Main test runner parser. """ - myparser = cmd_parser.add_parser('run', help=('Run a list of test modules ' - 'or dropin tests ' - '(space separated)')) + parser = cmd_parser.add_parser( + 'run', + help='Run a list of test modules or dropin tests (space separated)') - myparser.add_argument('url', type=str, - help=('Test module names or paths to dropin tests ' - '(space separated)'), - nargs='?', default=None) + parser.add_argument('url', type=str, default=None, + help=('Test module names or paths to dropin tests ' + '(space separated)')) - myparser.add_argument('-z', '--archive', action='store_true', default=False, - help='Archive (ZIP) files generated by tests.') + parser.add_argument('-z', '--archive', action='store_true', default=False, + help='Archive (ZIP) files generated by tests.') - myparser.add_argument('-m', '--multiplex-file', type=str, - help=('Path to an avocado multiplex ' - '(.mplex) file '), - nargs='?', default=None) + parser.add_argument('-m', '--multiplex-file', type=str, default=None, + help=('Path to an avocado multiplex ' + '(.mplex) file '), + nargs='?') - myparser.add_argument('--keep-tmp-files', action='store_true', - help=('Keep temporary files generated by tests. ' - 'Default: %(defaults)'), default=False) + parser.add_argument('--keep-tmp-files', action='store_true', default=False, + help='Keep temporary files generated by tests.') - myparser.add_argument('--unique-id', type=str, default=None, - help=('Unique Job id. Used by a server when job ' - 'was created at the server and run on a ' - 'different test machine')) + parser.add_argument('--unique-id', type=str, default=None, + help=('Unique Job id. Used by a server when job ' + 'was created at the server and run on a ' + 'different test machine')) - myparser.set_defaults(func=self.run_tests) + parser.set_defaults(func=self.run_tests) self.configured = True def run_tests(self, args): @@ -145,10 +143,10 @@ class SystemInformation(plugin.Plugin): :param parser: Main test runner parser. """ - myparser = cmd_parser.add_parser('sysinfo', - help='Collect system information') - myparser.add_argument('sysinfodir', type=str, - help='Dir where to dump sysinfo', - nargs='?', default='') - myparser.set_defaults(func=sysinfo.collect_sysinfo) + parser = cmd_parser.add_parser('sysinfo', + help='Collect system information') + parser.add_argument('sysinfodir', type=str, + help='Dir where to dump sysinfo', + nargs='?', default='') + parser.set_defaults(func=sysinfo.collect_sysinfo) self.configured = True diff --git a/avocado/plugins/silentresult.py b/avocado/plugins/silentresult.py index 1b4e15ac..c90d08f1 100644 --- a/avocado/plugins/silentresult.py +++ b/avocado/plugins/silentresult.py @@ -31,6 +31,8 @@ class Silent(plugin.Plugin): enabled = True def configure(self, app_parser, cmd_parser): - self.parser = app_parser - self.parser.add_argument('--silent', action='store_true', default=False) + parser = app_parser + parser.add_argument( + '--silent', action='store_true', default=False, + help='Silent output, do not display results.') self.configured = True diff --git a/avocado/plugins/xunit.py b/avocado/plugins/xunit.py index 1af01f02..c286e0b7 100644 --- a/avocado/plugins/xunit.py +++ b/avocado/plugins/xunit.py @@ -229,10 +229,10 @@ class XUnit(plugin.Plugin): def configure(self, app_parser, cmd_parser): self.parser = app_parser - app_parser.add_argument('--xunit', type=str, - dest='xunit_output', - help=('Enable xUnit output to the file where the result should be written.' - "Use '-' to redirect to the standard output.")) + app_parser.add_argument( + '--xunit', type=str, dest='xunit_output', + help=('Enable xUnit output to the file where the result should be written.' + "Use '-' to redirect to the standard output.")) self.configured = True def activate(self, app_args): diff --git a/selftests/all/functional/avocado/basic_tests.py b/selftests/all/functional/avocado/basic_tests.py index 23436368..40b6fe55 100644 --- a/selftests/all/functional/avocado/basic_tests.py +++ b/selftests/all/functional/avocado/basic_tests.py @@ -159,7 +159,7 @@ class RunnerOperationTest(unittest.TestCase): cmd_line = './scripts/avocado run' result = process.run(cmd_line, ignore_status=True) expected_rc = 2 - expected_output = 'Empty test ID. A test path or alias must be provided' + expected_output = 'avocado run: error: too few arguments' self.assertEqual(result.exit_status, expected_rc) self.assertIn(expected_output, result.stderr) -- GitLab