-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
78 lines (61 loc) · 2.45 KB
/
cli.py
File metadata and controls
78 lines (61 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""
cli.py
Interactive command-line interface for LoreBuilder.
Handles input parsing, command dispatch, and user context.
Requires app initialization/config prior to calling start_cli().
"""
from rich import print as rprint
import traceback
from commands import COMMANDS_REGISTRY
from help import print_banner, print_command_menu, print_command_help
from cli_utils import cli_error, cli_warn, cli_info, cli_success # centralized messaging
from prompt_toolkit import PromptSession
from prompt_toolkit.history import FileHistory
def start_cli(context, verbose=False):
"""
Start the interactive CLI loop.
Args:
context (dict): Shared state and module references needed for command handlers.
verbose (bool): Enable verbose output (prints tracebacks and debug if True).
"""
print_banner()
print_command_menu()
cli_info("Ready! Enter commands below:")
# Obtain history file path from config
config = context.get('config', {})
history_path = config.get('cli_history_path', '.lorebuilder_cli_history')
session = PromptSession(history=FileHistory(history_path))
while True:
try:
cmd = session.prompt(">> ").strip()
if cmd == "exit":
break
tokens = cmd.split()
if not tokens:
continue
command = tokens[0]
args = tokens[1:] if len(tokens) > 1 else []
# Handle commands with or without leading slash
canonical_command = command.lstrip('/')
# Help command support
if canonical_command == "help":
if not args:
print_command_menu()
else:
cmd_to_help = args[0]
handler = COMMANDS_REGISTRY.get(cmd_to_help)
if handler:
print_command_help(cmd_to_help, handler)
else:
cli_error(f"No help found: unknown command '{cmd_to_help}'.")
continue
# Find the handler in the registry
handler = COMMANDS_REGISTRY.get(canonical_command)
if handler:
handler(args, context)
else:
cli_error(f"Unknown command: {command}. Type a command from the list above.")
except Exception as e:
if verbose:
traceback.print_exc()
cli_error(f"Error processing command: {str(e)}")