File: exapp

package info (click to toggle)
knack 0.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 696 kB
  • sloc: python: 6,261; sh: 8; makefile: 4
file content (87 lines) | stat: -rwxr-xr-x 2,416 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python

""" User registers commands with CommandGroups """

import os
import sys
from collections import OrderedDict

from knack import CLI
from knack.commands import CLICommandsLoader, CommandGroup
from knack.arguments import ArgumentsContext
from knack.help import CLIHelp

from knack.help_files import helps

cli_name = os.path.basename(__file__)

helps['abc'] = """
type: group
short-summary: Manage the alphabet of words.
"""

helps['abc list'] = """
type: command
short-summary: List the alphabet.
examples:
    - name: It's pretty straightforward.
      text: {cli_name} abc list
""".format(cli_name=cli_name)


def a_test_command_handler():
    return [{'a': 1, 'b': 1234}, {'a': 3, 'b': 4}]


def abc_list_command_handler():
    import string
    return list(string.ascii_lowercase)


def hello_command_handler(myarg=None, abc=None):
    return ['hello', 'world', myarg, abc]

WELCOME_MESSAGE = r"""
   _____ _      _____ 
  / ____| |    |_   _|
 | |    | |      | |  
 | |    | |      | |  
 | |____| |____ _| |_ 
  \_____|______|_____|
                      
                      
Welcome to the cool new CLI!
"""


class MyCLIHelp(CLIHelp):

    def __init__(self, cli_ctx=None):
        super(MyCLIHelp, self).__init__(cli_ctx=cli_ctx,
                                        privacy_statement='My privacy statement.',
                                        welcome_message=WELCOME_MESSAGE)


class MyCommandsLoader(CLICommandsLoader):

    def load_command_table(self, args):
        with CommandGroup(self, 'hello', '__main__#{}') as g:
            g.command('world', 'hello_command_handler', confirmation=True)
        with CommandGroup(self, 'abc', '__main__#{}') as g:
                g.command('list', 'abc_list_command_handler')
                g.command('show', 'a_test_command_handler')
        return super(MyCommandsLoader, self).load_command_table(args)

    def load_arguments(self, command):
        with ArgumentsContext(self, 'hello world') as ac:
            ac.argument('myarg', type=int, default=100)
        super(MyCommandsLoader, self).load_arguments(command)


mycli = CLI(cli_name=cli_name,
            config_dir=os.path.expanduser(os.path.join('~', '.{}'.format(cli_name))),
            config_env_var_prefix=cli_name,
            commands_loader_cls=MyCommandsLoader,
            help_cls=MyCLIHelp)
exit_code = mycli.invoke(sys.argv[1:])
sys.exit(exit_code)