示例#1
0
        def atswitch(t, o):
            # check if we are at a recognized switch.  if not
            # assume the data is part of regular arguments.
            if not text.startswith('-', o):
                return None, o

            name, x = s_grammar.meh(t, o, s_grammar.whites)
            swit = switches.get(name)
            if swit is None:
                return None, o

            return swit, x
示例#2
0
    def getCmdOpts(self, text):
        '''
        Use the _cmd_syntax def to split/parse/normalize the cmd line.

        Args:
            text (str): Command to process.

        Notes:
            This is implemented independent of argparse (et al) due to the
            need for syntax aware argument splitting. Also, allows different
            split per command type

        Returns:
            dict: An opts dictionary.
        '''
        off = 0

        _, off = s_grammar.nom(text, off, s_grammar.whites)

        name, off = s_grammar.meh(text, off, s_grammar.whites)

        _, off = s_grammar.nom(text, off, s_grammar.whites)

        opts = {}

        args = collections.deque(
            [synt for synt in self._cmd_syntax if not synt[0].startswith('-')])
        switches = {
            synt[0]: synt
            for synt in self._cmd_syntax if synt[0].startswith('-')
        }

        # populate defaults and lists
        for synt in self._cmd_syntax:
            snam = synt[0].strip('-')

            defval = synt[1].get('defval')
            if defval is not None:
                opts[snam] = defval

            if synt[1].get('type') == 'list':
                opts[snam] = []

        def atswitch(t, o):
            # check if we are at a recognized switch.  if not
            # assume the data is part of regular arguments.
            if not text.startswith('-', o):
                return None, o

            name, x = s_grammar.meh(t, o, s_grammar.whites)
            swit = switches.get(name)
            if swit is None:
                return None, o

            return swit, x

        while off < len(text):

            _, off = s_grammar.nom(text, off, s_grammar.whites)

            swit, off = atswitch(text, off)
            if swit is not None:

                styp = swit[1].get('type', 'flag')
                snam = swit[0].strip('-')

                if styp == 'valu':
                    valu, off = s_parser.parse_cmd_string(text, off)
                    opts[snam] = valu

                elif styp == 'list':
                    valu, off = s_parser.parse_cmd_string(text, off)
                    if not isinstance(valu, list):
                        valu = valu.split(',')
                    opts[snam].extend(valu)

                elif styp == 'enum':
                    vals = swit[1].get('enum:vals')
                    valu, off = s_parser.parse_cmd_string(text, off)
                    if valu not in vals:
                        raise s_exc.BadSyntax(mesg='%s (%s)' %
                                              (swit[0], '|'.join(vals)),
                                              text=text)

                    opts[snam] = valu

                else:
                    opts[snam] = True

                continue

            if not args:
                raise s_exc.BadSyntax(mesg='trailing text: [%s]' %
                                      (text[off:], ),
                                      text=text)

            synt = args.popleft()
            styp = synt[1].get('type', 'valu')

            # a glob type eats the remainder of the string
            if styp == 'glob':
                opts[synt[0]] = text[off:]
                break

            # eat the remainder of the string as separate vals
            if styp == 'list':
                valu = []

                while off < len(text):
                    item, off = s_parser.parse_cmd_string(text, off)
                    valu.append(item)

                opts[synt[0]] = valu
                break

            valu, off = s_parser.parse_cmd_string(text, off)
            opts[synt[0]] = valu

        return opts