Skip to content

Commit cc330ab

Browse files
committed
✨ feat: enhance CLI with model selection and help
✨ feat: enhance CLI with model selection and help ✨ Features: - Added support for specifying AI model in commit command - Introduced a help command for detailed usage instructions 🐛 Bug Fixes: - Ensured average cost calculation defaults to 0.0 if not present 📝 Documentation: - Updated help text to reflect new commands and options Enhanced CLI functionality with new model options and comprehensive help.
1 parent 1cecfee commit cc330ab

File tree

3 files changed

+64
-14
lines changed

3 files changed

+64
-14
lines changed

commitloom/__main__.py

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
from .cli import console
2020
from .cli.cli_handler import CommitLoom
21+
from . import __version__
22+
from .config.settings import config
2123

2224

2325
def handle_error(error: BaseException) -> None:
@@ -30,6 +32,7 @@ def handle_error(error: BaseException) -> None:
3032

3133
@click.group()
3234
@click.option("-d", "--debug", is_flag=True, help="Enable debug logging")
35+
@click.version_option(version=__version__, prog_name="CommitLoom")
3336
@click.pass_context
3437
def cli(ctx, debug: bool) -> None:
3538
"""Create structured git commits with AI-generated messages."""
@@ -43,8 +46,14 @@ def cli(ctx, debug: bool) -> None:
4346
@cli.command(help="Generate an AI-powered commit message and commit your changes")
4447
@click.option("-y", "--yes", is_flag=True, help="Skip all confirmation prompts")
4548
@click.option("-c", "--combine", is_flag=True, help="Combine all changes into a single commit")
49+
@click.option(
50+
"-m",
51+
"--model",
52+
type=click.Choice(list(config.model_costs.keys())),
53+
help=f"Specify the AI model to use (default: {config.default_model})"
54+
)
4655
@click.pass_context
47-
def commit(ctx, yes: bool, combine: bool) -> None:
56+
def commit(ctx, yes: bool, combine: bool, model: str | None) -> None:
4857
"""Generate commit message and commit changes."""
4958
debug = ctx.obj.get("DEBUG", False)
5059

@@ -56,6 +65,12 @@ def commit(ctx, yes: bool, combine: bool) -> None:
5665

5766
# Initialize with test_mode
5867
loom = CommitLoom(test_mode=test_mode, api_key=api_key if api_key else None)
68+
69+
# Set custom model if specified
70+
if model:
71+
os.environ["COMMITLOOM_MODEL"] = model
72+
console.print_info(f"Using model: {model}")
73+
5974
loom.run(auto_commit=yes, combine_commits=combine, debug=debug)
6075
except (KeyboardInterrupt, Exception) as e:
6176
handle_error(e)
@@ -79,18 +94,51 @@ def stats(ctx) -> None:
7994
sys.exit(1)
8095

8196

97+
@cli.command(help="Display detailed help information")
98+
def help() -> None:
99+
"""Display detailed help information about CommitLoom."""
100+
help_text = f"""
101+
[bold cyan]CommitLoom v{__version__}[/bold cyan]
102+
[italic]Weave perfect git commits with AI-powered intelligence[/italic]
103+
104+
[bold]Basic Usage:[/bold]
105+
loom Run the default commit command
106+
loom commit Generate commit message for staged changes
107+
loom commit -y Skip confirmation prompts
108+
loom commit -c Combine all changes into a single commit
109+
loom commit -m MODEL Specify AI model to use
110+
loom stats Show usage statistics
111+
loom --version Display version information
112+
loom help Show this help message
113+
114+
[bold]Available Models:[/bold]
115+
{', '.join(config.model_costs.keys())}
116+
Default: {config.default_model}
117+
118+
[bold]Environment Setup:[/bold]
119+
1. Set OPENAI_API_KEY in your environment or in a .env file
120+
2. Stage your changes with 'git add'
121+
3. Run 'loom' to generate and apply commit messages
122+
123+
[bold]Documentation:[/bold]
124+
Full documentation: https://github.com/Arakiss/commitloom#readme
125+
"""
126+
console.console.print(help_text)
127+
128+
82129
# For backwards compatibility, default to commit command if no subcommand provided
83130
def main() -> None:
84131
"""Entry point for the CLI."""
85-
# Check if the first argument is a known command, if not, insert 'commit'
86-
known_commands = ['commit', 'stats']
87-
88-
if len(sys.argv) > 1 and not sys.argv[1].startswith('-') and sys.argv[1] not in known_commands:
132+
known_commands = ['commit', 'stats', 'help']
133+
commit_options = ['-y', '--yes', '-c', '--combine', '-m', '--model']
134+
135+
# If no arguments or only options without a command, add 'commit' as the default command
136+
if len(sys.argv) == 1 or (len(sys.argv) > 1 and sys.argv[1].startswith('-')):
137+
# Insert 'commit' as the first argument
138+
sys.argv.insert(1, 'commit')
139+
# If the first argument is not a known command and not an option, insert 'commit'
140+
elif len(sys.argv) > 1 and not sys.argv[1].startswith('-') and sys.argv[1] not in known_commands:
89141
sys.argv.insert(1, 'commit')
90-
91-
# If no arguments provided, add 'commit' as the default command
92-
if len(sys.argv) == 1:
93-
sys.argv.append('commit')
94142

95143
cli(obj={})
96144

commitloom/cli/cli_handler.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ def stats_command(self) -> None:
371371
if "avg_commits_per_day" in stats:
372372
avg_commits = stats["avg_commits_per_day"]
373373
console.console.print(f" • Average commits per day: {avg_commits:.2f}")
374-
avg_cost = stats["avg_cost_per_day"]
374+
avg_cost = stats.get("avg_cost_per_day", 0.0)
375375
console.console.print(f" • Average cost per day: €{avg_cost:.4f}")
376376

377377
# Display repository stats if available
@@ -403,7 +403,8 @@ def stats_command(self) -> None:
403403
for model, model_data in model_stats.items():
404404
console.console.print(f" • {model}:")
405405
console.console.print(f" - Total tokens: {model_data.get('tokens', 0):,}")
406-
console.console.print(f" - Total cost: €{model_data.get('cost', 0.0):.4f}")
406+
cost = model_data.get('cost', 0.0)
407+
console.console.print(f" - Total cost: €{cost:.4f}")
407408
avg_tokens = model_data.get("avg_tokens_per_commit", 0.0)
408409
console.console.print(f" - Avg tokens per commit: {avg_tokens:.1f}")
409410

commitloom/services/ai_service.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ def from_api_usage(
2929
completion_tokens = usage["completion_tokens"]
3030
total_tokens = usage["total_tokens"]
3131

32-
# Calculate costs
33-
input_cost = (prompt_tokens / 1_000_000) * config.model_costs[model].input
34-
output_cost = (completion_tokens / 1_000_000) * config.model_costs[model].output
32+
# Calculate costs - convert from per million tokens to actual cost
33+
# These costs are in EUR per 1M tokens, so we divide by 1M to get cost per token
34+
input_cost = (prompt_tokens / 1_000) * config.model_costs[model].input
35+
output_cost = (completion_tokens / 1_000) * config.model_costs[model].output
3536
total_cost = input_cost + output_cost
3637

3738
return cls(

0 commit comments

Comments
 (0)