Revision 017d37ce kamaki/cli/__init__.py
b/kamaki/cli/__init__.py | ||
---|---|---|
53 | 53 |
from ordereddict import OrderedDict |
54 | 54 |
|
55 | 55 |
#from kamaki import clients |
56 |
from .errors import CLIError |
|
56 |
from .errors import CLIError, CLISyntaxError, CLICmdIncompleteError
|
|
57 | 57 |
from .config import Config #TO BE REMOVED |
58 |
from .utils import magenta, red, yellow, CommandTree
|
|
58 |
from .utils import bold, magenta, red, yellow, CommandTree, print_list
|
|
59 | 59 |
from argument import _arguments, parse_known_args |
60 | 60 |
|
61 | 61 |
_commands = CommandTree() |
62 | 62 |
|
63 |
GROUPS={} |
|
64 |
CLI_LOCATIONS = ['kamaki.cli.commands', 'kamaki.commands', 'kamaki.cli', 'kamaki', ''] |
|
63 |
#basic command groups |
|
65 | 64 |
|
66 | 65 |
def command(): |
67 | 66 |
"""Class decorator that registers a class as a CLI command""" |
... | ... | |
112 | 111 |
else: |
113 | 112 |
|
114 | 113 |
|
114 |
def _expand_cmd(cmd_prefix, unparsed): |
|
115 |
if len(unparsed) == 0: |
|
116 |
return None |
|
117 |
prefix = (cmd_prefix+'_') if len(cmd_prefix) > 0 else '' |
|
118 |
for term in _commands.list(cmd_prefix): |
|
119 |
try: |
|
120 |
unparsed.remove(term) |
|
121 |
except ValueError: |
|
122 |
continue |
|
123 |
return prefix+term |
|
124 |
return None |
|
125 |
|
|
126 |
def _retrieve_cmd(unparsed): |
|
127 |
cmd_str = None |
|
128 |
cur_cmd = _expand_cmd('', unparsed) |
|
129 |
while cur_cmd is not None: |
|
130 |
cmd_str = cur_cmd |
|
131 |
cur_cmd = _expand_cmd(cur_cmd, unparsed) |
|
132 |
if cmd_str is None: |
|
133 |
print(bold('Command groups:')) |
|
134 |
print_list(_commands.get_groups(), ident=14) |
|
135 |
|
|
136 |
return None |
|
137 |
try: |
|
138 |
return _commands.get_class(cmd_str) |
|
139 |
except CLICmdIncompleteError: |
|
140 |
print(bold('%s:'%cmd_str)) |
|
141 |
print_list(_commands.list(cmd_str)) |
|
142 |
return None |
|
143 |
|
|
115 | 144 |
def one_command(): |
116 | 145 |
try: |
117 | 146 |
exe = basename(argv[0]) |
... | ... | |
119 | 148 |
parsed, unparsed = parse_known_args(parser) |
120 | 149 |
if _arguments['version'].value: |
121 | 150 |
exit(0) |
151 |
_commands.set_groups(_arguments['config'].get_groups()) |
|
152 |
cmd = _retrieve_cmd(unparsed) |
|
153 |
|
|
154 |
if cmd is None: |
|
155 |
parser.print_help() |
|
156 |
exit(0) |
|
157 |
|
|
122 | 158 |
except CLIError as err: |
123 | 159 |
_print_error_message(err) |
124 | 160 |
exit(1) |
Also available in: Unified diff