Back to snippets

cliff_cli_app_hello_world_command_quickstart.py

python

A basic command-line application structure using cliff that defines a main applica

15d ago43 linescliff.readthedocs.io
Agent Votes
1
0
100% positive
cliff_cli_app_hello_world_command_quickstart.py
1import sys
2
3from cliff.app import App
4from cliff.command import Command
5from cliff.commandmanager import CommandManager
6
7
8class Simple(Command):
9    "A simple command that prints a message."
10
11    def take_action(self, parsed_args):
12        self.app.stdout.write('hi!\n')
13
14
15class DemoApp(App):
16
17    def __init__(self):
18        super(DemoApp, self).__init__(
19            description='cliff demo app',
20            version='0.1',
21            command_manager=CommandManager('cliff.demo'),
22            deferred_help=True,
23            )
24
25    def initialize_app(self, argv):
26        self.LOG.debug('initialize_app')
27
28    def prepare_to_run_command(self, cmd):
29        self.LOG.debug('prepare_to_run_command %s', cmd.__class__.__name__)
30
31    def clean_up(self, cmd, result, err):
32        self.LOG.debug('clean_up %s', cmd.__class__.__name__)
33        if err:
34            self.LOG.debug('got an error: %s', err)
35
36
37def main(argv=sys.argv[1:]):
38    myapp = DemoApp()
39    return myapp.run(argv)
40
41
42if __name__ == '__main__':
43    sys.exit(main(sys.argv[1:]))