Skip to content

Commit 1899495

Browse files
committed
fix: fail on parse errors instead of silently ignoring them
This is the same as PR #64, but adds a regression test and doesn't modify ParseArgs().
1 parent 5f3540f commit 1899495

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

subCommand.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,9 @@ func (sc *Subcommand) SetValueForKey(key string, value string) (bool, error) {
689689
// debugPrint("Evaluating string flag", f.ShortName, "==", key, "||", f.LongName, "==", key)
690690
if f.ShortName == key || f.LongName == key {
691691
// debugPrint("Setting string value for", key, "to", value)
692-
f.identifyAndAssignValue(value)
692+
if err := f.identifyAndAssignValue(value); err != nil {
693+
return false, err
694+
}
693695
return true, nil
694696
}
695697
}

subcommand_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -804,3 +804,18 @@ func TestNestedSCBoolFlag(t *testing.T) {
804804
t.Fatal("Error parsing args: " + err.Error())
805805
}
806806
}
807+
808+
func TestParseErrorsAreReportedRegression(t *testing.T) {
809+
defer func() {
810+
r := recover()
811+
if r == nil {
812+
t.Fatal("Expected crash on invalid syntax")
813+
}
814+
}()
815+
816+
flaggy.ResetParser()
817+
intFlag := 42
818+
flaggy.Int(&intFlag, "i", "int", "dummy")
819+
os.Args = []string{"prog", "--int", "abc"}
820+
flaggy.Parse()
821+
}

0 commit comments

Comments
 (0)