4ea36783d6
Extracted main.py (556 lines) into focused modules: - cli/parser.py: Argument parsing (151 lines) - cli/main_runner.py: Main application logic (320 lines) - cli/test_runner.py: Test mode runner (81 lines) - cli/tool_server.py: Tool server runner (69 lines) - utils/network.py: Network utilities (IP detection) main.py is now 99 lines (down from 556). All 35 tests pass. Note: main_runner.py at 320 lines is slightly over 300 limit, will address in subsequent refactoring.
152 lines
4.2 KiB
Python
152 lines
4.2 KiB
Python
"""CLI argument parsing for Local Swarm."""
|
|
|
|
import argparse
|
|
from typing import Optional
|
|
|
|
|
|
def create_parser() -> argparse.ArgumentParser:
|
|
"""Create and configure the argument parser."""
|
|
parser = argparse.ArgumentParser(
|
|
description="Local Swarm - AI-powered coding LLM swarm",
|
|
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
epilog="""
|
|
Examples:
|
|
python main.py # Interactive setup and start
|
|
python main.py --auto # Auto-detect and start without menu
|
|
python main.py --detect # Show hardware detection only
|
|
python main.py --model qwen:3b:q4 # Use specific model (skip menu)
|
|
python main.py --port 17615 # Use custom port (default: 17615)
|
|
python main.py --host 192.168.1.5 # Bind to specific IP
|
|
python main.py --instances 4 # Force number of instances
|
|
python main.py --download-only # Download model only
|
|
python main.py --test # Test with sample prompt
|
|
python main.py --mcp # Enable MCP server
|
|
python main.py --federation # Enable federation with other instances
|
|
python main.py --federation --peer 192.168.1.10:17615 # Manual peer
|
|
"""
|
|
)
|
|
|
|
# Mode options
|
|
parser.add_argument(
|
|
"--auto",
|
|
action="store_true",
|
|
help="Auto-detect best configuration without interactive menu"
|
|
)
|
|
parser.add_argument(
|
|
"--detect",
|
|
action="store_true",
|
|
help="Show hardware detection and exit"
|
|
)
|
|
|
|
# Model options
|
|
parser.add_argument(
|
|
"--model",
|
|
type=str,
|
|
help="Model to use (format: name:size:quant, e.g., qwen:3b:q4)"
|
|
)
|
|
parser.add_argument(
|
|
"--instances",
|
|
type=int,
|
|
help="Force number of instances (overrides auto-calculation)"
|
|
)
|
|
|
|
# Server options
|
|
parser.add_argument(
|
|
"--port",
|
|
type=int,
|
|
default=17615,
|
|
help="Port to run the API server on (default: 17615)"
|
|
)
|
|
parser.add_argument(
|
|
"--host",
|
|
type=str,
|
|
default=None,
|
|
help="Host IP to bind to (default: auto-detect)"
|
|
)
|
|
|
|
# Operation modes
|
|
parser.add_argument(
|
|
"--download-only",
|
|
action="store_true",
|
|
help="Download models only, don't start server"
|
|
)
|
|
parser.add_argument(
|
|
"--test",
|
|
action="store_true",
|
|
help="Test with a sample prompt"
|
|
)
|
|
parser.add_argument(
|
|
"--mcp",
|
|
action="store_true",
|
|
help="Enable MCP server alongside HTTP API"
|
|
)
|
|
|
|
# Configuration
|
|
parser.add_argument(
|
|
"--config",
|
|
type=str,
|
|
default="config.yaml",
|
|
help="Path to config file"
|
|
)
|
|
|
|
# Federation options
|
|
parser.add_argument(
|
|
"--federation",
|
|
action="store_true",
|
|
help="Enable federation with other Local Swarm instances on the network"
|
|
)
|
|
parser.add_argument(
|
|
"--peer",
|
|
action="append",
|
|
dest="peers",
|
|
help="Manually add a peer (format: host:port, can be used multiple times)"
|
|
)
|
|
|
|
# Tool server options
|
|
parser.add_argument(
|
|
"--tool-server",
|
|
action="store_true",
|
|
help="Run as dedicated tool execution server (executes read/write/bash tools)"
|
|
)
|
|
parser.add_argument(
|
|
"--tool-port",
|
|
type=int,
|
|
default=17616,
|
|
help="Port for tool execution server (default: 17616)"
|
|
)
|
|
parser.add_argument(
|
|
"--tool-host",
|
|
type=str,
|
|
default=None,
|
|
nargs='?',
|
|
const='',
|
|
help="URL of tool execution server. Use without value for auto-detected local IP"
|
|
)
|
|
parser.add_argument(
|
|
"--use-opencode-tools",
|
|
action="store_true",
|
|
help="Use opencode's tool definitions (~27k tokens). Default: use local tool server"
|
|
)
|
|
|
|
# Version
|
|
parser.add_argument(
|
|
"--version",
|
|
action="version",
|
|
version="%(prog)s 0.1.0"
|
|
)
|
|
|
|
return parser
|
|
|
|
|
|
def parse_args(args: Optional[list] = None):
|
|
"""Parse command line arguments.
|
|
|
|
Args:
|
|
args: Command line arguments (defaults to sys.argv)
|
|
|
|
Returns:
|
|
Parsed arguments namespace
|
|
"""
|
|
parser = create_parser()
|
|
return parser.parse_args(args)
|