Revision efbcdc41 kamaki/cli/command_shell.py

b/kamaki/cli/command_shell.py
35 35
from os import popen
36 36
from sys import stdout
37 37
from argparse import ArgumentParser
38
from re import compile as regex_compile
39 38

  
40 39
from kamaki.cli import _exec_cmd, _print_error_message
41 40
from kamaki.cli.argument import update_arguments
42
from kamaki.cli.utils import print_dict
41
from kamaki.cli.utils import print_dict, split_input
43 42
from kamaki.cli.history import History
44 43
from kamaki.cli.errors import CLIError
45 44

  
46 45

  
47
def _parse_with_regex(line, regex):
48
    re_parser = regex_compile(regex)
49
    return (re_parser.split(line), re_parser.findall(line))
50

  
51

  
52
def _split_input(line):
53
    """Use regular expressions to split a line correctly
54
    """
55
    line = ' %s ' % line
56
    (trivial_parts, interesting_parts) = _parse_with_regex(line, ' \'.*?\' ')
57
    terms = []
58
    for i, ipart in enumerate(interesting_parts):
59
        tpart = trivial_parts[i]
60
        (sub_trivials, sub_interesting) = _parse_with_regex(tpart, ' ".*?" ')
61
        for subi, subipart in enumerate(sub_interesting):
62
            terms += sub_trivials[subi].split()
63
            terms.append(subipart[2:-2])
64
        terms += sub_trivials[-1].split()
65
        terms.append(ipart[2:-2])
66
    tpart = trivial_parts[-1]
67
    (sub_trivials, sub_interesting) = _parse_with_regex(tpart, ' ".*?" ')
68
    for subi, subipart in enumerate(sub_interesting):
69
        terms += sub_trivials[subi].split()
70
        terms.append(subipart[2:-2])
71
    terms += sub_trivials[-1].split()
72
    return terms
73

  
74

  
75 46
def _init_shell(exe_string, arguments):
76 47
    arguments.pop('version', None)
77 48
    arguments.pop('options', None)
......
180 151
                <cmd> <term> <term> <args> is always parsed to most specific
181 152
                even if cmd_term_term is not a terminal path
182 153
            """
183
            subcmd, cmd_args = cmd.parse_out(_split_input(line))
154
            subcmd, cmd_args = cmd.parse_out(split_input(line))
184 155
            if self._history:
185 156
                self._history.add(' '.join([cmd.path.replace('_', ' '), line]))
186 157
            cmd_parser = ArgumentParser(cmd.name, add_help=False)
......
231 202
        self._register_method(help_method, 'help_%s' % cmd.name)
232 203

  
233 204
        def complete_method(self, text, line, begidx, endidx):
234
            subcmd, cmd_args = cmd.parse_out(_split_input(line)[1:])
205
            subcmd, cmd_args = cmd.parse_out(split_input(line)[1:])
235 206
            if subcmd.is_command:
236 207
                cls = subcmd.get_class()
237 208
                instance = cls(dict(arguments))

Also available in: Unified diff