diff options
author | Jenkins <jenkins@review.openstack.org> | 2015-02-26 17:21:32 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2015-02-26 17:21:32 +0000 |
commit | ed6ab06d2793021d6ab21329b9fad3bb147fbc62 (patch) | |
tree | b9ebf6b4dc09fb923d16ae09b001f6bbf39a65a2 | |
parent | c8b020a1b8df1cb290f405d35d6de1b47f9eb710 (diff) | |
parent | 9d37798bc123910952a53630ba22f70c1997529d (diff) | |
download | cliff-ed6ab06d2793021d6ab21329b9fad3bb147fbc62.tar.gz |
Merge "Allow to call initialize_app when running --help"1.10.0
-rw-r--r-- | cliff/app.py | 32 | ||||
-rw-r--r-- | cliff/tests/test_app.py | 26 |
2 files changed, 49 insertions, 9 deletions
diff --git a/cliff/app.py b/cliff/app.py index 65ee07d..e052571 100644 --- a/cliff/app.py +++ b/cliff/app.py @@ -64,7 +64,8 @@ class App(object): def __init__(self, description, version, command_manager, stdin=None, stdout=None, stderr=None, - interactive_app_factory=InteractiveApp): + interactive_app_factory=InteractiveApp, + deferred_help=False): """Initialize the application. """ self.command_manager = command_manager @@ -72,6 +73,7 @@ class App(object): self.command_manager.add_command('complete', CompleteCommand) self._set_streams(stdin, stdout, stderr) self.interactive_app_factory = interactive_app_factory + self.deferred_help = deferred_help self.parser = self.build_option_parser(description, version) self.interactive_mode = False self.interpreter = None @@ -143,13 +145,21 @@ class App(object): const=0, help='suppress output except warnings and errors', ) - parser.add_argument( - '-h', '--help', - action=HelpAction, - nargs=0, - default=self, # tricky - help="show this help message and exit", - ) + if self.deferred_help: + parser.add_argument( + '-h', '--help', + dest='deferred_help', + action='store_true', + help="show this help message and exit", + ) + else: + parser.add_argument( + '-h', '--help', + action=HelpAction, + nargs=0, + default=self, # tricky + help="show this help message and exit", + ) parser.add_argument( '--debug', default=False, @@ -185,6 +195,11 @@ class App(object): root_logger.addHandler(console) return + def print_help_if_requested(self): + if self.deferred_help and self.options.deferred_help: + action = HelpAction(None, None, default=self) + action(self.parser, self.parser, None, None) + def run(self, argv): """Equivalent to the main program for the application. @@ -196,6 +211,7 @@ class App(object): self.configure_logging() self.interactive_mode = not remainder self.initialize_app(remainder) + self.print_help_if_requested() except Exception as err: if hasattr(self, 'options'): debug = self.options.debug diff --git a/cliff/tests/test_app.py b/cliff/tests/test_app.py index 175f8a7..799e5ee 100644 --- a/cliff/tests/test_app.py +++ b/cliff/tests/test_app.py @@ -15,7 +15,7 @@ from cliff.command import Command from cliff.commandmanager import CommandManager -def make_app(): +def make_app(**kwargs): cmd_mgr = CommandManager('cliff.tests') # Register a command that succeeds @@ -38,6 +38,7 @@ def make_app(): '1', cmd_mgr, stderr=mock.Mock(), # suppress warning messages + **kwargs ) return app, command @@ -377,6 +378,29 @@ def test_error_encoding_sys(): assert data == actual +def _test_help(deferred_help): + app, _ = make_app(deferred_help=deferred_help) + with mock.patch.object(app, 'initialize_app') as init: + with mock.patch('cliff.help.HelpAction.__call__', + side_effect=SystemExit(0)) as helper: + try: + app.run(['--help']) + except SystemExit: + pass + else: + raise Exception('Exception was not thrown') + assert helper.called + assert init.called == deferred_help + + +def test_help(): + _test_help(False) + + +def test_deferred_help(): + _test_help(True) + + def test_unknown_cmd(): app, command = make_app() assert app.run(['hell']) == 2 |