Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: urfave/cli
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.19.1
Choose a base ref
...
head repository: urfave/cli
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v2.19.2
Choose a head ref
  • 7 commits
  • 11 files changed
  • 2 contributors

Commits on Oct 6, 2022

  1. fix: stop automatic sorting for --help

    FGYFFFF authored and dearchap committed Oct 6, 2022
    Copy the full SHA
    1ada1a1 View commit details

Commits on Oct 7, 2022

  1. Fix tests

    dearchap committed Oct 7, 2022
    Copy the full SHA
    02613e5 View commit details
  2. Run make v2approve

    dearchap committed Oct 7, 2022
    Copy the full SHA
    e62a087 View commit details
  3. Copy the full SHA
    8227be1 View commit details
  4. Refactor code

    dearchap committed Oct 7, 2022
    Copy the full SHA
    81f9145 View commit details

Commits on Oct 9, 2022

  1. Copy the full SHA
    bffaf3b View commit details
  2. Merge pull request #1430 from FGYFFFF/fix/stop_auto_sort_for_help

    fix: stop automatic sorting for --help
    dearchap authored Oct 9, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    f37b9d9 View commit details
Showing with 94 additions and 31 deletions.
  1. +3 −1 app.go
  2. +28 −5 app_test.go
  3. +3 −1 category.go
  4. +1 −6 command.go
  5. +35 −0 command_test.go
  6. +1 −1 docs/v2/examples/flags.md
  7. +4 −2 godoc-current.txt
  8. +1 −5 help.go
  9. +8 −4 help_test.go
  10. +6 −4 template.go
  11. +4 −2 testdata/godoc-v2.x.txt
