Skip to content

Commit

Permalink
Merge pull request #72 from marco-m/fix-parse-error-silently-ignored
Browse files Browse the repository at this point in the history
fix: fail on parse errors instead of silently ignoring them
  • Loading branch information
Eric Greer committed May 17, 2022
2 parents 56e4402 + 1899495 commit f98079e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion subCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,9 @@ func (sc *Subcommand) SetValueForKey(key string, value string) (bool, error) {
// debugPrint("Evaluating string flag", f.ShortName, "==", key, "||", f.LongName, "==", key)
if f.ShortName == key || f.LongName == key {
// debugPrint("Setting string value for", key, "to", value)
f.identifyAndAssignValue(value)
if err := f.identifyAndAssignValue(value); err != nil {
return false, err
}
return true, nil
}
}
Expand Down
15 changes: 15 additions & 0 deletions subcommand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -814,3 +814,18 @@ func TestNestedSCBoolFlag(t *testing.T) {
t.Fatal("Error parsing args: " + err.Error())
}
}

func TestParseErrorsAreReportedRegression(t *testing.T) {
defer func() {
r := recover()
if r == nil {
t.Fatal("Expected crash on invalid syntax")
}
}()

flaggy.ResetParser()
intFlag := 42
flaggy.Int(&intFlag, "i", "int", "dummy")
os.Args = []string{"prog", "--int", "abc"}
flaggy.Parse()
}

0 comments on commit f98079e

Please sign in to comment.