diff options
author | Doug Hellmann <doug.hellmann@gmail.com> | 2012-04-22 15:03:15 -0700 |
---|---|---|
committer | Doug Hellmann <doug.hellmann@gmail.com> | 2012-04-22 15:03:15 -0700 |
commit | 35f42f9f8bf00a5bcdb2032d1e4535346c782289 (patch) | |
tree | 8bd46097e631585a48c2e9b808ddc9e26d9ec017 | |
parent | 21b06cd2be7d42a04bad1db1428fb3269ad38fa1 (diff) | |
download | cliff-tablib-35f42f9f8bf00a5bcdb2032d1e4535346c782289.tar.gz |
tweak App api to make it easier to override and perform global actions before and after a command runs
-rw-r--r-- | cliff/app.py | 42 | ||||
-rw-r--r-- | demoapp/cliffdemo/main.py | 10 | ||||
-rw-r--r-- | demoapp/cliffdemo/simple.py | 3 |
3 files changed, 44 insertions, 11 deletions
diff --git a/cliff/app.py b/cliff/app.py index a285c92..ab55a10 100644 --- a/cliff/app.py +++ b/cliff/app.py @@ -22,41 +22,48 @@ class App(object): def __init__(self, description, version, command_manager): self.command_manager = command_manager - self._build_parser(description, version) + self.parser = self.build_option_parser(description, version) - def _build_parser(self, description, version): - self.parser = optparse.OptionParser( + def build_option_parser(self, description, version): + """Return an optparse option parser for this application. + + Subclasses may override this method to extend + the parser with more global options. + """ + parser = optparse.OptionParser( description=description, version='%prog {}'.format(version), add_help_option=False, ) - self.parser.disable_interspersed_args() - self.parser.add_option( + parser.disable_interspersed_args() + parser.add_option( '-v', '--verbose', action='count', dest='verbose_level', default=self.DEFAULT_VERBOSE_LEVEL, help='Increase verbosity of output. Can be repeated.', ) - self.parser.add_option( + parser.add_option( '-q', '--quiet', action='store_const', dest='verbose_level', const=0, help='suppress output except warnings and errors', ) - self.parser.add_option( + parser.add_option( '-h', action='help', help="show this help message and exit", ) - self.parser.add_option( + parser.add_option( '--help', action='callback', callback=self.show_verbose_help, help="show verbose help message and exit", ) - return + return parser def show_verbose_help(self, *args): + """Displays the normal syntax info and a list of available subcommands. + """ self.parser.print_help() print('') print('Commands:') @@ -94,13 +101,28 @@ class App(object): root_logger.addHandler(console) return + def prepare_to_run_command(self, cmd): + """Perform any preliminary work needed to run a command. + """ + return + + def clean_up(self, cmd, result): + """Hook run after a command is done to shutdown the app. + """ + return + def run(self, argv): + """Equivalent to the main program for the application. + """ if not argv: argv = ['-h'] self.options, remainder = self.parser.parse_args(argv) self.configure_logging() cmd_factory, cmd_name, sub_argv = self.command_manager.find_command(remainder) cmd = cmd_factory(self, self.options) + self.prepare_to_run_command(cmd) cmd_parser = cmd.get_parser(' '.join([self.NAME, cmd_name])) parsed_args = cmd_parser.parse_args(sub_argv) - return cmd.run(parsed_args) + result = cmd.run(parsed_args) + self.clean_up(cmd, result) + return result diff --git a/demoapp/cliffdemo/main.py b/demoapp/cliffdemo/main.py index 0a5b728..cebf552 100644 --- a/demoapp/cliffdemo/main.py +++ b/demoapp/cliffdemo/main.py @@ -1,3 +1,4 @@ +import logging import sys from cliff.app import App @@ -5,6 +6,9 @@ from cliff.commandmanager import CommandManager class DemoApp(App): + + log = logging.getLogger(__name__) + def __init__(self): super(DemoApp, self).__init__( description='cliff demo app', @@ -12,6 +16,12 @@ class DemoApp(App): command_manager=CommandManager('cliff.demo'), ) + def prepare_to_run_command(self, cmd): + self.log.debug('prepare_to_run_command %s', cmd.__class__.__name__) + + def clean_up(self, cmd, result): + self.log.debug('clean_up %s', cmd.__class__.__name__) + def main(argv=sys.argv[1:]): myapp = DemoApp() diff --git a/demoapp/cliffdemo/simple.py b/demoapp/cliffdemo/simple.py index f4a16f8..2f273b9 100644 --- a/demoapp/cliffdemo/simple.py +++ b/demoapp/cliffdemo/simple.py @@ -9,5 +9,6 @@ class Simple(Command): log = logging.getLogger(__name__) def run(self, parsed_args): + self.log.info('sending greeting') self.log.debug('debugging') - self.log.info('hi!') + print('hi!') |