70 lines
3.0 KiB
Python
70 lines
3.0 KiB
Python
# ProjectInitializerTool/project_initializer/cli/interface.py
|
|
|
|
import argparse
|
|
import sys
|
|
from pathlib import Path
|
|
from projectinitializer.core import project_creator # type: ignore
|
|
# Settings non è direttamente usato qui, ma il core sì
|
|
# from project_initializer.config import settings
|
|
|
|
def handle_cli_invocation(cli_args: list[str] | None = None) -> None:
|
|
"""
|
|
Parses command-line arguments and triggers project creation.
|
|
Manages CLI-specific output and error handling.
|
|
|
|
Args:
|
|
cli_args (list[str] | None, optional): A list of command line arguments
|
|
(e.g. from sys.argv[1:]).
|
|
If None, sys.argv[1:] is used.
|
|
"""
|
|
parser = argparse.ArgumentParser(
|
|
description="Python Project Initializer Tool (CLI Mode). "
|
|
"Creates a standard Python project structure."
|
|
)
|
|
parser.add_argument(
|
|
"root_directory",
|
|
type=str,
|
|
help="The absolute or relative root directory where the new project folder will be created."
|
|
)
|
|
parser.add_argument(
|
|
"project_name",
|
|
type=str,
|
|
help="The name of the new project (e.g., 'MyAwesomeProject'). "
|
|
"This will be used for the main project folder and, in a sanitized form, "
|
|
"for the Python package name."
|
|
)
|
|
# Aggiungere un flag per forzare la modalità CLI se __main__.py diventa più complesso
|
|
# parser.add_argument("--cli", action="store_true", help="Force CLI mode if auto-detection is ambiguous.")
|
|
|
|
if cli_args is None:
|
|
args = parser.parse_args() # Uses sys.argv[1:] by default
|
|
else:
|
|
args = parser.parse_args(cli_args)
|
|
|
|
|
|
root_dir_path = Path(args.root_directory).resolve() # Resolve to absolute path for clarity
|
|
project_name_str = args.project_name.strip()
|
|
|
|
if not project_name_str:
|
|
print("Error: Project name cannot be empty.", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
# La validazione di root_dir_path (se esiste ed è una directory)
|
|
# e la validazione più approfondita di project_name (es. isidentifier)
|
|
# sono gestite dalla funzione project_creator.create_project.
|
|
# Qui facciamo solo controlli di base sugli argomenti forniti.
|
|
|
|
try:
|
|
print(f"Attempting to create project '{project_name_str}' in '{root_dir_path}'...")
|
|
success_message = project_creator.create_project(str(root_dir_path), project_name_str)
|
|
# Il core creator stampa già i suoi messaggi di info/successo.
|
|
# Potremmo voler sopprimere quelli e stampare solo il messaggio finale qui,
|
|
# ma per ora va bene così.
|
|
# print(f"\n{success_message}") # Già stampato dal core.
|
|
sys.exit(0) # Success
|
|
except project_creator.ProjectCreationError as e:
|
|
print(f"Error: Project creation failed: {e}", file=sys.stderr)
|
|
sys.exit(1) # Failure
|
|
except Exception as e: # Catch-all for other unexpected errors from core or here
|
|
print(f"Error: An unexpected error occurred: {e}", file=sys.stderr)
|
|
sys.exit(1) |