File size: 2,662 Bytes
100a6dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import argparse
import sys
from chess_engine.api.cli import main as cli_main

# Try to import uvicorn, but don't fail if it's not available
try:
    import uvicorn
    UVICORN_AVAILABLE = True
except ImportError:
    UVICORN_AVAILABLE = False

def main():
    """Main entry point for the application"""
    parser = argparse.ArgumentParser(description="Chess Engine with Stockfish")
    subparsers = parser.add_subparsers(dest="command", help="Command to run")
    
    # CLI command
    cli_parser = subparsers.add_parser("cli", help="Run command-line interface")
    cli_parser.add_argument("--color", "-c", choices=["white", "black"], default="white",
                        help="Player's color (default: white)")
    cli_parser.add_argument("--difficulty", "-d", 
                        choices=["beginner", "easy", "medium", "hard", "expert", "master"],
                        default="medium", help="AI difficulty level (default: medium)")
    cli_parser.add_argument("--time", "-t", type=float, default=1.0,
                        help="Time limit for AI moves in seconds (default: 1.0)")
    cli_parser.add_argument("--stockfish-path", "-s", type=str, default=None,
                        help="Path to Stockfish executable")
    cli_parser.add_argument("--no-book", action="store_true",
                        help="Disable opening book")
    cli_parser.add_argument("--no-analysis", action="store_true",
                        help="Disable position analysis")
    
    # API command
    api_parser = subparsers.add_parser("api", help="Run REST API server")
    api_parser.add_argument("--host", default="127.0.0.1", help="Host to bind (default: 127.0.0.1)")
    api_parser.add_argument("--port", "-p", type=int, default=8000, help="Port to bind (default: 8000)")
    api_parser.add_argument("--reload", action="store_true", help="Enable auto-reload")
    
    args = parser.parse_args()
    
    if args.command == "cli":
        cli_main(args)
    elif args.command == "api":
        if not UVICORN_AVAILABLE:
            print("Error: uvicorn package is not installed. Please install it with:")
            print("pip install uvicorn")
            sys.exit(1)
        
        try:
            from chess_engine.api.rest_api import app
            uvicorn.run(app, host=args.host, port=args.port, reload=args.reload)
        except ImportError as e:
            print(f"Error: {e}")
            print("Make sure all required packages are installed:")
            print("pip install fastapi uvicorn")
            sys.exit(1)
    else:
        # Default to CLI if no command specified
        cli_main()

if __name__ == "__main__":
    main()