From 5a55380d0a5e31b8341f00a82a2fc336c8dc88a3 Mon Sep 17 00:00:00 2001 From: Seth Hoenig Date: Thu, 26 Nov 2026 06:18:19 -0600 Subject: [PATCH] flags: do not panic if required flag is set with no value --- commands.go | 57 ++++++++++------- commands_test.go | 162 ++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 179 insertions(+), 40 deletions(-) diff --git a/commands.go b/commands.go index d9485fd..d592f5c 100644 --- a/commands.go +++ b/commands.go @@ -156,6 +156,10 @@ func (c *Component) run(output io.Writer) *result { return &result{code: Success} } + if !c.checkRequired(output) { + return &result{code: Failure} + } + if c.Leaf() && c.Function != nil { code := c.Function(c) if code == Usability { @@ -329,9 +333,6 @@ func (c *Component) GetString(flag string) string { if f.Default != nil { return f.Default.Value.(string) } - if f.Require { - panicf("no value for string flag %q", flag) - } case 1: return c.vals.strings[flag][0] default: @@ -346,9 +347,6 @@ func (c *Component) GetStrings(flag string) []string { if f.Default != nil { return []string{f.Default.Value.(string)} } - if f.Require { - panicf("no value for string flag %q", flag) - } } return slices.Clone(c.vals.strings[flag]) } @@ -364,9 +362,6 @@ func (c *Component) GetInt(flag string) int { if f.Default != nil { return f.Default.Value.(int) } - if f.Require { - panicf("no value for int flag %q", flag) - } case 1: return c.vals.ints[flag][0] default: @@ -381,9 +376,6 @@ func (c *Component) GetInts(flag string) []int { if f.Default != nil { return []int{f.Default.Value.(int)} } - if f.Require { - panicf("no value for int flag %q", flag) - } } return slices.Clone(c.vals.ints[flag]) } @@ -399,9 +391,6 @@ func (c *Component) GetDuration(flag string) time.Duration { if f.Default != nil { return f.Default.Value.(time.Duration) } - if f.Require { - panicf("no value for duration flag %q", flag) - } case 1: return c.vals.durations[flag][0] default: @@ -416,9 +405,6 @@ func (c *Component) GetDurations(flag string) []time.Duration { if f.Default != nil { return []time.Duration{f.Default.Value.(time.Duration)} } - if f.Require { - panicf("no value for duration flag %q", flag) - } } return slices.Clone(c.vals.durations[flag]) } @@ -434,9 +420,6 @@ func (c *Component) GetBool(flag string) bool { if f.Default != nil { return f.Default.Value.(bool) } - if f.Require { - panicf("no value for boolean flag %q", flag) - } case 1: return c.vals.bools[flag][0] default: @@ -451,9 +434,35 @@ func (c *Component) GetBools(flag string) []bool { if f.Default != nil { return []bool{f.Default.Value.(bool)} } - if f.Require { - panicf("no value for boolean flag %q", flag) - } } return slices.Clone(c.vals.bools[flag]) } + +// checkRequired verifies every required flag has been given a value. +// A required flag with a Default is considered satisfied. It writes a +// message for each missing flag and returns false if any are missing. +func (c *Component) checkRequired(output io.Writer) bool { + ok := true + for _, f := range c.Flags { + if !f.Require || f.Default != nil { + continue + } + id := f.Identity() + var present bool + switch f.Type { + case StringFlag: + present = c.HasString(id) + case IntFlag: + present = c.HasInt(id) + case BooleanFlag: + present = c.HasBool(id) + case DurationFlag: + present = c.HasDuration(id) + } + if !present { + writef(output, "babycli: flag %q is required", id) + ok = false + } + } + return ok +} diff --git a/commands_test.go b/commands_test.go index 9d3cddb..995df00 100644 --- a/commands_test.go +++ b/commands_test.go @@ -599,7 +599,7 @@ func TestComponent_GetString(t *testing.T) { name: "required string not provided no default", expText: "", expCode: Failure, - expPanic: `babycli: no value for string flag "name"`, + expPanic: `babycli: flag "name" is required`, args: nil, root: &Component{ Flags: Flags{ @@ -717,7 +717,7 @@ func TestComponent_GetString(t *testing.T) { result := c.Run() must.Eq(t, tc.expText, output) must.Eq(t, tc.expCode, result) - must.Eq(t, tc.expPanic, failure.String()) + must.Eq(t, tc.expPanic, strings.TrimSpace(failure.String())) }) } } @@ -775,7 +775,7 @@ func TestComponent_GetStrings(t *testing.T) { name: "repeated strings not provided no default required", expText: "", expCode: Failure, - expPanic: `babycli: no value for string flag "name"`, + expPanic: `babycli: flag "name" is required`, args: nil, root: &Component{ Flags: Flags{ @@ -833,7 +833,7 @@ func TestComponent_GetStrings(t *testing.T) { result := c.Run() must.Eq(t, tc.expText, output) must.Eq(t, tc.expCode, result) - must.Eq(t, tc.expPanic, failure.String()) + must.Eq(t, tc.expPanic, strings.TrimSpace(failure.String())) }) } } @@ -915,7 +915,7 @@ func TestComponent_GetInt(t *testing.T) { name: "required int not provided no default", expText: "", expCode: Failure, - expPanic: `babycli: no value for int flag "age"`, + expPanic: `babycli: flag "age" is required`, args: nil, root: &Component{ Flags: Flags{ @@ -1012,7 +1012,7 @@ func TestComponent_GetInt(t *testing.T) { result := c.Run() must.Eq(t, tc.expText, output) must.Eq(t, tc.expCode, result) - must.Eq(t, tc.expPanic, failure.String()) + must.Eq(t, tc.expPanic, strings.TrimSpace(failure.String())) }) } } @@ -1070,7 +1070,7 @@ func TestComponent_GetInts(t *testing.T) { name: "repeated ints not provided no default required", expText: "", expCode: Failure, - expPanic: `babycli: no value for int flag "age"`, + expPanic: `babycli: flag "age" is required`, args: nil, root: &Component{ Flags: Flags{ @@ -1128,7 +1128,7 @@ func TestComponent_GetInts(t *testing.T) { result := c.Run() must.Eq(t, tc.expText, output) must.Eq(t, tc.expCode, result) - must.Eq(t, tc.expPanic, failure.String()) + must.Eq(t, tc.expPanic, strings.TrimSpace(failure.String())) }) } } @@ -1210,7 +1210,7 @@ func TestComponent_GetDuration(t *testing.T) { name: "required duration not provided no default", expText: "", expCode: Failure, - expPanic: `babycli: no value for duration flag "ttl"`, + expPanic: `babycli: flag "ttl" is required`, args: nil, root: &Component{ Flags: Flags{ @@ -1307,7 +1307,7 @@ func TestComponent_GetDuration(t *testing.T) { result := c.Run() must.Eq(t, tc.expText, output) must.Eq(t, tc.expCode, result) - must.Eq(t, tc.expPanic, failure.String()) + must.Eq(t, tc.expPanic, strings.TrimSpace(failure.String())) }) } } @@ -1365,7 +1365,7 @@ func TestComponent_GetDurations(t *testing.T) { name: "repeated durations not provided no default required", expText: "", expCode: Failure, - expPanic: `babycli: no value for duration flag "ttl"`, + expPanic: `babycli: flag "ttl" is required`, args: nil, root: &Component{ Flags: Flags{ @@ -1423,7 +1423,7 @@ func TestComponent_GetDurations(t *testing.T) { result := c.Run() must.Eq(t, tc.expText, output) must.Eq(t, tc.expCode, result) - must.Eq(t, tc.expPanic, failure.String()) + must.Eq(t, tc.expPanic, strings.TrimSpace(failure.String())) }) } } @@ -1545,7 +1545,7 @@ func TestComponent_GetBoolean(t *testing.T) { name: "required boolean not provided no default", expText: "", expCode: Failure, - expPanic: `babycli: no value for boolean flag "verbose"`, + expPanic: `babycli: flag "verbose" is required`, args: nil, root: &Component{ Flags: Flags{ @@ -1684,7 +1684,7 @@ func TestComponent_GetBoolean(t *testing.T) { result := c.Run() must.Eq(t, tc.expText, output) must.Eq(t, tc.expCode, result) - must.Eq(t, tc.expPanic, failure.String()) + must.Eq(t, tc.expPanic, strings.TrimSpace(failure.String())) }) } } @@ -1742,7 +1742,7 @@ func TestComponent_GetBooleans(t *testing.T) { name: "repeated booleans not provided no default required", expText: "", expCode: Failure, - expPanic: `babycli: no value for boolean flag "verbose"`, + expPanic: `babycli: flag "verbose" is required`, args: nil, root: &Component{ Flags: Flags{ @@ -1800,11 +1800,141 @@ func TestComponent_GetBooleans(t *testing.T) { result := c.Run() must.Eq(t, tc.expText, output) must.Eq(t, tc.expCode, result) - must.Eq(t, tc.expPanic, failure.String()) + must.Eq(t, tc.expPanic, strings.TrimSpace(failure.String())) }) } } +func TestComponent_required(t *testing.T) { + t.Parallel() + + var output string + var failure *strings.Builder + + cases := []struct { + name string + args []string + flags Flags + expText string + expCode Code + expErr string + }{ + { + name: "required string missing", + args: nil, + flags: Flags{{Type: StringFlag, Long: "name", Require: true}}, + expText: "", + expCode: Failure, + expErr: `babycli: flag "name" is required`, + }, + { + name: "required string provided", + args: []string{"--name", "bob"}, + flags: Flags{{Type: StringFlag, Long: "name", Require: true}}, + expText: "hello bob", + expCode: Success, + }, + { + name: "required string with default", + args: nil, + flags: Flags{ + { + Type: StringFlag, + Long: "name", + Require: true, + Default: &Default{Value: "alice"}, + }, + }, + expText: "hello alice", + expCode: Success, + }, + { + name: "multiple required missing", + args: nil, + flags: Flags{ + {Type: StringFlag, Long: "name", Require: true}, + {Type: IntFlag, Long: "age", Require: true}, + }, + expText: "", + expCode: Failure, + expErr: "babycli: flag \"name\" is required\nbabycli: flag \"age\" is required", + }, + } + + for _, tc := range cases { + output = "" // reset for each case + failure = new(strings.Builder) // reset for each case + + t.Run(tc.name, func(t *testing.T) { + config := &Configuration{ + Arguments: tc.args, + Top: &Component{ + Flags: tc.flags, + Function: func(c *Component) Code { + output = "hello " + c.GetString("name") + return Success + }, + }, + Output: failure, + } + c := New(config) + result := c.Run() + must.Eq(t, tc.expText, output) + must.Eq(t, tc.expCode, result) + must.Eq(t, tc.expErr, strings.TrimSpace(failure.String())) + }) + } +} + +func TestComponent_required_help(t *testing.T) { + t.Parallel() + + var output string + + config := &Configuration{ + Arguments: []string{"--help"}, + Top: &Component{ + Flags: Flags{ + {Type: StringFlag, Long: "name", Require: true}, + }, + Function: func(_ *Component) Code { + output = "ran" + return Success + }, + }, + Output: new(bytes.Buffer), + } + c := New(config) + result := c.Run() + must.Eq(t, Success, result) + must.Eq(t, "", output) +} + +func TestComponent_required_version(t *testing.T) { + t.Parallel() + + var output string + + config := &Configuration{ + Arguments: []string{"--version"}, + Version: "1.0", + Top: &Component{ + Flags: Flags{ + {Type: StringFlag, Long: "name", Require: true}, + }, + Function: func(_ *Component) Code { + output = "ran" + return Success + }, + }, + Output: new(bytes.Buffer), + } + c := New(config) + result := c.Run() + must.Eq(t, Success, result) + must.Eq(t, "", output) +} + func TestComponent_maybeSplit(t *testing.T) { t.Parallel()