Skip to content

Commit 22fb72b

Browse files
committed
fix(cli): preserve concrete parse errors
Purpose of the change: - Address CodeRabbit feedback on the CLI unknown-option formatter. - Ensure concrete parser errors are not replaced by later unknown-option text. How behavior was before: - Any parse failure could trigger an argument scan for unknown options. - A command like -R not-an-int --blah could report --blah instead of the invalid integer value. Why that was a problem: - It hid the real parse failure and made CLI diagnostics less accurate. - The Apprise-style unknown-option formatter was broader than the condition it was meant to handle. What the new change accomplishes: - Limits Apprise-style unknown-option formatting to actual unknown-flag parse errors. - Leaves other parse failures on the standard parser error path. - Adds regression coverage for invalid integer parsing followed by an unknown option. How it works: - Adds a focused parse-error classifier for the Go flag unknown-option error prefix. - Keeps the existing unknown-option scan only inside that classifier branch.
1 parent 957caa2 commit 22fb72b

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

internal/cli/cli.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,11 @@ func Run(args []string, stdout, stderr io.Writer) int {
206206
printHelp(stdout)
207207
return 0
208208
}
209-
if option := unknownOption(args, fs); option != "" {
210-
printUnknownOption(stderr, option)
211-
return 2
209+
if isUnknownFlagParseError(err) {
210+
if option := unknownOption(args, fs); option != "" {
211+
printUnknownOption(stderr, option)
212+
return 2
213+
}
212214
}
213215
fmt.Fprintln(stderr, err)
214216
return 2
@@ -341,6 +343,10 @@ func printUnknownOption(w io.Writer, option string) {
341343
fmt.Fprintf(w, "Error: No such option '%s'.\n", option)
342344
}
343345

346+
func isUnknownFlagParseError(err error) bool {
347+
return strings.HasPrefix(err.Error(), "flag provided but not defined: ")
348+
}
349+
344350
func defaultCliOptions() cliOptions {
345351
return cliOptions{
346352
notificationType: string(notify.NotifyInfo),

internal/cli/cli_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,23 @@ func TestCLILegacyFlagFormsStillParse(t *testing.T) {
160160
}
161161
}
162162

163+
func TestCLIParseErrorsAreNotReportedAsLaterUnknownOptions(t *testing.T) {
164+
result := runGoCLI("-R", "not-an-int", "--blah")
165+
166+
if result.code != 2 {
167+
t.Fatalf("expected parse failure exit code, got code=%d stdout=%q stderr=%q", result.code, result.stdout, result.stderr)
168+
}
169+
if result.stdout != "" {
170+
t.Fatalf("expected empty stdout, got %q", result.stdout)
171+
}
172+
if !strings.Contains(result.stderr, `invalid value "not-an-int" for flag -R`) {
173+
t.Fatalf("expected invalid integer parse error, got stderr=%q", result.stderr)
174+
}
175+
if strings.Contains(result.stderr, "No such option") {
176+
t.Fatalf("expected parse error not unknown-option error, got stderr=%q", result.stderr)
177+
}
178+
}
179+
163180
func TestRunConvertsMarkdownInputForHTMLTargetFormat(t *testing.T) {
164181
testutil.RequirePythonApprise(t)
165182

0 commit comments

Comments
 (0)