Beispiel #1
0
    def get_short_option_arg(self, current, token, arg_token, rest):
        prepended = False

        opt_2_ins = {}
        # TODO: Better solution, maybe cache one
        for options in self.titled_opt_to_ins.values():
            opt_2_ins.update(options)

        if current in opt_2_ins:
            ins = opt_2_ins[current][0]
            # In Options it requires no argument
            if ins.ref is None:
                # sth stacked with it
                if rest:
                    if Atom.get_class(rest)[0] is Argument:
                        raise DocpieError(
                            ('%s announced difference in '
                             'Options(%s) and Usage(%s)') %
                            (current, ins, current))
                    if not (self.stdopt and self.attachopt):
                        raise DocpieError(
                            ("You can't write %s while it requires "
                             "argument and attachopt=False") % current)

                    token.insert(0, '-' + rest)
                    prepended = True
            # In Options it requires argument
            else:
                if rest:
                    arg_token = Token([rest])
                elif arg_token:
                    pass
                else:
                    _current = token.current()
                    if _current in '([':
                        tk = [token.next()]
                        tk.extend(token.till_end_bracket(tk[0]))
                        tk.append(')' if tk[0] == '(' else ']')
                        arg_token = Token(tk)
                    elif _current in ('...', '|'):
                        raise DocpieError(
                            ('%s requires argument in Options(%s) '
                             'but hit "%s" in Usage') %
                            current, ins, _current)
                    else:
                        arg_token = Token([token.next()])

                if token.current() == '...':
                    arg_token.append(token.next())
        elif rest:
            if not (self.stdopt and self.attachopt):
                raise DocpieError(
                    "You can't write %s while it requires argument "
                    "and attachopt=False" % current)
            # -asth -> -a -sth
            token.insert(0, '-' + rest)
            prepended = True

        return arg_token, prepended
Beispiel #2
0
    def get_short_option_arg(self, current, token, arg_token, rest):
        prepended = False

        opt_2_ins = {}
        # TODO: Better solution, maybe cache one
        for options in self.titled_opt_to_ins.values():
            opt_2_ins.update(options)

        if current in opt_2_ins:
            ins = opt_2_ins[current][0]
            # In Options it requires no argument
            if ins.ref is None:
                # sth stacked with it
                if rest:
                    if Atom.get_class(rest)[0] is Argument:
                        raise DocpieError(
                            ('%s announced difference in '
                             'Options(%s) and Usage(%s)') %
                            (current, ins, current))
                    if not (self.stdopt and self.attachopt):
                        raise DocpieError(
                            ("You can't write %s while it requires "
                             "argument and attachopt=False") % current)

                    token.insert(0, '-' + rest)
                    prepended = True
            # In Options it requires argument
            else:
                if rest:
                    arg_token = Token([rest])
                elif arg_token:
                    pass
                else:
                    _current = token.current()
                    if _current in '([':
                        tk = [token.next()]
                        tk.extend(token.till_end_bracket(tk[0]))
                        tk.append(')' if tk[0] == '(' else ']')
                        arg_token = Token(tk)
                    elif _current in ('...', '|'):
                        raise DocpieError(
                            ('%s requires argument in Options(%s) '
                             'but hit "%s" in Usage') %
                            current, ins, _current)
                    else:
                        arg_token = Token([token.next()])

                if token.current() == '...':
                    arg_token.append(token.next())
        elif rest:
            if not (self.stdopt and self.attachopt):
                raise DocpieError(
                    "You can't write %s while it requires argument "
                    "and attachopt=False" % current)
            # -asth -> -a -sth
            token.insert(0, '-' + rest)
            prepended = True

        return arg_token, prepended
Beispiel #3
0
    def parse_other_element(self, current, token):
        atom_class, title = Atom.get_class(current)
        if atom_class is OptionsShortcut:
            return self.parse_options_shortcut(title, token)

        args = set([current])

        if atom_class is Option:
            for options in self.titled_opt_to_ins.values():
                if current in options:
                    ins_in_opt = options[current][0]

                    args.update(ins_in_opt.names)
                    if ins_in_opt.ref is not None:
                        ref_current = token.next()
                        ref_token = Token()
                        if ref_current in '([':
                            ref_token.append(ref_current)
                            ref_token.extend(
                                token.till_end_bracket(ref_current)
                            )
                            ref_token.append(
                                ')' if ref_current == '(' else ']'
                            )
                        else:
                            ref_token.extend(('(', ref_current, ')'))

                        ref_ins = self.parse_pattern(ref_token)

                        logger.debug(ins_in_opt.ref)
                        logger.debug(ref_ins[0])

                        if len(ref_ins) != 1:
                            raise DocpieError(
                                ('%s announced difference in '
                                 'Options(%s) and Usage(%s)') %
                                (current, ins_in_opt, ref_ins))

                        if ins_in_opt.ref != ref_ins[0]:
                            raise DocpieError(
                                ('%s announced difference in '
                                 'Options(%s) and Usage(%s)') %
                                (current, ins_in_opt, ref_ins))

                        ins = atom_class(*args, **{'ref': ins_in_opt.ref})
                        return (ins,)

        ins = atom_class(*args)

        repeat = token.check_ellipsis_and_drop()
        if repeat:
            ins = Required(ins, repeat=True)
        logger.debug('%s -> %s', current, ins)
        return (ins,)
