MCPcopy Index your code
hub / github.com/pyinvoke/invoke / add_arg

Method add_arg

invoke/parser/context.py:108–155  ·  view source on GitHub ↗

Adds given ``Argument`` (or constructor args for one) to this context. The Argument in question is added to the following dict attributes: * ``args``: "normal" access, i.e. the given names are directly exposed as keys. * ``flags``: "flaglike" access, i.e.

(self, *args: Any, **kwargs: Any)

Source from the content-addressed store, hash-verified

106 return "<parser/Context{}{}>".format(name, args)
107
108 def add_arg(self, *args: Any, **kwargs: Any) -> None:
109 """
110 Adds given ``Argument`` (or constructor args for one) to this context.
111
112 The Argument in question is added to the following dict attributes:
113
114 * ``args``: "normal" access, i.e. the given names are directly exposed
115 as keys.
116 * ``flags``: "flaglike" access, i.e. the given names are translated
117 into CLI flags, e.g. ``"foo"`` is accessible via ``flags['--foo']``.
118 * ``inverse_flags``: similar to ``flags`` but containing only the
119 "inverse" versions of boolean flags which default to True. This
120 allows the parser to track e.g. ``--no-myflag`` and turn it into a
121 False value for the ``myflag`` Argument.
122
123 .. versionadded:: 1.0
124 """
125 # Normalize
126 if len(args) == 1 and isinstance(args[0], Argument):
127 arg = args[0]
128 else:
129 arg = Argument(*args, **kwargs)
130 # Uniqueness constraint: no name collisions
131 for name in arg.names:
132 if name in self.args:
133 msg = "Tried to add an argument named {!r} but one already exists!" # noqa
134 raise ValueError(msg.format(name))
135 # First name used as "main" name for purposes of aliasing
136 main = arg.names[0] # NOT arg.name
137 self.args[main] = arg
138 # Note positionals in distinct, ordered list attribute
139 if arg.positional:
140 self.positional_args.append(arg)
141 # Add names & nicknames to flags, args
142 self.flags[to_flag(main)] = arg
143 for name in arg.nicknames:
144 self.args.alias(name, to=main)
145 self.flags.alias(to_flag(name), to=to_flag(main))
146 # Add attr_name to args, but not flags
147 if arg.attr_name:
148 self.args.alias(arg.attr_name, to=main)
149 # Add to inverse_flags if required
150 if arg.kind == bool and arg.default is True:
151 # Invert the 'main' flag name here, which will be a dashed version
152 # of the primary argument name if underscore-to-dash transformation
153 # occurred.
154 inverse_name = to_flag("no-{}".format(main))
155 self.inverse_flags[inverse_name] = to_flag(main)
156
157 @property
158 def missing_positional_args(self) -> List[Argument]:

Calls 2

ArgumentClass · 0.85
to_flagFunction · 0.85

Tested by

no test coverage detected