src/runegraft/shell.py) wraps prompt_toolkit to provide completion, validation, aliases, and history. This reference documents the available classes so you can extend or embed the shell in other programs.
ShellConfig
history_path: file storing the persistent command history (~/.runegraft/history/<prog>.txtby default). The parent directory is created automatically.prompt: string displayed before each line (e.g.,runegraft>).
CLI.shell() constructs this config automatically, but you can pass a custom one if you instantiate Shell yourself.
Shell
- Builds a nested fuzzy completer from registered commands and shell built-ins (
help,history,alias, etc.). - Validates input before execution by calling
CLI.parse_for_invoke. - Dispatches reserved words (
exit,help,history,alias,clear,!system escape). - Maintains an
AliasStorefor simple token substitutions.
Shell.run() prints a header, then loops forever until the user exits via Ctrl+D, Ctrl+C, exit, or quit. It returns 0, and any errors during completion/validation are displayed using the CLI’s Rich console.
Customizing behavior
SubclassShell when you need to adjust bindings or completions:
- Override
_validate_lineto change how input is pre-validated. - Patch the key bindings (e.g., add custom shortcuts) by overriding
run()and modifying theKeyBindingsobject before callingPromptSession.
Alias helpers
runegraft.builtins exposes utilities used by the shell:
AliasStore: simple dataclass containing a mapping of alias names to replacement text.expand_alias(store, line): replaces the first token when it matches an alias.clear_screen(): cross-platform clear helper triggered byCtrl+Lor theclearcommand.
Built-in commands
The shell reserves several names and prevents you from registering conflicting commands:help,?exit,quit,qhistoryclearalias!(system escape)
CLI.command raises CLIError. Use aliases if you need shortcuts that mimic these behaviors.
Error handling
All exceptions raised by command functions or parsing are caught and printed usingcli.ui.console.print("[red]Error:[/red] …"). When you throw CLIError from custom validation code, the message surfaces directly in the shell and in non-interactive mode, so keep them concise and user-friendly.