cmd2.decorators
cmd2.decorators
Decorators for cmd2 commands.
ArgListCommandFunc
module-attribute
ArgparseCommandFunc
module-attribute
ArgparseCommandFunc = (
UnboundCommandFunc[CmdOrSetT, [Namespace]]
| UnboundCommandFunc[CmdOrSetT, [Namespace, list[str]]]
)
with_category
Decorate a do_* command function to apply a category.
Permissive type hints allow this decorator to be stacked in any order, even when other decorators in the chain transform the signature or return type of the command function.
| PARAMETER | DESCRIPTION |
|---|---|
category
|
the name of the category in which this command should be grouped when displaying the list of commands.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Callable[[F], F]
|
a decorator that assigns the specified category to the command function Example: For an alternative approach to categorizing commands using a function, see cmd2.utils.categorize |
Source code in cmd2/decorators.py
with_argument_list
with_argument_list(
cmd_func: ArgListCommandFunc[CmdOrSetT],
*,
preserve_quotes: bool = False,
) -> RawCommandFunc[CmdOrSetT]
with_argument_list(
cmd_func: None = None, *, preserve_quotes: bool = False
) -> Callable[
[ArgListCommandFunc[CmdOrSetT]],
RawCommandFunc[CmdOrSetT],
]
Decorate a do_* command function to receive a list of parsed arguments.
This decorator can be used either directly (@with_argument_list) or as a
factory with arguments (@with_argument_list(preserve_quotes=True)).
| PARAMETER | DESCRIPTION |
|---|---|
cmd_func
|
The command function being decorated.
TYPE:
|
preserve_quotes
|
If
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
RawCommandFunc[CmdOrSetT] | Callable[[ArgListCommandFunc[CmdOrSetT]], RawCommandFunc[CmdOrSetT]]
|
A command function that accepts a list of strings instead of a raw string. Example: |
Source code in cmd2/decorators.py
with_argparser
with_argparser(
parser_source: Cmd2ArgumentParser,
*,
ns_provider: Callable[..., Namespace] | None = None,
preserve_quotes: bool = False,
with_unknown_args: bool = False,
) -> Callable[
[ArgparseCommandFunc[CmdOrSetT]],
RawCommandFunc[CmdOrSetT],
]
with_argparser(
parser_source: NoParamParserFactory,
*,
ns_provider: Callable[..., Namespace] | None = None,
preserve_quotes: bool = False,
with_unknown_args: bool = False,
) -> Callable[
[ArgparseCommandFunc[CmdOrSetT]],
RawCommandFunc[CmdOrSetT],
]
with_argparser(
parser_source: ClassParamParserFactory[CmdOrSetT],
*,
ns_provider: Callable[..., Namespace] | None = None,
preserve_quotes: bool = False,
with_unknown_args: bool = False,
) -> Callable[
[ArgparseCommandFunc[CmdOrSetT]],
RawCommandFunc[CmdOrSetT],
]
with_argparser(
parser_source,
*,
ns_provider=None,
preserve_quotes=False,
with_unknown_args=False,
)
Decorate a do_* command function to populate its args argument with a Cmd2ArgumentParser.
| PARAMETER | DESCRIPTION |
|---|---|
parser_source
|
an existing Cmd2ArgumentParser instance or a factory (callable, staticmethod, or classmethod) that returns one.
TYPE:
|
ns_provider
|
An optional function that accepts a cmd2.Cmd or cmd2.CommandSet object as an argument and returns an argparse.Namespace. This is useful if the Namespace needs to be prepopulated with state data that affects parsing.
TYPE:
|
preserve_quotes
|
if
TYPE:
|
with_unknown_args
|
if true, then capture unknown args
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Callable[[ArgparseCommandFunc[CmdOrSetT]], RawCommandFunc[CmdOrSetT]]
|
function that gets passed argparse-parsed args in a Example: Example with unknown args: |
Source code in cmd2/decorators.py
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 | |
as_subcommand_to
as_subcommand_to(
command: str,
subcommand: str,
parser_source: Cmd2ArgumentParser,
*,
help: str | None = None,
aliases: Sequence[str] = (),
deprecated: bool = False,
) -> Callable[[F], F]
as_subcommand_to(
command: str,
subcommand: str,
parser_source: NoParamParserFactory,
*,
help: str | None = None,
aliases: Sequence[str] = (),
deprecated: bool = False,
) -> Callable[[F], F]
as_subcommand_to(
command: str,
subcommand: str,
parser_source: ClassParamParserFactory[CmdOrSetT],
*,
help: str | None = None,
aliases: Sequence[str] = (),
deprecated: bool = False,
) -> Callable[[F], F]
Tag a function as a subcommand to an existing argparse decorated command.
Permissive type hints allow this decorator to be stacked in any order, even when other decorators in the chain transform the signature or return type of the subcommand function.
While this decorator has permissive type hints, the subcommand function's signature
must match the root command's signature. For example, if the root command uses
with_unknown_args=True, then the subcommand function must also accept the
unknown arguments list.
| PARAMETER | DESCRIPTION |
|---|---|
command
|
Command Name. Space-delimited subcommands may optionally be specified
TYPE:
|
subcommand
|
Subcommand name
TYPE:
|
parser_source
|
an existing Cmd2ArgumentParser instance or a factory (callable, staticmethod, or classmethod) that returns one.
TYPE:
|
help
|
optional help message for this subcommand which displays in the list of subcommands of the command we are adding to.
TYPE:
|
aliases
|
optional alternative names for this subcommand.
TYPE:
|
deprecated
|
whether this subcommand is deprecated (requires Python 3.13+).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Callable[[F], F]
|
a decorator which configures the target function to be a subcommand handler Example: |