Chess_Engine / main.py
electro-sb's picture
first commit
100a6dd
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()