Skip to content

Commit

Permalink
Merge branch 'urfave:v2-maint' into v2-maint
Browse files Browse the repository at this point in the history
  • Loading branch information
skelouse authored Mar 28, 2023
2 parents a21bcd2 + 31b7abb commit 448be4b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func (c *Command) setup(ctx *Context) {
if scmd.HelpName == "" {
scmd.HelpName = fmt.Sprintf("%s %s", c.HelpName, scmd.Name)
}
scmd.separator = c.separator
newCmds = append(newCmds, scmd)
}
c.Subcommands = newCmds
Expand Down
27 changes: 27 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,3 +515,30 @@ func TestCommand_RunSubcommandWithDefault(t *testing.T) {
err = app.Run([]string{"app"})
expect(t, err, errors.New("should not run this subcommand"))
}

func TestCommand_PreservesSeparatorOnSubcommands(t *testing.T) {
var values []string
subcommand := &Command{
Name: "bar",
Flags: []Flag{
&StringSliceFlag{Name: "my-flag"},
},
Action: func(c *Context) error {
values = c.StringSlice("my-flag")
return nil
},
}
app := &App{
Commands: []*Command{
{
Name: "foo",
Subcommands: []*Command{subcommand},
},
},
SliceFlagSeparator: ";",
}

app.Run([]string{"app", "foo", "bar", "--my-flag", "1;2;3"})

expect(t, values, []string{"1", "2", "3"})
}
5 changes: 3 additions & 2 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,10 @@ func (cCtx *Context) checkRequiredFlags(flags []Flag) requiredFlagsErr {
var flagPresent bool
var flagName string

for _, key := range f.Names() {
flagName = key
flagNames := f.Names()
flagName = flagNames[0]

for _, key := range flagNames {
if cCtx.IsSet(strings.TrimSpace(key)) {
flagPresent = true
}
Expand Down
15 changes: 15 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,21 @@ func TestCheckRequiredFlags(t *testing.T) {
&StringFlag{Name: "n", Required: true},
},
},
{
testCase: "required_flag_with_alias_errors_with_actual_name",
expectedAnError: true,
expectedErrorContents: []string{"Required flag \"collection\" not set"},
flags: []Flag{
&StringFlag{Name: "collection", Required: true, Aliases: []string{"c"}},
},
},
{
testCase: "required_flag_without_name_or_aliases_doesnt_error",
expectedAnError: false,
flags: []Flag{
&StringFlag{Required: true},
},
},
}

for _, test := range tdata {
Expand Down

0 comments on commit 448be4b

Please sign in to comment.