MCPcopy
hub / github.com/pallets/click / _parse_decls

Method _parse_decls

src/click/core.py:3106–3165  ·  view source on GitHub ↗
(
        self, decls: cabc.Sequence[str], expose_value: bool
    )

Source from the content-addressed store, hash-verified

3104 return result
3105
3106 def _parse_decls(
3107 self, decls: cabc.Sequence[str], expose_value: bool
3108 ) -> tuple[str, list[str], list[str]]:
3109 opts = []
3110 secondary_opts = []
3111 name = None
3112 possible_names = []
3113
3114 for decl in decls:
3115 if decl.isidentifier():
3116 if name is not None:
3117 raise TypeError(_("Name '{name}' defined twice").format(name=name))
3118 name = decl
3119 else:
3120 split_char = ";" if decl[:1] == "/" else "/"
3121 if split_char in decl:
3122 first, second = decl.split(split_char, 1)
3123 first = first.rstrip()
3124 if first:
3125 possible_names.append(_split_opt(first))
3126 opts.append(first)
3127 second = second.lstrip()
3128 if second:
3129 secondary_opts.append(second.lstrip())
3130 if first == second:
3131 raise ValueError(
3132 _(
3133 "Boolean option {decl!r} cannot use the"
3134 " same flag for true/false."
3135 ).format(decl=decl)
3136 )
3137 else:
3138 possible_names.append(_split_opt(decl))
3139 opts.append(decl)
3140
3141 if name is None and possible_names:
3142 possible_names.sort(key=lambda x: -len(x[0])) # group long options first
3143 name = possible_names[0][1].replace("-", "_").lower()
3144 if not name.isidentifier():
3145 name = None
3146
3147 if name is None:
3148 if not expose_value:
3149 return "", opts, secondary_opts
3150 raise TypeError(
3151 _(
3152 "Could not determine name for option with declarations {decls!r}"
3153 ).format(decls=decls)
3154 )
3155
3156 if not opts and not secondary_opts:
3157 raise TypeError(
3158 _(
3159 "No options defined but a name was passed ({name})."
3160 " Did you mean to declare an argument instead? Did"
3161 " you mean to pass '--{name}'?"
3162 ).format(name=name)
3163 )

Callers

nothing calls this directly

Calls 1

_split_optFunction · 0.85

Tested by

no test coverage detected