Developer tools dump stack traces, exit codes, and jargon. Figuring out *what to do next* often means searching the same Stack Overflow answers again.
| 1 | """Typer CLI entry point for why-failed.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | from pathlib import Path |
| 6 | |
| 7 | import typer |
| 8 | |
| 9 | from why_failed import __version__ |
| 10 | from why_failed.formatter import ( |
| 11 | format_json, |
| 12 | format_match_rich, |
| 13 | format_no_match_rich, |
| 14 | format_quiet, |
| 15 | format_rules_list, |
| 16 | ) |
| 17 | from why_failed.matcher import analyze |
| 18 | from why_failed.stdin import InputError, join_args, read_file, read_stdin, stdin_is_piped |
| 19 | |
| 20 | # Exposed for CliRunner / tests. |
| 21 | app = typer.Typer( |
| 22 | name="why-failed", |
| 23 | help=( |
| 24 | "Analyze developer errors locally and get actionable explanations.\n\n" |
| 25 | "Examples:\n\n" |
| 26 | ' why-failed "fatal: not a git repository"\n\n' |
| 27 | " npm install 2>&1 | why-failed\n\n" |
| 28 | " why-failed --file build.log" |
| 29 | ), |
| 30 | add_completion=False, |
| 31 | no_args_is_help=False, |
| 32 | rich_markup_mode="rich", |
| 33 | ) |
| 34 | |
| 35 | |
| 36 | def _version_callback(value: bool) -> None: |
| 37 | if value: |
| 38 | typer.echo(f"why-failed {__version__}") |
| 39 | raise typer.Exit(0) |
| 40 | |
| 41 | |
| 42 | @app.command() |
| 43 | def cli( |
| 44 | text: list[str] | None = typer.Argument( |
| 45 | None, |
| 46 | help="Error text to analyze. Multiple arguments are joined with spaces.", |
| 47 | ), |
| 48 | file: Path | None = typer.Option( |
| 49 | None, |
| 50 | "--file", |
| 51 | "-f", |
| 52 | help="Read error text from a file.", |
| 53 | exists=False, |
| 54 | dir_okay=False, |
| 55 | readable=True, |
| 56 | ), |
| 57 | stdin: bool = typer.Option( |
| 58 | False, |
| 59 | "--stdin", |
| 60 | help="Read error text from stdin (even if not piped).", |
| 61 | ), |
| 62 | json_output: bool = typer.Option( |
| 63 | False, |
| 64 | "--json", |
| 65 | help="Emit machine-readable JSON (no ANSI colors).", |
| 66 | ), |
| 67 | quiet: bool = typer.Option( |
| 68 | False, |
| 69 | "--quiet", |
| 70 | "-q", |
| 71 | help="Print only the primary fix suggestion.", |
| 72 | ), |
| 73 | verbose: bool = typer.Option( |
| 74 | False, |
| 75 | "--verbose", |
| 76 | "-v", |
| 77 | help="Show match score, rule id, and alternatives.", |
| 78 | ), |
| 79 | list_rules: bool = typer.Option( |
| 80 | False, |
| 81 | "--list-rules", |
| 82 | help="List all available local rules and exit.", |
| 83 | ), |
| 84 | version: bool = typer.Option( |
| 85 | False, |
| 86 | "--version", |
| 87 | callback=_version_callback, |
| 88 | is_eager=True, |
| 89 | help="Show version and exit.", |
| 90 | ), |
| 91 | ) -> None: |
| 92 | """Analyze developer errors locally and get actionable explanations. |
| 93 | |
| 94 | Examples: |
| 95 | |
| 96 | why-failed "fatal: not a git repository" |
| 97 | npm install 2>&1 | why-failed |
| 98 | why-failed --file build.log |
| 99 | """ |
| 100 | del version # handled by eager callback |
| 101 | |
| 102 | if list_rules: |
| 103 | format_rules_list() |
| 104 | raise typer.Exit(0) |
| 105 | |
| 106 | try: |
| 107 | raw = _resolve_input(text, file=file, force_stdin=stdin) |
| 108 | except InputError as exc: |
| 109 | if json_output: |
| 110 | typer.echo('{"matched": false, "rule_id": null, "error": "input_error"}') |
| 111 | else: |
| 112 | typer.secho(str(exc), fg=typer.colors.RED, err=True) |
| 113 | raise typer.Exit(1) from exc |
| 114 | |
| 115 | if not raw.strip(): |
| 116 | msg = ( |
| 117 | "Empty input. Pass error text as arguments, use --file, " |
| 118 | "--stdin, or pipe output into why-failed." |
| 119 | ) |
| 120 | if json_output: |
| 121 | typer.echo('{"matched": false, "rule_id": null, "error": "empty_input"}') |
| 122 | else: |
| 123 | typer.secho(msg, fg=typer.colors.RED, err=True) |
| 124 | raise typer.Exit(1) |
| 125 | |
| 126 | result = analyze(raw) |
| 127 | |
| 128 | if json_output: |
| 129 | typer.echo(format_json(result)) |
| 130 | elif quiet: |
| 131 | format_quiet(result.match) |
| 132 | elif result.matched and result.match is not None: |
| 133 | format_match_rich(result.match, result, verbose=verbose) |
| 134 | else: |
| 135 | format_no_match_rich(result, verbose=verbose) |
| 136 | |
| 137 | if result.matched: |
| 138 | raise typer.Exit(0) |
| 139 | raise typer.Exit(2) |
| 140 | |
| 141 | |
| 142 | def _resolve_input( |
| 143 | text: list[str] | None, |
| 144 | *, |
| 145 | file: Path | None, |
| 146 | force_stdin: bool, |
| 147 | ) -> str: |
| 148 | if file is not None: |
| 149 | return read_file(file) |
| 150 | |
| 151 | if force_stdin: |
| 152 | return read_stdin(force=True) |
| 153 | |
| 154 | if text: |
| 155 | return join_args(text) |
| 156 | |
| 157 | if stdin_is_piped(): |
| 158 | return read_stdin(force=True) |
| 159 | |
| 160 | raise InputError( |
| 161 | "No input provided. Pass error text as arguments, use --file, " |
| 162 | "--stdin, or pipe output into why-failed." |
| 163 | ) |
| 164 | |
| 165 | |
| 166 | def main() -> None: |
| 167 | """Console script entry point.""" |
| 168 | app() |
| 169 | |
| 170 | |
| 171 | if __name__ == "__main__": |
| 172 | main() |