diff options
author | Cedric Brandily <zzelle@gmail.com> | 2015-02-23 16:58:33 +0100 |
---|---|---|
committer | Cedric Brandily <zzelle@gmail.com> | 2015-02-24 21:44:08 +0100 |
commit | 9d37798bc123910952a53630ba22f70c1997529d (patch) | |
tree | 41c37e7fd0fa67bb8e2213da98678cb4655cd1a9 | |
parent | 01c8d8f05cb404d9f06745f24c431f83edec54bf (diff) | |
download | cliff-9d37798bc123910952a53630ba22f70c1997529d.tar.gz |
Allow to call initialize_app when running --help
Currently initialize_app is not called when running --help. Some
applications (like openstackclient) define its sub-commands in
initialize_app. So sub-commands are hidden when running '--help' except
if applications hack cliff to defer help print (openstackclient case).
This change defines a new option to allow deferring help print after
initialize_app. Openstackclient will be updated in a follow-up change
in order to remove previous hack.
Change-Id: I0c6fb93d5713654b180b4488e85118c1818fdfb0
Closes-Bug: #1316622
-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 c2c8f50..6fd9a25 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 @@ -144,13 +146,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, @@ -186,6 +196,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. @@ -197,6 +212,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 efff788..b15ef40 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 @@ -376,6 +377,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 |