提交 a7eef069 编写于 作者: R Rudá Moura 提交者: Ruda Moura

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: NRuda Moura <rmoura@redhat.com>
上级 6d55fda4
...@@ -31,12 +31,15 @@ class AvocadoApp(object): ...@@ -31,12 +31,15 @@ class AvocadoApp(object):
""" """
def __init__(self, external_plugins=None): def __init__(self, external_plugins=None):
# Catch all libc runtime errors to STDERR # Catch all libc runtime errors to STDERR
os.environ['LIBC_FATAL_STDERR_'] = '1' os.environ['LIBC_FATAL_STDERR_'] = '1'
self.external_plugins = external_plugins self.external_plugins = external_plugins
self.plugin_manager = None self.plugin_manager = None
self.app_parser = ArgumentParser(prog='avocado', self.app_parser = ArgumentParser(prog='avocado',
version=VERSION, version=VERSION,
add_help=False, # see parent parse
description='Avocado Test Runner') description='Avocado Test Runner')
self.app_parser.add_argument('-V', '--verbose', action='store_true', self.app_parser.add_argument('-V', '--verbose', action='store_true',
help='print extra debug messages', help='print extra debug messages',
...@@ -50,12 +53,16 @@ class AvocadoApp(object): ...@@ -50,12 +53,16 @@ class AvocadoApp(object):
self.app_parser.add_argument('--plugins', action='store', self.app_parser.add_argument('--plugins', action='store',
help='Load extra plugins from directory', help='Load extra plugins from directory',
dest='plugins_dir', default='') dest='plugins_dir', default='')
args, _ = self.app_parser.parse_known_args() 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) self.load_plugin_manager(args.plugins_dir)
args, _ = self.app_parser.parse_known_args() args, _ = self.app_parser.parse_known_args()
self.plugin_manager.activate(args) self.plugin_manager.activate(args)
......
...@@ -27,9 +27,10 @@ class DataDirList(plugin.Plugin): ...@@ -27,9 +27,10 @@ class DataDirList(plugin.Plugin):
enabled = True enabled = True
def configure(self, app_parser, cmd_parser): def configure(self, app_parser, cmd_parser):
myparser = cmd_parser.add_parser('datadir', parser = cmd_parser.add_parser(
help='List all relevant dirs used by avocado') 'datadir',
myparser.set_defaults(func=self.list_data_dirs) help='List all relevant directories used by avocado')
parser.set_defaults(func=self.list_data_dirs)
self.configured = True self.configured = True
def list_data_dirs(self, args): def list_data_dirs(self, args):
......
...@@ -95,10 +95,11 @@ class JSON(plugin.Plugin): ...@@ -95,10 +95,11 @@ class JSON(plugin.Plugin):
def configure(self, app_parser, cmd_parser): def configure(self, app_parser, cmd_parser):
self.parser = app_parser self.parser = app_parser
self.parser.add_argument('--json', type=str, self.parser.add_argument(
dest='json_output', '--json', type=str,
help='Enable JSON output to the file where the result should be written.' dest='json_output',
"Use '-' to redirect to the standard output.") help='Enable JSON output to the file where the result should be written.'
"Use '-' to redirect to the standard output.")
self.configured = True self.configured = True
def activate(self, app_args): def activate(self, app_args):
......
...@@ -27,9 +27,9 @@ class PluginsList(plugin.Plugin): ...@@ -27,9 +27,9 @@ class PluginsList(plugin.Plugin):
enabled = True enabled = True
def configure(self, app_parser, cmd_parser): def configure(self, app_parser, cmd_parser):
myparser = cmd_parser.add_parser('plugins', parser = cmd_parser.add_parser('plugins',
help='List all plugins loaded') help='List all plugins loaded')
myparser.set_defaults(func=self.list_plugins) parser.set_defaults(func=self.list_plugins)
self.configured = True self.configured = True
def list_plugins(self, args): def list_plugins(self, args):
......
...@@ -36,11 +36,11 @@ class Multiplexer(plugin.Plugin): ...@@ -36,11 +36,11 @@ class Multiplexer(plugin.Plugin):
myparser.add_argument('multiplex_file', type=str, myparser.add_argument('multiplex_file', type=str,
help='Path to a multiplex file ', help='Path to a multiplex file ',
nargs='?', default=None) default=None)
myparser.add_argument('-c', '--contents', action='store_true', myparser.add_argument('-c', '--contents', action='store_true',
help=('Keep temporary files generated by tests. ' help='Keep temporary files generated by tests.',
'Default: %(defaults)'), default=False) default=False)
myparser.set_defaults(func=self.multiplex) myparser.set_defaults(func=self.multiplex)
self.configured = True self.configured = True
......
...@@ -41,9 +41,9 @@ class TestLister(plugin.Plugin): ...@@ -41,9 +41,9 @@ class TestLister(plugin.Plugin):
:param parser: Main test runner parser. :param parser: Main test runner parser.
""" """
myparser = cmd_parser.add_parser('list', parser = cmd_parser.add_parser('list',
help='List available test modules') help='List available test modules')
myparser.set_defaults(func=self.list_tests) parser.set_defaults(func=self.list_tests)
self.configured = True self.configured = True
def list_tests(self, args): def list_tests(self, args):
...@@ -91,33 +91,31 @@ class TestRunner(plugin.Plugin): ...@@ -91,33 +91,31 @@ class TestRunner(plugin.Plugin):
:param parser: Main test runner parser. :param parser: Main test runner parser.
""" """
myparser = cmd_parser.add_parser('run', help=('Run a list of test modules ' parser = cmd_parser.add_parser(
'or dropin tests ' 'run',
'(space separated)')) help='Run a list of test modules or dropin tests (space separated)')
myparser.add_argument('url', type=str, parser.add_argument('url', type=str, default=None,
help=('Test module names or paths to dropin tests ' help=('Test module names or paths to dropin tests '
'(space separated)'), '(space separated)'))
nargs='?', default=None)
myparser.add_argument('-z', '--archive', action='store_true', default=False, parser.add_argument('-z', '--archive', action='store_true', default=False,
help='Archive (ZIP) files generated by tests.') help='Archive (ZIP) files generated by tests.')
myparser.add_argument('-m', '--multiplex-file', type=str, parser.add_argument('-m', '--multiplex-file', type=str, default=None,
help=('Path to an avocado multiplex ' help=('Path to an avocado multiplex '
'(.mplex) file '), '(.mplex) file '),
nargs='?', default=None) nargs='?')
myparser.add_argument('--keep-tmp-files', action='store_true', parser.add_argument('--keep-tmp-files', action='store_true', default=False,
help=('Keep temporary files generated by tests. ' help='Keep temporary files generated by tests.')
'Default: %(defaults)'), default=False)
myparser.add_argument('--unique-id', type=str, default=None, parser.add_argument('--unique-id', type=str, default=None,
help=('Unique Job id. Used by a server when job ' help=('Unique Job id. Used by a server when job '
'was created at the server and run on a ' 'was created at the server and run on a '
'different test machine')) 'different test machine'))
myparser.set_defaults(func=self.run_tests) parser.set_defaults(func=self.run_tests)
self.configured = True self.configured = True
def run_tests(self, args): def run_tests(self, args):
...@@ -145,10 +143,10 @@ class SystemInformation(plugin.Plugin): ...@@ -145,10 +143,10 @@ class SystemInformation(plugin.Plugin):
:param parser: Main test runner parser. :param parser: Main test runner parser.
""" """
myparser = cmd_parser.add_parser('sysinfo', parser = cmd_parser.add_parser('sysinfo',
help='Collect system information') help='Collect system information')
myparser.add_argument('sysinfodir', type=str, parser.add_argument('sysinfodir', type=str,
help='Dir where to dump sysinfo', help='Dir where to dump sysinfo',
nargs='?', default='') nargs='?', default='')
myparser.set_defaults(func=sysinfo.collect_sysinfo) parser.set_defaults(func=sysinfo.collect_sysinfo)
self.configured = True self.configured = True
...@@ -31,6 +31,8 @@ class Silent(plugin.Plugin): ...@@ -31,6 +31,8 @@ class Silent(plugin.Plugin):
enabled = True enabled = True
def configure(self, app_parser, cmd_parser): def configure(self, app_parser, cmd_parser):
self.parser = app_parser parser = app_parser
self.parser.add_argument('--silent', action='store_true', default=False) parser.add_argument(
'--silent', action='store_true', default=False,
help='Silent output, do not display results.')
self.configured = True self.configured = True
...@@ -229,10 +229,10 @@ class XUnit(plugin.Plugin): ...@@ -229,10 +229,10 @@ class XUnit(plugin.Plugin):
def configure(self, app_parser, cmd_parser): def configure(self, app_parser, cmd_parser):
self.parser = app_parser self.parser = app_parser
app_parser.add_argument('--xunit', type=str, app_parser.add_argument(
dest='xunit_output', '--xunit', type=str, dest='xunit_output',
help=('Enable xUnit output to the file where the result should be written.' help=('Enable xUnit output to the file where the result should be written.'
"Use '-' to redirect to the standard output.")) "Use '-' to redirect to the standard output."))
self.configured = True self.configured = True
def activate(self, app_args): def activate(self, app_args):
......
...@@ -159,7 +159,7 @@ class RunnerOperationTest(unittest.TestCase): ...@@ -159,7 +159,7 @@ class RunnerOperationTest(unittest.TestCase):
cmd_line = './scripts/avocado run' cmd_line = './scripts/avocado run'
result = process.run(cmd_line, ignore_status=True) result = process.run(cmd_line, ignore_status=True)
expected_rc = 2 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.assertEqual(result.exit_status, expected_rc)
self.assertIn(expected_output, result.stderr) self.assertIn(expected_output, result.stderr)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册