Skip to content

Commit d9d9022

Browse files
authored
Merge branch 'v2-maint' into issue_1731
2 parents dd8ce75 + c1bfd14 commit d9d9022

16 files changed

Lines changed: 114 additions & 30 deletions

altsrc/map_input_source.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package altsrc
33
import (
44
"fmt"
55
"math"
6-
"reflect"
76
"strings"
87
"time"
98

@@ -471,11 +470,5 @@ func (fsm *MapInputSource) isSet(name string) bool {
471470
}
472471

473472
func incorrectTypeForFlagError(name, expectedTypeName string, value interface{}) error {
474-
valueType := reflect.TypeOf(value)
475-
valueTypeName := ""
476-
if valueType != nil {
477-
valueTypeName = valueType.Name()
478-
}
479-
480-
return fmt.Errorf("Mismatched type for flag '%s'. Expected '%s' but actual is '%s'", name, expectedTypeName, valueTypeName)
473+
return fmt.Errorf("Mismatched type for flag '%s'. Expected '%s' but actual is '%T'", name, expectedTypeName, value)
481474
}

altsrc/map_input_source_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package altsrc
22

33
import (
4+
"fmt"
45
"testing"
56
"time"
67
)
@@ -33,3 +34,8 @@ func TestMapInputSource_Int64Slice(t *testing.T) {
3334
expect(t, []int64{1, 2, 3}, d)
3435
expect(t, nil, err)
3536
}
37+
38+
func TestMapInputSource_IncorrectFlagTypeError(t *testing.T) {
39+
var testVal *bool
40+
expect(t, incorrectTypeForFlagError("test", "bool", testVal), fmt.Errorf("Mismatched type for flag 'test'. Expected 'bool' but actual is '*bool'"))
41+
}

app.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,18 @@ func (a *App) RunContext(ctx context.Context, arguments []string) (err error) {
332332
return a.rootCommand.Run(cCtx, arguments...)
333333
}
334334

335-
// This is a stub function to keep public API unchanged from old code
336-
//
337-
// Deprecated: use App.Run or App.RunContext
335+
// RunAsSubcommand is for legacy/compatibility purposes only. New code should only
336+
// use App.RunContextMost. This function is slated to be removed in v3
338337
func (a *App) RunAsSubcommand(ctx *Context) (err error) {
339-
return a.RunContext(ctx.Context, ctx.Args().Slice())
338+
a.Setup()
339+
340+
cCtx := NewContext(a, nil, ctx)
341+
cCtx.shellComplete = ctx.shellComplete
342+
343+
a.rootCommand = a.newRootCommand()
344+
cCtx.Command = a.rootCommand
345+
346+
return a.rootCommand.Run(cCtx, ctx.Args().Slice()...)
340347
}
341348

342349
func (a *App) suggestFlagFromError(err error, command string) (string, error) {

app_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2195,6 +2195,48 @@ func TestApp_Run_Categories(t *testing.T) {
21952195
}
21962196
}
21972197

2198+
func TestApp_Run_SubCommand_Categories(t *testing.T) {
2199+
buf := new(bytes.Buffer)
2200+
2201+
app := &App{
2202+
Name: "categories",
2203+
HideHelp: true,
2204+
Commands: []*Command{
2205+
{
2206+
Name: "main",
2207+
Subcommands: []*Command{
2208+
{
2209+
Name: "command1",
2210+
Category: "CAT1",
2211+
},
2212+
{
2213+
Name: "command2",
2214+
Category: "CAT2",
2215+
},
2216+
{
2217+
Name: "command3",
2218+
Category: "CAT1",
2219+
},
2220+
},
2221+
},
2222+
},
2223+
Writer: buf,
2224+
}
2225+
2226+
_ = app.Run([]string{"categories", "main"})
2227+
2228+
output := buf.String()
2229+
2230+
if !strings.Contains(output, "CAT1:\n command1") {
2231+
t.Errorf("want buffer to include category %q, did not: \n%q", "CAT1:\n command1", output)
2232+
}
2233+
2234+
if !strings.Contains(output, "CAT2:\n command2") {
2235+
t.Errorf("want buffer to include category %q, did not: \n%q", "CAT2:\n command2", output)
2236+
}
2237+
2238+
}
2239+
21982240
func TestApp_VisibleCategories(t *testing.T) {
21992241
app := &App{
22002242
Name: "visible-categories",

docs.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,14 @@ func prepareFlags(
153153
// flagDetails returns a string containing the flags metadata
154154
func flagDetails(flag DocGenerationFlag) string {
155155
description := flag.GetUsage()
156-
value := flag.GetValue()
157-
if value != "" {
158-
description += " (default: " + value + ")"
156+
if flag.TakesValue() {
157+
defaultText := flag.GetDefaultText()
158+
if defaultText == "" {
159+
defaultText = flag.GetValue()
160+
}
161+
if defaultText != "" {
162+
description += " (default: " + defaultText + ")"
163+
}
159164
}
160165
return ": " + description
161166
}

fish_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ func testApp() *App {
2727
app.Name = "greet"
2828
app.Flags = []Flag{
2929
&StringFlag{
30-
Name: "socket",
31-
Aliases: []string{"s"},
32-
Usage: "some 'usage' text",
33-
Value: "value",
34-
TakesFile: true,
30+
Name: "socket",
31+
Aliases: []string{"s"},
32+
Usage: "some 'usage' text",
33+
Value: "value",
34+
DefaultText: "/some/path",
35+
TakesFile: true,
3536
},
3637
&StringFlag{Name: "flag", Aliases: []string{"fl", "f"}},
3738
&BoolFlag{

godoc-current.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ USAGE:
141141
DESCRIPTION:
142142
{{template "descriptionTemplate" .}}{{end}}{{if .VisibleCommands}}
143143

144-
COMMANDS:{{template "visibleCommandTemplate" .}}{{end}}{{if .VisibleFlagCategories}}
144+
COMMANDS:{{template "visibleCommandCategoryTemplate" .}}{{end}}{{if .VisibleFlagCategories}}
145145

146146
OPTIONS:{{template "visibleFlagCategoryTemplate" .}}{{else if .VisibleFlags}}
147147

help.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ var helpCommand = &Command{
4242
// 3 $ app foo
4343
// 4 $ app help foo
4444
// 5 $ app foo help
45+
// 6 $ app foo -h (with no other sub-commands nor flags defined)
4546

4647
// Case 4. when executing a help command set the context to parent
4748
// to allow resolution of subsequent args. This will transform
@@ -77,6 +78,8 @@ var helpCommand = &Command{
7778
HelpPrinter(cCtx.App.Writer, templ, cCtx.Command)
7879
return nil
7980
}
81+
82+
// Case 6, handling incorporated in the callee itself
8083
return ShowSubcommandHelp(cCtx)
8184
},
8285
}
@@ -292,8 +295,12 @@ func ShowSubcommandHelp(cCtx *Context) error {
292295
if cCtx == nil {
293296
return nil
294297
}
295-
296-
HelpPrinter(cCtx.App.Writer, SubcommandHelpTemplate, cCtx.Command)
298+
// use custom template when provided (fixes #1703)
299+
templ := SubcommandHelpTemplate
300+
if cCtx.Command != nil && cCtx.Command.CustomHelpTemplate != "" {
301+
templ = cCtx.Command.CustomHelpTemplate
302+
}
303+
HelpPrinter(cCtx.App.Writer, templ, cCtx.Command)
297304
return nil
298305
}
299306

help_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,29 @@ func TestHideHelpCommand_WithSubcommands(t *testing.T) {
11261126
}
11271127
}
11281128

1129+
func TestHideHelpCommand_RunAsSubcommand_True_CustomTemplate(t *testing.T) {
1130+
var buf bytes.Buffer
1131+
1132+
app := &App{
1133+
Writer: &buf,
1134+
Commands: []*Command{
1135+
{
1136+
Name: "dummy",
1137+
CustomHelpTemplate: "TEMPLATE",
1138+
HideHelpCommand: true,
1139+
},
1140+
},
1141+
}
1142+
1143+
err := app.RunAsSubcommand(newContextFromStringSlice([]string{"", "dummy", "-h"}))
1144+
if err != nil {
1145+
t.Errorf("Run returned unexpected error: %v", err)
1146+
}
1147+
if !strings.Contains(buf.String(), "TEMPLATE") {
1148+
t.Errorf("Custom Help template ignored")
1149+
}
1150+
}
1151+
11291152
func TestDefaultCompleteWithFlags(t *testing.T) {
11301153
origEnv := os.Environ()
11311154
origArgv := os.Args

template.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ USAGE:
8888
DESCRIPTION:
8989
{{template "descriptionTemplate" .}}{{end}}{{if .VisibleCommands}}
9090
91-
COMMANDS:{{template "visibleCommandTemplate" .}}{{end}}{{if .VisibleFlagCategories}}
91+
COMMANDS:{{template "visibleCommandCategoryTemplate" .}}{{end}}{{if .VisibleFlagCategories}}
9292
9393
OPTIONS:{{template "visibleFlagCategoryTemplate" .}}{{else if .VisibleFlags}}
9494

0 commit comments

Comments
 (0)