Files
2025-04-09 09:34:15 +02:00

129 lines
4.5 KiB
Python

"""
Command-line interface for the Edison application.
"""
import argparse
import logging
from colorama import init
from edison.config import config_manager
from edison.core import api_client
from edison.ui import console, interactive
from edison.utils import logging_utils, os_utils
# Logger will be initialized after parsing args
logger = None
def parse_arguments():
"""
Parse command line arguments.
Returns:
argparse.Namespace: The parsed arguments.
"""
parser = argparse.ArgumentParser(
description='AI bot that translates your question to a command.'
)
parser.add_argument('text', nargs='*',
help='A sequence of strings')
parser.add_argument("-s", "--safety", action='store_true',
help='Enable safety mode (only useful when safety is off)')
parser.add_argument("-c", "--config", action='store_true',
help='Print current configuration')
parser.add_argument("-v", "--verbose", action='store_true',
help='Enable verbose logging (DEBUG level)')
parser.add_argument("-i", "--interactive", action='store_true',
help='Start interactive shell mode')
parser.add_argument("-e", "--explain", action='store_true',
help='Explain the generated command')
parser.add_argument("--no-streaming", action='store_true',
help='Disable streaming output for command generation')
parser.add_argument("--no-rich", action='store_true',
help='Disable rich text formatting')
parser.add_argument("--theme", type=str,
help='Set syntax highlighting theme (e.g., monokai, github-dark)')
return parser.parse_args()
def main():
"""
Main entry point for the application.
"""
# Parse command line arguments
args = parse_arguments()
# Initialize logging based on the verbose flag
global logger
logger = logging_utils.setup_logging(verbose=args.verbose)
try:
# Load configuration
config = config_manager.load_config()
logger.debug("Configuration loaded")
# Process command-line arguments
if args.safety:
config["safety"] = args.safety
# Set streaming and UI options based on command-line arguments
if args.no_streaming:
config["streaming"] = False
# Initialize UI config if not present
if "ui" not in config:
config["ui"] = {}
# Set rich formatting option
if args.no_rich:
config["ui"]["rich_formatting"] = False
# Set theme if specified
if args.theme:
config["ui"]["theme"] = args.theme
# Set default shell if not specified
if "shell" not in config:
config["shell"] = os_utils.get_default_shell()
# Print configuration if requested
if args.config:
config_manager.print_config(config)
return
# Enable color output on Windows using colorama
init()
# Initialize API client
try:
client = api_client.create_client(config)
logger.debug("API client initialized")
# Start interactive mode if requested
if args.interactive:
interactive.interactive_mode(client, config)
return
# Check if we have a query
if not args.text:
print("No query provided. Use -i for interactive mode.")
return
# Join the text arguments
user_prompt = " ".join(args.text)
# Generate command with or without streaming based on config
if config.get("streaming", True):
# Use streaming version
console.handle_command_execution_streaming(client, config, user_prompt, explain=args.explain)
else:
# Use non-streaming version
command = api_client.generate_command(client, config, user_prompt)
logger.debug("Command generated")
console.handle_command_execution(client, config, command, explain=args.explain)
except Exception as e:
logger.error(f"Error: {e}")
print(f"Error: {e}")
except Exception as e:
logger.error(f"Unhandled exception: {e}")
print(f"An unexpected error occurred: {e}")
if __name__ == "__main__":
main()