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.
:param category: the name of the category in which this command should be grouped when displaying the list of commands.
Example:
class MyApp(cmd2.Cmd):
@cmd2.with_category('Text Functions')
def do_echo(self, args)
self.poutput(args)
For an alternative approach to categorizing commands using a function, see cmd2.utils.categorize
| PARAMETER | DESCRIPTION |
|---|---|
category
|
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.
:param func_arg: Single-element positional argument list containing doi_* method
this decorator is wrapping
:param preserve_quotes: if True, then argument quotes will not be stripped
:return: function that gets passed a list of argument strings
Example:
class MyApp(cmd2.Cmd):
@cmd2.with_argument_list
def do_echo(self, arglist):
self.poutput(' '.join(arglist)
| PARAMETER | DESCRIPTION |
|---|---|
func_arg
|
TYPE:
|
preserve_quotes
|
TYPE:
|
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.
:param parser: unique instance of ArgumentParser or a callable that returns an ArgumentParser
:param 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.
:param preserve_quotes: if True, then arguments passed to argparse maintain their quotes
:param with_unknown_args: if true, then capture unknown args
:return: function that gets passed argparse-parsed args in a Namespace
A cmd2.argparse_custom.Cmd2AttributeWrapper called cmd2_statement is included
in the Namespace to provide access to the cmd2.Statement object that was created when
parsing the command line. This can be useful if the command function needs to know the command line.
Example:
parser = cmd2.Cmd2ArgumentParser()
parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
parser.add_argument('-r', '--repeat', type=int, help='output [n] times')
parser.add_argument('words', nargs='+', help='words to print')
class MyApp(cmd2.Cmd):
@cmd2.with_argparser(parser, preserve_quotes=True)
def do_argprint(self, args):
"Print the options and argument list this options command was called with."
self.poutput(f'args: {args!r}')
Example with unknown args:
parser = cmd2.Cmd2ArgumentParser()
parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
parser.add_argument('-r', '--repeat', type=int, help='output [n] times')
class MyApp(cmd2.Cmd):
@cmd2.with_argparser(parser, with_unknown_args=True)
def do_argprint(self, args, unknown):
"Print the options and argument list this options command was called with."
self.poutput(f'args: {args!r}')
self.poutput(f'unknowns: {unknown}')
| PARAMETER | DESCRIPTION |
|---|---|
parser
|
TYPE:
|
ns_provider
|
TYPE:
|
preserve_quotes
|
TYPE:
|
with_unknown_args
|
TYPE:
|
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.
:param command: Command Name. Space-delimited subcommands may optionally be specified :param subcommand: Subcommand name :param parser: argparse Parser for this subcommand :param 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(). :param aliases: Alternative names for this subcommand. This is passed as the alias argument to subparsers.add_parser(). :return: Wrapper function that can receive an argparse.Namespace
| PARAMETER | DESCRIPTION |
|---|---|
command
|
TYPE:
|
subcommand
|
TYPE:
|
parser
|
TYPE:
|
help
|
TYPE:
|
aliases
|
TYPE:
|