Skip to content

Commit

Permalink
Merge branch 'v2-maint' into issue_1731
Browse files Browse the repository at this point in the history
  • Loading branch information
dearchap authored May 27, 2023
2 parents dd8ce75 + c1bfd14 commit d9d9022
Show file tree
Hide file tree
Showing 16 changed files with 114 additions and 30 deletions.
9 changes: 1 addition & 8 deletions altsrc/map_input_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package altsrc
import (
"fmt"
"math"
"reflect"
"strings"
"time"

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

func incorrectTypeForFlagError(name, expectedTypeName string, value interface{}) error {
valueType := reflect.TypeOf(value)
valueTypeName := ""
if valueType != nil {
valueTypeName = valueType.Name()
}

return fmt.Errorf("Mismatched type for flag '%s'. Expected '%s' but actual is '%s'", name, expectedTypeName, valueTypeName)
return fmt.Errorf("Mismatched type for flag '%s'. Expected '%s' but actual is '%T'", name, expectedTypeName, value)
}
6 changes: 6 additions & 0 deletions altsrc/map_input_source_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package altsrc

import (
"fmt"
"testing"
"time"
)
Expand Down Expand Up @@ -33,3 +34,8 @@ func TestMapInputSource_Int64Slice(t *testing.T) {
expect(t, []int64{1, 2, 3}, d)
expect(t, nil, err)
}

func TestMapInputSource_IncorrectFlagTypeError(t *testing.T) {
var testVal *bool
expect(t, incorrectTypeForFlagError("test", "bool", testVal), fmt.Errorf("Mismatched type for flag 'test'. Expected 'bool' but actual is '*bool'"))
}
15 changes: 11 additions & 4 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,18 @@ func (a *App) RunContext(ctx context.Context, arguments []string) (err error) {
return a.rootCommand.Run(cCtx, arguments...)
}

// This is a stub function to keep public API unchanged from old code
//
// Deprecated: use App.Run or App.RunContext
// RunAsSubcommand is for legacy/compatibility purposes only. New code should only
// use App.RunContextMost. This function is slated to be removed in v3
func (a *App) RunAsSubcommand(ctx *Context) (err error) {
return a.RunContext(ctx.Context, ctx.Args().Slice())
a.Setup()

cCtx := NewContext(a, nil, ctx)
cCtx.shellComplete = ctx.shellComplete

a.rootCommand = a.newRootCommand()
cCtx.Command = a.rootCommand

return a.rootCommand.Run(cCtx, ctx.Args().Slice()...)
}

func (a *App) suggestFlagFromError(err error, command string) (string, error) {
Expand Down
42 changes: 42 additions & 0 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2195,6 +2195,48 @@ func TestApp_Run_Categories(t *testing.T) {
}
}

func TestApp_Run_SubCommand_Categories(t *testing.T) {
buf := new(bytes.Buffer)

app := &App{
Name: "categories",
HideHelp: true,
Commands: []*Command{
{
Name: "main",
Subcommands: []*Command{
{
Name: "command1",
Category: "CAT1",
},
{
Name: "command2",
Category: "CAT2",
},
{
Name: "command3",
Category: "CAT1",
},
},
},
},
Writer: buf,
}

_ = app.Run([]string{"categories", "main"})

output := buf.String()

if !strings.Contains(output, "CAT1:\n command1") {
t.Errorf("want buffer to include category %q, did not: \n%q", "CAT1:\n command1", output)
}

if !strings.Contains(output, "CAT2:\n command2") {
t.Errorf("want buffer to include category %q, did not: \n%q", "CAT2:\n command2", output)
}

}

func TestApp_VisibleCategories(t *testing.T) {
app := &App{
Name: "visible-categories",
Expand Down
11 changes: 8 additions & 3 deletions docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,14 @@ func prepareFlags(
// flagDetails returns a string containing the flags metadata
func flagDetails(flag DocGenerationFlag) string {
description := flag.GetUsage()
value := flag.GetValue()
if value != "" {
description += " (default: " + value + ")"
if flag.TakesValue() {
defaultText := flag.GetDefaultText()
if defaultText == "" {
defaultText = flag.GetValue()
}
if defaultText != "" {
description += " (default: " + defaultText + ")"
}
}
return ": " + description
}
Expand Down
11 changes: 6 additions & 5 deletions fish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ func testApp() *App {
app.Name = "greet"
app.Flags = []Flag{
&StringFlag{
Name: "socket",
Aliases: []string{"s"},
Usage: "some 'usage' text",
Value: "value",
TakesFile: true,
Name: "socket",
Aliases: []string{"s"},
Usage: "some 'usage' text",
Value: "value",
DefaultText: "/some/path",
TakesFile: true,
},
&StringFlag{Name: "flag", Aliases: []string{"fl", "f"}},
&BoolFlag{
Expand Down
2 changes: 1 addition & 1 deletion godoc-current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ USAGE:
DESCRIPTION:
{{template "descriptionTemplate" .}}{{end}}{{if .VisibleCommands}}

COMMANDS:{{template "visibleCommandTemplate" .}}{{end}}{{if .VisibleFlagCategories}}
COMMANDS:{{template "visibleCommandCategoryTemplate" .}}{{end}}{{if .VisibleFlagCategories}}

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

Expand Down
11 changes: 9 additions & 2 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var helpCommand = &Command{
// 3 $ app foo
// 4 $ app help foo
// 5 $ app foo help
// 6 $ app foo -h (with no other sub-commands nor flags defined)

// Case 4. when executing a help command set the context to parent
// to allow resolution of subsequent args. This will transform
Expand Down Expand Up @@ -77,6 +78,8 @@ var helpCommand = &Command{
HelpPrinter(cCtx.App.Writer, templ, cCtx.Command)
return nil
}

// Case 6, handling incorporated in the callee itself
return ShowSubcommandHelp(cCtx)
},
}
Expand Down Expand Up @@ -292,8 +295,12 @@ func ShowSubcommandHelp(cCtx *Context) error {
if cCtx == nil {
return nil
}

HelpPrinter(cCtx.App.Writer, SubcommandHelpTemplate, cCtx.Command)
// use custom template when provided (fixes #1703)
templ := SubcommandHelpTemplate
if cCtx.Command != nil && cCtx.Command.CustomHelpTemplate != "" {
templ = cCtx.Command.CustomHelpTemplate
}
HelpPrinter(cCtx.App.Writer, templ, cCtx.Command)
return nil
}

Expand Down
23 changes: 23 additions & 0 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,29 @@ func TestHideHelpCommand_WithSubcommands(t *testing.T) {
}
}

func TestHideHelpCommand_RunAsSubcommand_True_CustomTemplate(t *testing.T) {
var buf bytes.Buffer

app := &App{
Writer: &buf,
Commands: []*Command{
{
Name: "dummy",
CustomHelpTemplate: "TEMPLATE",
HideHelpCommand: true,
},
},
}

err := app.RunAsSubcommand(newContextFromStringSlice([]string{"", "dummy", "-h"}))
if err != nil {
t.Errorf("Run returned unexpected error: %v", err)
}
if !strings.Contains(buf.String(), "TEMPLATE") {
t.Errorf("Custom Help template ignored")
}
}

func TestDefaultCompleteWithFlags(t *testing.T) {
origEnv := os.Environ()
origArgv := os.Args
Expand Down
2 changes: 1 addition & 1 deletion template.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ USAGE:
DESCRIPTION:
{{template "descriptionTemplate" .}}{{end}}{{if .VisibleCommands}}
COMMANDS:{{template "visibleCommandTemplate" .}}{{end}}{{if .VisibleFlagCategories}}
COMMANDS:{{template "visibleCommandCategoryTemplate" .}}{{end}}{{if .VisibleFlagCategories}}
OPTIONS:{{template "visibleFlagCategoryTemplate" .}}{{else if .VisibleFlags}}
Expand Down
2 changes: 1 addition & 1 deletion testdata/expected-doc-full.man
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ app [first_arg] [second_arg]
\fB--flag, --fl, -f\fP="":

.PP
\fB--socket, -s\fP="": some 'usage' text (default: value)
\fB--socket, -s\fP="": some 'usage' text (default: /some/path)


.SH COMMANDS
Expand Down
2 changes: 1 addition & 1 deletion testdata/expected-doc-full.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ app [first_arg] [second_arg]

**--flag, --fl, -f**="":

**--socket, -s**="": some 'usage' text (default: value)
**--socket, -s**="": some 'usage' text (default: /some/path)


# COMMANDS
Expand Down
2 changes: 1 addition & 1 deletion testdata/expected-doc-no-authors.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ app [first_arg] [second_arg]

**--flag, --fl, -f**="":

**--socket, -s**="": some 'usage' text (default: value)
**--socket, -s**="": some 'usage' text (default: /some/path)


# COMMANDS
Expand Down
2 changes: 1 addition & 1 deletion testdata/expected-doc-no-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ app [first_arg] [second_arg]

**--flag, --fl, -f**="":

**--socket, -s**="": some 'usage' text (default: value)
**--socket, -s**="": some 'usage' text (default: /some/path)

2 changes: 1 addition & 1 deletion testdata/expected-doc-no-usagetext.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ greet [GLOBAL OPTIONS] command [COMMAND OPTIONS] [ARGUMENTS...]

**--flag, --fl, -f**="":

**--socket, -s**="": some 'usage' text (default: value)
**--socket, -s**="": some 'usage' text (default: /some/path)


# COMMANDS
Expand Down
2 changes: 1 addition & 1 deletion testdata/godoc-v2.x.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ USAGE:
DESCRIPTION:
{{template "descriptionTemplate" .}}{{end}}{{if .VisibleCommands}}

COMMANDS:{{template "visibleCommandTemplate" .}}{{end}}{{if .VisibleFlagCategories}}
COMMANDS:{{template "visibleCommandCategoryTemplate" .}}{{end}}{{if .VisibleFlagCategories}}

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

Expand Down

0 comments on commit d9d9022

Please sign in to comment.