cmd2.decorators
cmd2.decorators
Decorators for cmd2
commands
CommandParent
module-attribute
CommandParent = TypeVar('CommandParent', bound=Union['cmd2.Cmd', CommandSet])
CommandParentType
module-attribute
CommandParentType = TypeVar('CommandParentType', bound=Union[Type['cmd2.Cmd'], Type[CommandSet]])
RawCommandFuncOptionalBoolReturn
module-attribute
RawCommandFuncOptionalBoolReturn = Callable[[CommandParent, Union[Statement, str]], Optional[bool]]
ArgListCommandFuncOptionalBoolReturn
module-attribute
ArgListCommandFuncOptionalBoolReturn = Callable[[CommandParent, List[str]], Optional[bool]]
ArgListCommandFuncBoolReturn
module-attribute
ArgListCommandFuncBoolReturn = Callable[[CommandParent, List[str]], bool]
ArgListCommandFuncNoneReturn
module-attribute
ArgListCommandFuncNoneReturn = Callable[[CommandParent, List[str]], None]
ArgListCommandFunc
module-attribute
ArgListCommandFunc = Union[ArgListCommandFuncOptionalBoolReturn[CommandParent], ArgListCommandFuncBoolReturn[CommandParent], ArgListCommandFuncNoneReturn[CommandParent]]
ArgparseCommandFuncOptionalBoolReturn
module-attribute
ArgparseCommandFuncOptionalBoolReturn = Callable[[CommandParent, Namespace], Optional[bool]]
ArgparseCommandFuncWithUnknownArgsOptionalBoolReturn
module-attribute
ArgparseCommandFuncWithUnknownArgsOptionalBoolReturn = Callable[[CommandParent, Namespace, List[str]], Optional[bool]]
ArgparseCommandFuncBoolReturn
module-attribute
ArgparseCommandFuncBoolReturn = Callable[[CommandParent, Namespace], bool]
ArgparseCommandFuncWithUnknownArgsBoolReturn
module-attribute
ArgparseCommandFuncWithUnknownArgsBoolReturn = Callable[[CommandParent, Namespace, List[str]], bool]
ArgparseCommandFuncNoneReturn
module-attribute
ArgparseCommandFuncNoneReturn = Callable[[CommandParent, Namespace], None]
ArgparseCommandFuncWithUnknownArgsNoneReturn
module-attribute
ArgparseCommandFuncWithUnknownArgsNoneReturn = Callable[[CommandParent, Namespace, List[str]], None]
ArgparseCommandFunc
module-attribute
ArgparseCommandFunc = Union[ArgparseCommandFuncOptionalBoolReturn[CommandParent], ArgparseCommandFuncWithUnknownArgsOptionalBoolReturn[CommandParent], ArgparseCommandFuncBoolReturn[CommandParent], ArgparseCommandFuncWithUnknownArgsBoolReturn[CommandParent], ArgparseCommandFuncNoneReturn[CommandParent], ArgparseCommandFuncWithUnknownArgsNoneReturn[CommandParent]]
with_category
A decorator to apply a category to a do_*
command method.
PARAMETER | DESCRIPTION |
---|---|
category
|
the name of the category in which this command should be grouped when displaying the list of commands. Example:
TYPE:
|
Source code in cmd2/decorators.py
with_argument_list
A decorator to alter the arguments passed to a do_*
method. Default
passes a string of whatever the user typed. With this decorator, the
decorated method will receive a list of arguments parsed from user input.
PARAMETER | DESCRIPTION |
---|---|
func_arg
|
Single-element positional argument list containing
TYPE:
|
preserve_quotes
|
if
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[RawCommandFuncOptionalBoolReturn[CommandParent], Callable[[ArgListCommandFunc[CommandParent]], RawCommandFuncOptionalBoolReturn[CommandParent]]]
|
function that gets passed a list of argument strings Example: |
Source code in cmd2/decorators.py
with_argparser
A decorator to alter a cmd2 method to populate its args
argument by parsing arguments
with the given instance of argparse.ArgumentParser.
PARAMETER | DESCRIPTION |
---|---|
parser
|
unique instance of ArgumentParser or a callable that returns an ArgumentParser
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[CommandParent]], RawCommandFuncOptionalBoolReturn[CommandParent]]
|
function that gets passed argparse-parsed args in a |
Source code in cmd2/decorators.py
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 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 |
|
as_subcommand_to
Tag this method as a subcommand to an existing argparse decorated command.
PARAMETER | DESCRIPTION |
---|---|
command
|
Command Name. Space-delimited subcommands may optionally be specified
TYPE:
|
subcommand
|
Subcommand name
TYPE:
|
parser
|
argparse Parser for this subcommand
TYPE:
|
help
|
Help message for this subcommand which displays in the list of subcommands of the command we are adding to. This is passed as the help argument to subparsers.add_parser().
TYPE:
|
aliases
|
Alternative names for this subcommand. This is passed as the alias argument to subparsers.add_parser().
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Callable[[ArgparseCommandFunc[CommandParent]], ArgparseCommandFunc[CommandParent]]
|
Wrapper function that can receive an argparse.Namespace |