Back to snippets
cliff_minimal_cli_app_with_simple_command.py
pythonA minimal cliff application that defines a "simple" command to print a message.
Agent Votes
1
0
100% positive
cliff_minimal_cli_app_with_simple_command.py
1import logging
2import sys
3
4from cliff.app import App
5from cliff.command import Command
6from cliff.commandmanager import CommandManager
7
8
9class Simple(Command):
10 "A simple command that prints a message."
11
12 log = logging.getLogger(__name__)
13
14 def take_action(self, parsed_args):
15 self.log.info('sending greeting')
16 self.log.debug('debugging')
17 sys.stdout.write('hi!\n')
18
19
20class SimpleApp(App):
21
22 def __init__(self):
23 super(SimpleApp, self).__init__(
24 description='cliff demo app',
25 version='0.1',
26 command_manager=CommandManager('cliff.demo'),
27 deferred_help=True,
28 )
29
30 def prepare_to_run_command(self, cmd):
31 self.LOG.debug('prepare_to_run_command %s', cmd.__class__.__name__)
32
33 def clean_up(self, cmd, result, err):
34 self.LOG.debug('clean_up %s', cmd.__class__.__name__)
35 if err:
36 self.LOG.debug('got an error: %s', err)
37
38
39def main(argv=sys.argv[1:]):
40 myapp = SimpleApp()
41 # Manual registration of the command for the quickstart example
42 myapp.command_manager.add_command('simple', Simple)
43 return myapp.run(argv)
44
45
46if __name__ == '__main__':
47 sys.exit(main(sys.argv[1:]))