Esempio n. 1
0
    def __init__(self, parameters):
        params = self.parameters = util.OrderedDict()
        pos = self.positional = []
        named = self.named = []
        alt = self.alternate = []
        aliases = self.aliases = {}
        required = self.required = set()
        optional = self.optional = set()
        for param in _develop_extras(parameters):
            required_ = getattr(param, 'required', False)
            aliases_ = getattr(param, 'aliases', None)

            if required_:
                required.add(param)
            else:
                optional.add(param)

            if aliases_ is not None:
                for alias in aliases_:
                    existing = aliases.get(alias)
                    if existing is not None:
                        raise ValueError(
                            "Parameters {0.display_name} and {1.display_name} "
                            "use a duplicate alias {2!r}.".format(
                                existing, param, alias))
                    aliases[alias] = param

            if param.is_alternate_action:
                alt.append(param)
            elif aliases_ is not None:
                named.append(param)
            else:
                pos.append(param)
            params[getattr(param, 'argument_name', param.display_name)] = param
Esempio n. 2
0
 def prepare(self):
     super(ClizeHelp, self).prepare()
     s = self.sections = util.OrderedDict((
         (LABEL_POS, util.OrderedDict()),
         (LABEL_OPT, util.OrderedDict()),
         (LABEL_ALT, util.OrderedDict()),
     ))
     self.after = {}
     for p in filter_undocumented(self.signature.positional):
         s[LABEL_POS][pname(p)] = p, ''
     for p in sorted(filter_undocumented(self.signature.named), key=pname):
         s[LABEL_OPT][pname(p)] = p, ''
     for p in sorted(filter_undocumented(self.signature.alternate),
                     key=pname):
         s[LABEL_ALT][pname(p)] = p, ''
     self._parse_help()
     s[LABEL_ALT] = s.pop(LABEL_ALT)
Esempio n. 3
0
def cli_commands(obj, namef, clizer):
    cmds = util.OrderedDict()
    cmd_by_name = {}
    try:
        names = util.dict_from_names(obj).items()
    except AttributeError:
        raise ValueError("Cannot guess name for anonymous objects "
                         "(lists, dicts, etc)")
    for key, val in names:
        if not key:
            continue
        names = tuple(namef(name) for name in util.maybe_iter(key))
        cli = clizer.get_cli(val)
        cmds[names] = cli
        for name in names:
            cmd_by_name[name] = cli
    return cmds, cmd_by_name
Esempio n. 4
0
 def parse_docstring(self, s):
     free_text = []
     header = []
     label = None
     last_argname = None
     for p in split_docstring(s):
         argdoc = self.argdoc_re.match(p)
         if argdoc:
             argname, text = argdoc.groups()
             if free_text:
                 if free_text[-1].endswith(':'):
                     label = free_text.pop()
                 if last_argname:
                     self.after[last_argname] = free_text
                 else:
                     header.extend(free_text)
                 free_text = []
             last_argname = argname
             try:
                 default_label = self.get_param_type(
                     self.signature.parameters[argname])
             except KeyError:
                 continue
             if default_label != LABEL_POS:
                 try:
                     param, _ = self.sections[default_label].pop(argname)
                 except KeyError:
                     continue
                 label_ = label or default_label
                 if label_ not in self.sections:
                     self.sections[label_] = util.OrderedDict()
             else:
                 try:
                     param, _ = self.sections[default_label][argname]
                 except KeyError:
                     continue
                 label_ = default_label
             self.sections[label_][argname] = param, text
         else:
             free_text.append(p)
     if not last_argname:
         header = free_text
         footer = []
     else:
         footer = free_text
     return lines_to_paragraphs(header), lines_to_paragraphs(footer)