Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 33 additions & 24 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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:
Expand All @@ -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])
}
Expand All @@ -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:
Expand All @@ -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])
}
Expand All @@ -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:
Expand All @@ -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])
}
Expand All @@ -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:
Expand All @@ -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
}
162 changes: 146 additions & 16 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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()))
})
}
}
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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()))
})
}
}
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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()))
})
}
}
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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()))
})
}
}
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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()))
})
}
}
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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()))
})
}
}
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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()))
})
}
}
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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()

Expand Down