4 changes: 3 additions & 1 deletion app.go
Original file line number Diff line number Diff line change
@@ -229,7 +229,9 @@ func (a *App) Setup() {
a.flagCategories = newFlagCategories()
for _, fl := range a.Flags {
if cf, ok := fl.(CategorizableFlag); ok {
a.flagCategories.AddFlag(cf.GetCategory(), cf)
if cf.GetCategory() != "" {
a.flagCategories.AddFlag(cf.GetCategory(), cf)
}
}
}

33 changes: 28 additions & 5 deletions app_test.go
Original file line number Diff line number Diff line change
@@ -144,8 +144,8 @@ func ExampleApp_Run_appHelp() {
// help, h Shows a list of commands or help for one command
//
// GLOBAL OPTIONS:
// --help, -h show help (default: false)
// --name value a name to say (default: "bob")
// --help, -h show help (default: false)
// --version, -v print the version (default: false)
}

@@ -177,7 +177,7 @@ func ExampleApp_Run_commandHelp() {
// greet describeit - use it to see a description
//
// USAGE:
// greet describeit [arguments...]
// greet describeit [command options] [arguments...]
//
// DESCRIPTION:
// This is how we describe describeit the function
@@ -2242,10 +2242,33 @@ func TestApp_VisibleCategories(t *testing.T) {
}

func TestApp_VisibleFlagCategories(t *testing.T) {
app := &App{}
app := &App{
Flags: []Flag{
&StringFlag{
Name: "strd", // no category set
},
&Int64Flag{
Name: "intd",
Aliases: []string{"altd1", "altd2"},
Category: "cat1",
},
},
}
app.Setup()
vfc := app.VisibleFlagCategories()
if len(vfc) != 0 {
t.Errorf("unexpected visible flag categories %+v", vfc)
if len(vfc) != 1 {
t.Fatalf("unexpected visible flag categories %+v", vfc)
}
if vfc[0].Name() != "cat1" {
t.Errorf("expected category name cat1 got %s", vfc[0].Name())
}
if len(vfc[0].Flags()) != 1 {
t.Fatalf("expected flag category to have just one flag got %+v", vfc[0].Flags())
}

fl := vfc[0].Flags()[0]
if !reflect.DeepEqual(fl.Names(), []string{"intd", "altd1", "altd2"}) {
t.Errorf("unexpected flag %+v", fl.Names())
}
}

4 changes: 3 additions & 1 deletion category.go
Original file line number Diff line number Diff line change
@@ -102,7 +102,9 @@ func newFlagCategoriesFromFlags(fs []Flag) FlagCategories {
fc := newFlagCategories()
for _, fl := range fs {
if cf, ok := fl.(CategorizableFlag); ok {
fc.AddFlag(cf.GetCategory(), cf)
if cf.GetCategory() != "" {
fc.AddFlag(cf.GetCategory(), cf)
}
}
}

7 changes: 1 addition & 6 deletions command.go
Original file line number Diff line number Diff line change
@@ -309,12 +309,7 @@ func (c *Command) VisibleCommands() []*Command {
// VisibleFlagCategories returns a slice containing all the visible flag categories with the flags they contain
func (c *Command) VisibleFlagCategories() []VisibleFlagCategory {
if c.flagCategories == nil {
c.flagCategories = newFlagCategories()
for _, fl := range c.Flags {
if cf, ok := fl.(CategorizableFlag); ok {
c.flagCategories.AddFlag(cf.GetCategory(), cf)
}
}
c.flagCategories = newFlagCategoriesFromFlags(c.Flags)
}
return c.flagCategories.VisibleCategories()
}
35 changes: 35 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ import (
"flag"
"fmt"
"io/ioutil"
"reflect"
"strings"
"testing"
)
@@ -449,3 +450,37 @@ func TestCommand_VisibleSubcCommands(t *testing.T) {

expect(t, c.VisibleCommands(), []*Command{subc1, subc3})
}

func TestCommand_VisibleFlagCategories(t *testing.T) {

c := &Command{
Name: "bar",
Usage: "this is for testing",
Flags: []Flag{
&StringFlag{
Name: "strd", // no category set
},
&Int64Flag{
Name: "intd",
Aliases: []string{"altd1", "altd2"},
Category: "cat1",
},
},
}

vfc := c.VisibleFlagCategories()
if len(vfc) != 1 {
t.Fatalf("unexpected visible flag categories %+v", vfc)
}
if vfc[0].Name() != "cat1" {
t.Errorf("expected category name cat1 got %s", vfc[0].Name())
}
if len(vfc[0].Flags()) != 1 {
t.Fatalf("expected flag category to have just one flag got %+v", vfc[0].Flags())
}

fl := vfc[0].Flags()[0]
if !reflect.DeepEqual(fl.Names(), []string{"intd", "altd1", "altd2"}) {
t.Errorf("unexpected flag %+v", fl.Names())
}
}
2 changes: 1 addition & 1 deletion docs/v2/examples/flags.md
Original file line number Diff line number Diff line change
@@ -239,7 +239,7 @@ For example this:

<!-- {
"args": ["&#45;&#45;help"],
"output": ".*Load configuration from FILE\n.*\n.*Language for the greeting.*"
"output": ".*Load configuration from FILE\n.*Language for the greeting.*"
} -->
```go
package main
6 changes: 4 additions & 2 deletions godoc-current.txt
Original file line number Diff line number Diff line change
@@ -73,7 +73,8 @@ DESCRIPTION:

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

OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}`
OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}
`
CommandHelpTemplate is the text template for the command help topic. cli.go
uses text/template to render templates. You can render custom help text by
setting this variable.
@@ -144,7 +145,8 @@ COMMANDS:{{template "visibleCommandTemplate" .}}{{end}}{{if .VisibleFlagCategori

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

OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}`
OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}
`
SubcommandHelpTemplate is the text template for the subcommand help topic.
cli.go uses text/template to render templates. You can render custom help
text by setting this variable.
6 changes: 1 addition & 5 deletions help.go
Original file line number Diff line number Diff line change
@@ -242,11 +242,7 @@ func ShowCommandHelp(ctx *Context, command string) error {
c.Subcommands = append(c.Subcommands, helpCommandDontUse)
}
if !ctx.App.HideHelp && HelpFlag != nil {
if c.flagCategories == nil {
c.flagCategories = newFlagCategoriesFromFlags([]Flag{HelpFlag})
} else {
c.flagCategories.AddFlag("", HelpFlag)
}
c.appendFlag(HelpFlag)
}
templ := c.CustomHelpTemplate
if templ == "" {
12 changes: 8 additions & 4 deletions help_test.go
Original file line number Diff line number Diff line change
@@ -1366,7 +1366,8 @@ DESCRIPTION:
case
OPTIONS:
--help, -h show help (default: false)
--help, -h show help
(default: false)
`

if output.String() != expected {
@@ -1435,7 +1436,8 @@ USAGE:
even more
OPTIONS:
--help, -h show help (default: false)
--help, -h show help
(default: false)
`

if output.String() != expected {
@@ -1510,8 +1512,10 @@ USAGE:
even more
OPTIONS:
--help, -h show help (default: false)
--test-f value my test usage
--test-f value my test
usage
--help, -h show help
(default: false)
`

if output.String() != expected {
10 changes: 6 additions & 4 deletions template.go
Original file line number Diff line number Diff line change
@@ -18,8 +18,8 @@ var visibleFlagCategoryTemplate = `{{range .VisibleFlagCategories}}
{{else}}{{$e}}
{{end}}{{end}}{{end}}`

var visibleFlagTemplate = `{{range $index, $option := .VisibleFlags}}{{if $index}}{{end}}
{{wrap $option.String 6}}{{end}}`
var visibleFlagTemplate = `{{range $i, $e := .VisibleFlags}}
{{wrap $e.String 6}}{{end}}`

var versionTemplate = `{{if .Version}}{{if not .HideVersion}}
@@ -73,7 +73,8 @@ DESCRIPTION:
OPTIONS:{{template "visibleFlagCategoryTemplate" .}}{{else if .VisibleFlags}}
OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}`
OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}
`

// SubcommandHelpTemplate is the text template for the subcommand help topic.
// cli.go uses text/template to render templates. You can
@@ -91,7 +92,8 @@ COMMANDS:{{template "visibleCommandTemplate" .}}{{end}}{{if .VisibleFlagCategori
OPTIONS:{{template "visibleFlagCategoryTemplate" .}}{{else if .VisibleFlags}}
OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}`
OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}
`

var MarkdownDocTemplate = `{{if gt .SectionNum 0}}% {{ .App.Name }} {{ .SectionNum }}
6 changes: 4 additions & 2 deletions testdata/godoc-v2.x.txt
Original file line number Diff line number Diff line change
@@ -73,7 +73,8 @@ DESCRIPTION:

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

OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}`
OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}
`
CommandHelpTemplate is the text template for the command help topic. cli.go
uses text/template to render templates. You can render custom help text by
setting this variable.
@@ -144,7 +145,8 @@ COMMANDS:{{template "visibleCommandTemplate" .}}{{end}}{{if .VisibleFlagCategori

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

OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}`
OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}
`
SubcommandHelpTemplate is the text template for the subcommand help topic.
cli.go uses text/template to render templates. You can render custom help
text by setting this variable.