base
Class info¶
Classes¶
| Name | Children | Inherits |
|---|---|---|
| BaseCommand slashed.base Abstract base class for commands. |
||
| Command slashed.base Concrete command that can be created directly. |
||
| CommandContext slashed.base Context passed to command handlers. |
||
| CommandResult slashed.base Result from command execution. |
||
| OutputWriter slashed.base Interface for command output. |
||
| ParsedCommand slashed.base Complete parsed command. |
||
| ParsedCommandArgs slashed.base Arguments parsed from a command string. |
🛈 DocStrings¶
Base interfaces for the command system.
BaseCommand
¶
Bases: ABC
Abstract base class for commands.
Source code in src/slashed/base.py
raw_args
class-attribute
instance-attribute
¶
Pass the words after the command name unparsed, as positional arguments.
For commands forwarding an argv (/exec git -C dir status): no --flag value
parsing and no --help interception.
execute
abstractmethod
async
¶
execute(ctx: CommandContext[Any], args: list[str], kwargs: dict[str, str]) -> Any
format_usage
¶
get_callable
¶
get_completer
¶
get_completer() -> CompletionProvider | None
is_available
¶
Check if command is currently available.
Override or use condition predicate to implement dynamic availability.
is_visible
¶
is_visible(ctx: CommandContext[Any]) -> bool
Check if command should be visible in command listings.
Override to implement context-dependent visibility. This is called when listing commands to filter which ones should be shown to the user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
CommandContext[Any]
|
The command context, providing access to context data |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if command should be listed, False to hide it |
Source code in src/slashed/base.py
Command
¶
Bases: BaseCommand
Concrete command that can be created directly.
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
TContext
|
Type of the context data. Defaults to Any for flexibility. Use explicit type parameter for type-safe commands. |
Any
|
Example
Source code in src/slashed/base.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 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 350 351 352 353 | |
__init__
¶
__init__(
execute_func: CommandFunc,
*,
name: str | None = None,
description: str | None = None,
category: str = "general",
usage: str | None = None,
help_text: str | None = None,
completer: CompletionProvider | Callable[[], CompletionProvider] | None = None,
condition: ConditionPredicate | None = None,
visible: VisibilityPredicate[TContext] | None = None,
) -> None
Initialize command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
execute_func
|
CommandFunc
|
Function to execute command |
required |
name
|
str | None
|
Optional command name (defaults to function name) |
None
|
description
|
str | None
|
Command description (defaults to function docstring) |
None
|
category
|
str
|
Command category |
'general'
|
usage
|
str | None
|
Optional usage string (auto-generated if None) |
None
|
help_text
|
str | None
|
Optional help text (defaults to description) |
None
|
completer
|
CompletionProvider | Callable[[], CompletionProvider] | None
|
Optional completion provider or factory |
None
|
condition
|
ConditionPredicate | None
|
Optional predicate to check command availability |
None
|
visible
|
VisibilityPredicate[TContext] | None
|
Optional predicate to check command visibility in listings |
None
|
Source code in src/slashed/base.py
execute
async
¶
execute(
ctx: CommandContext[TContext],
args: list[str] | None = None,
kwargs: dict[str, str] | None = None,
) -> Any
Execute the command using provided function.
Source code in src/slashed/base.py
from_raw
classmethod
¶
from_raw(
func: Callable[[CommandContext[Any], list[str], dict[str, str]], Any | Awaitable[Any]],
*,
name: str | None = None,
description: str | None = None,
category: str = "general",
usage: str | None = None,
help_text: str | None = None,
completer: CompletionProvider | Callable[[], CompletionProvider] | None = None,
condition: ConditionPredicate | None = None,
visible: VisibilityPredicate[Any] | None = None,
) -> Command[Any]
Create a command from a raw-style function.
Use this for functions that expect the traditional (ctx, args, kwargs) signature, such as when wrapping external command sources.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[[CommandContext[Any], list[str], dict[str, str]], Any | Awaitable[Any]]
|
Function with signature (ctx, args: list[str], kwargs: dict[str, str]) |
required |
name
|
str | None
|
Optional command name (defaults to function name) |
None
|
description
|
str | None
|
Command description (defaults to function docstring) |
None
|
category
|
str
|
Command category |
'general'
|
usage
|
str | None
|
Optional usage string |
None
|
help_text
|
str | None
|
Optional help text |
None
|
completer
|
CompletionProvider | Callable[[], CompletionProvider] | None
|
Optional completion provider or factory |
None
|
condition
|
ConditionPredicate | None
|
Optional predicate to check command availability |
None
|
visible
|
VisibilityPredicate[Any] | None
|
Optional predicate to check command visibility |
None
|
Example
Source code in src/slashed/base.py
get_callable
¶
get_completer
¶
get_completer() -> CompletionProvider | None
Get completion provider.
Source code in src/slashed/base.py
is_available
¶
is_visible
¶
is_visible(ctx: CommandContext[TContext]) -> bool
Check if command should be visible based on visibility predicate.
CommandContext
dataclass
¶
Context passed to command handlers.
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
TData
|
Type of the data available to commands. Access via get_data() for type-safe operations. |
required |
Source code in src/slashed/base.py
CommandResult
dataclass
¶
Result from command execution.
This represents the output of a command that can be piped to another command. Separate from ctx.print() which is for user-facing status messages.
Attributes:
| Name | Type | Description |
|---|---|---|
stdout |
str
|
Standard output (data that can be piped) |
stderr |
str
|
Standard error (error messages) |
exit_code |
int
|
Exit code (0 = success, non-zero = failure) |
Source code in src/slashed/base.py
OutputWriter
¶
Bases: Protocol
Interface for command output.
Source code in src/slashed/base.py
ParsedCommand
dataclass
¶
ParsedCommandArgs
dataclass
¶
extract_usage_params
¶
Extract usage parameters from a function's signature.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., Any]
|
The function to extract usage from |
required |
skip_first
|
bool
|
If True, skip the first parameter (for methods with 'self') |
False
|
Source code in src/slashed/base.py
parse_args
¶
parse_args(
func: Callable[..., Any],
ctx: CommandContext[Any],
args: list[str],
kwargs: dict[str, str],
*,
skip_first: bool = False,
) -> tuple[list[Any], dict[str, Any]]
Parse parameters and return positional and keyword arguments with type coercion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., Any]
|
The function to parse arguments for |
required |
ctx
|
CommandContext[Any]
|
The command context |
required |
args
|
list[str]
|
Positional arguments from command line |
required |
kwargs
|
dict[str, str]
|
Keyword arguments from command line |
required |
skip_first
|
bool
|
If True, skip the first parameter (for methods with 'self') |
False
|
Returns:
| Type | Description |
|---|---|
tuple[list[Any], dict[str, Any]]
|
Tuple of (positional_args, keyword_args) with values coerced to annotated types |
Source code in src/slashed/base.py
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 | |
parse_command
¶
parse_command(cmd_str: str) -> ParsedCommand
Parse command string into name and arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cmd_str
|
str
|
Command string without leading slash |
required |
Returns:
| Type | Description |
|---|---|
ParsedCommand
|
Parsed command with name and arguments |
Raises:
| Type | Description |
|---|---|
CommandError
|
If command syntax is invalid |