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=type['cmd2.Cmd'] | type[CommandSet])
RawCommandFuncOptionalBoolReturn
module-attribute
RawCommandFuncOptionalBoolReturn = Callable[[CommandParent, Statement | str], bool | None]
ArgListCommandFuncOptionalBoolReturn
module-attribute
ArgListCommandFuncOptionalBoolReturn = Callable[[CommandParent, list[str]], bool | None]
ArgListCommandFuncBoolReturn
module-attribute
ArgListCommandFuncBoolReturn = Callable[[CommandParent, list[str]], bool]
ArgListCommandFuncNoneReturn
module-attribute
ArgListCommandFuncNoneReturn = Callable[[CommandParent, list[str]], None]
ArgListCommandFunc
module-attribute
ArgListCommandFunc = ArgListCommandFuncOptionalBoolReturn[CommandParent] | ArgListCommandFuncBoolReturn[CommandParent] | ArgListCommandFuncNoneReturn[CommandParent]
ArgparseCommandFuncOptionalBoolReturn
module-attribute
ArgparseCommandFuncOptionalBoolReturn = Callable[[CommandParent, Namespace], bool | None]
ArgparseCommandFuncWithUnknownArgsOptionalBoolReturn
module-attribute
ArgparseCommandFuncWithUnknownArgsOptionalBoolReturn = Callable[[CommandParent, Namespace, list[str]], bool | None]
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 = ArgparseCommandFuncOptionalBoolReturn[CommandParent] | ArgparseCommandFuncWithUnknownArgsOptionalBoolReturn[CommandParent] | ArgparseCommandFuncBoolReturn[CommandParent] | ArgparseCommandFuncWithUnknownArgsBoolReturn[CommandParent] | ArgparseCommandFuncNoneReturn[CommandParent] | ArgparseCommandFuncWithUnknownArgsNoneReturn[CommandParent]
with_category
Decorate a do_* command method to apply a category.
| 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
Decorate a do_* method to alter the arguments passed to it so it is passed a list[str].
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 |
|---|---|
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
Decorate a do_* method to populate its args argument with the given instance of argparse.ArgumentParser.
| PARAMETER | DESCRIPTION |
|---|---|
parser
|
instance of ArgumentParser or a callable that returns an ArgumentParser for this command
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
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 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 | |
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
|
instance of ArgumentParser or a callable that returns an ArgumentParser 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 |