Beispiel #4
0
    def parse_other_element(self, current, token):
        atom_class, title = Atom.get_class(current)
        if atom_class is OptionsShortcut:
            return self.parse_options_shortcut(title, token)

        args = set([current])

        if atom_class is Option:
            for options in self.titled_opt_to_ins.values():
                if current in options:
                    ins_in_opt = options[current][0]

                    args.update(ins_in_opt.names)
                    if ins_in_opt.ref is not None:
                        ref_current = token.next()
                        ref_token = Token()
                        if ref_current in '([':
                            ref_token.append(ref_current)
                            ref_token.extend(token.till_end_bracket(ref_current))
                            ref_token.append(')' if ref_current == '(' else ']')
                        else:
                            ref_token.extend(('(', ref_current, ')'))

                        ref_ins = self.parse_pattern(ref_token)

                        logger.debug(ins_in_opt.ref)
                        logger.debug(ref_ins[0])

                        if len(ref_ins) != 1:
                            raise DocpieError(
                                '%s announced difference in Options(%s) and Usage(%s)' %
                                (current, ins_in_opt, ref_ins))

                        if ins_in_opt.ref != ref_ins[0]:
                            raise DocpieError(
                                '%s announced difference in Options(%s) and Usage(%s)' %
                                (current, ins_in_opt, ref_ins))

                        ins = atom_class(*args, **{'ref': ins_in_opt.ref})
                        return (ins,)

        ins = atom_class(*args)

        repeat = token.check_ellipsis_and_drop()
        if repeat:
            ins = Required(ins, repeat=True)
        logger.debug('%s -> %s', current, ins)
        return (ins,)
Beispiel #5
0
    def get_long_option_with_arg(self, current, token):
        flag, arg = current.split('=', 1)
        if Atom.get_class(flag)[0] is Option:
            if arg:
                arg_token = Token([arg])
            else:
                next_arg = token.next()
                if next_arg not in '([':
                    raise DocpieError('format error: %s' % current)
                tk = [next_arg]
                tk.extend(token.till_end_bracket(next_arg))
                tk.append(')' if next_arg == '(' else ']')
                arg_token = Token(tk)

            if token.current() == '...':
                arg_token.append(token.next())

            return flag, arg_token
Beispiel #6
0
    def get_long_option_with_arg(self, current, token):
        flag, arg = current.split('=', 1)
        if Atom.get_class(flag)[0] is Option:
            if arg:
                arg_token = Token([arg])
            else:
                next_arg = token.next()
                if next_arg not in '([':
                    raise DocpieError('format error: %s' % current)
                tk = [next_arg]
                tk.extend(token.till_end_bracket(next_arg))
                tk.append(')' if next_arg == '(' else ']')
                arg_token = Token(tk)

            if token.current() == '...':
                arg_token.append(token.next())

            return flag, arg_token
Beispiel #7
0
    def get_short_option_with_arg(self, current, token):
        flag = None
        arg_token = None
        rest = None
        prepended = False

        # -a<sth> -abc<sth>
        if current.find('<') >= 0 and current.endswith('>'):
            temp_flag, arg_token, prepended = \
                self.get_short_option_with_angle_bracket(current, token)
        else:
            temp_flag, rest = current[:2], current[2:]

        if Atom.get_class(temp_flag)[0] is Option:
            flag = temp_flag
            arg_token, prepended = \
                self.get_short_option_arg(flag, token, arg_token, rest)

        return flag, arg_token, prepended
Beispiel #8
0
    def get_short_option_with_arg(self, current, token):
        flag = None
        arg_token = None
        rest = None
        prepended = False

        # -a<sth> -abc<sth>
        if current.find('<') >= 0 and current.endswith('>'):
            temp_flag, arg_token, prepended = \
                self.get_short_option_with_angle_bracket(current, token)
        else:
            temp_flag, rest = current[:2], current[2:]

        if Atom.get_class(temp_flag)[0] is Option:
            flag = temp_flag
            arg_token, prepended = \
                self.get_short_option_arg(flag, token, arg_token, rest)

        return flag, arg_token, prepended