File size: 2,822 Bytes
3e802a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import click
from .config import load_config
from .llm_handler import LLMHandler
from .orchestrator import DocstringOrchestrator
from .readme_generator import ReadmeGenerator

@click.group(context_settings=dict(help_option_names=['-h', '--help']))
@click.pass_context
def cli(ctx):
    """
    CodeScribe AI: A tool for AI-assisted project documentation.
    
    Choose a command below (e.g., 'docstrings', 'readmes') and provide its options.
    """
    # This parent command now only handles setup common to all subcommands.
    ctx.ensure_object(dict)
    try:
        config = load_config()
        if not config.api_keys:
            raise click.UsageError("No API keys found in the .env file. Please create one.")
        
        ctx.obj['LLM_HANDLER'] = LLMHandler(config.api_keys)
        click.echo(f"Initialized with {len(config.api_keys)} API keys.")

    except Exception as e:
        raise click.ClickException(f"Initialization failed: {e}")

@cli.command()
@click.option('--path', required=True, help='The local path or Git URL of the project.')
@click.option('--desc', required=True, help='A short description of the project.')
@click.option('--exclude', multiple=True, help='Directory/regex pattern to exclude. Can be used multiple times.')
@click.pass_context
def docstrings(ctx, path, desc, exclude):
    """Generates Python docstrings for all files."""
    click.echo("\n--- Starting Docstring Generation ---")
    llm_handler = ctx.obj['LLM_HANDLER']
    
    orchestrator = DocstringOrchestrator(
        path_or_url=path,
        description=desc,
        exclude=list(exclude),
        llm_handler=llm_handler
    )
    try:
        orchestrator.run()
        click.secho("Successfully generated all docstrings.", fg="green")
    except Exception as e:
        click.secho(f"An error occurred during docstring generation: {e}", fg="red")


@cli.command()
@click.option('--path', required=True, help='The local path or Git URL of the project.')
@click.option('--desc', required=True, help='A short description of the project.')
@click.option('--exclude', multiple=True, help='Directory/regex pattern to exclude. Can be used multiple times.')
@click.pass_context
def readmes(ctx, path, desc, exclude):
    """Generates README.md files for each directory."""
    click.echo("\n--- Starting README Generation ---")
    llm_handler = ctx.obj['LLM_HANDLER']
    
    generator = ReadmeGenerator(
        path_or_url=path,
        description=desc,
        exclude=list(exclude),
        llm_handler=llm_handler
    )
    try:
        generator.run()
        click.secho("Successfully generated all README files.", fg="green")
    except Exception as e:
        click.secho(f"An error occurred during README generation: {e}", fg="red")

def main():
    cli(obj={})

if __name__ == '__main__':
    main()