File size: 2,466 Bytes
f56729d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""
Command registry and execution for the Folio CLI.

This module provides a central registry for all commands and handles command execution.
"""

from collections.abc import Callable
from typing import Any

# Import command modules
from .help import help_command
from .portfolio import portfolio_command
from .position import position_command
from .simulate import simulate_command

# Command registry
_COMMANDS = {}


def register_command(
    name: str, handler: Callable, help_text: str, subcommands: list[str] | None = None
):
    """Register a command with the command registry.

    Args:
        name: Command name
        handler: Function that handles the command
        help_text: Help text for the command
        subcommands: List of subcommands (if any)
    """
    _COMMANDS[name] = {
        "handler": handler,
        "help": help_text,
        "subcommands": subcommands,
    }


def get_command_registry():
    """Get the command registry.

    Returns:
        Dictionary of registered commands
    """
    return _COMMANDS


def execute_command(command_line: str, state: dict[str, Any], console):
    """Execute a command from the command line.

    Args:
        command_line: Full command line to execute
        state: Application state dictionary
        console: Rich console for output
    """
    # Parse the command line
    parts = command_line.strip().split()
    if not parts:
        return

    command = parts[0].lower()
    args = parts[1:]

    # Check if the command exists
    if command not in _COMMANDS:
        console.print(f"[bold red]Unknown command:[/bold red] {command}")
        console.print("Type 'help' to see available commands.")
        return

    # Execute the command
    try:
        _COMMANDS[command]["handler"](args, state, console)
    except Exception as e:
        console.print(
            f"[bold red]Error executing command '{command}':[/bold red] {e!s}"
        )


# Import command modules

# Register commands
register_command("help", help_command, "Show help information")
register_command(
    "simulate",
    simulate_command,
    "Simulate portfolio performance with SPY changes",
    ["spy", "scenario"],
)
register_command("position", position_command, "Analyze a specific position group")
register_command(
    "portfolio",
    portfolio_command,
    "View and analyze portfolio",
    ["list", "summary", "load"],
)
register_command("exit", lambda *args: None, "Exit the application")