MCPcopy Create free account
hub / github.com/python-cmd2/cmd2 / _format_args

Method _format_args

cmd2/rich_utils.py:190–214  ·  view source on GitHub ↗

Override to handle cmd2's custom nargs formatting. All formats in this function need to be handled by _rich_metavar_parts().

(self, action: argparse.Action, default_metavar: str)

Source from the content-addressed store, hash-verified

188 return range_str
189
190 def _format_args(self, action: argparse.Action, default_metavar: str) -> str:
191 """Override to handle cmd2's custom nargs formatting.
192
193 All formats in this function need to be handled by _rich_metavar_parts().
194 """
195 get_metavar = self._metavar_formatter(action, default_metavar)
196
197 # Handle nargs specified as a range
198 nargs_range = action.get_nargs_range() # type: ignore[attr-defined]
199 if nargs_range is not None:
200 arg_str = "%s" % get_metavar(1) # noqa: UP031
201 range_str = self._build_nargs_range_str(nargs_range)
202 return f"{arg_str}{range_str}"
203
204 # When nargs is just a number, argparse repeats the arg in the help text.
205 # For instance, when nargs=5 the help text looks like: 'command arg arg arg arg arg'.
206 # To make this less verbose, format it like: 'command arg{5}'.
207 # Do not customize the output when metavar is a tuple of strings. Allow argparse's
208 # formatter to handle that instead.
209 if not isinstance(action.metavar, tuple) and isinstance(action.nargs, int) and action.nargs > 1:
210 arg_str = "%s" % get_metavar(1) # noqa: UP031
211 return f"{arg_str}{{{action.nargs}}}"
212
213 # Fallback to parent for all other cases
214 return super()._format_args(action, default_metavar)
215
216 def _rich_metavar_parts(
217 self,

Callers

nothing calls this directly

Calls 1

Tested by

no test coverage detected