Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add newline after flags #1515

Closed
wants to merge 2 commits into from
Closed

Add newline after flags #1515

wants to merge 2 commits into from

Conversation

aimichal
Copy link

@aimichal aimichal commented Oct 4, 2022

What type of PR is this?

  • bug

What this PR does / why we need it:

Adds missing newlines after flags in help output.

Which issue(s) this PR fixes:

Fixes #1514

Testing

I made the change locally and recompiled my app and saw the help output has newlines after each flag.

Release Notes

Newlines are added after each flag in help output.

@aimichal aimichal requested a review from a team as a code owner October 4, 2022 16:36
@julian7
Copy link
Contributor

julian7 commented Oct 4, 2022

Hmm, are there non-categorizable flags at all? I mean, all built-in types define GetCategory(), therefore the second "OPTIONS" will hardly ever be hit.

@@ -54,7 +54,8 @@ DESCRIPTION:

OPTIONS:{{range .VisibleFlagCategories}}
{{if .Name}}{{.Name}}
{{end}}{{range .Flags}}{{.}}{{end}}{{end}}{{else}}{{if .VisibleFlags}}
{{end}}{{range .Flags}}{{.}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line results an empty line with three spaces, and two empty lines if there are any options, which breaks tests.

I'm thinking of modifying this template like this:

 OPTIONS:{{range .VisibleFlagCategories}}
    {{if .Name}}{{.Name}}
-   {{end}}{{range .Flags}}{{.}}{{end}}{{end}}{{else}}{{if .VisibleFlags}}
+   {{end}}{{range .Flags}}{{.}}
+{{else}}
+{{end}}{{end}}{{else}}{{if .VisibleFlags}}
 
 OPTIONS:
-   {{range .VisibleFlags}}{{.}}{{end}}{{end}}{{end}}
+   {{range .VisibleFlags}}{{.}}
+{{else}}
+{{end}}{{end}}{{end -}}
 `

This will get rid of the last empty line, and inserts one if there are no flags in the list.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@julian7 Do you run make test after this change ? Since it impacts the unit tests those need to be updated as welll.

Copy link
Contributor

@julian7 julian7 Oct 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did something (which is very similar, probably the same as here), and I ran go test on them until they came back 100% successful. Then, I just copied the git diff portion here :)

This is what I have now (EDIT: added test output):

js@hutton in cli ☸️  yolo on  main [!] via 🐹 v1.19.1 
❯ git show | cat
commit 8335f5435f27584829776f1a34289f0da9a0a0d2
Merge: 6491dde 15b2789
Author: dearchap <dear.chap@gmail.com>
Date:   Wed Oct 5 08:33:35 2022 -0400

    Merge pull request #1513 from abitrolly/patch-1
    
    wrap: Avoid trailing whitespace for empty lines

js@hutton in cli ☸️  yolo on  main [!] via 🐹 v1.19.1 
❯ go test | tail
   help, h  Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --help, -h   show help (default: false)
   --opt value  
   
*cli.stringGeneric 1
*cli.stringGeneric 
PASS
ok      github.com/urfave/cli/v2        0.146s

js@hutton in cli ☸️  yolo on  main [!] via 🐹 v1.19.1
❯ git diff | cat
diff --git help_test.go help_test.go
index d7cdfa2..b0bdb7e 100644
--- help_test.go
+++ help_test.go
@@ -1370,7 +1370,7 @@ OPTIONS:
 `
 
        if output.String() != expected {
-               t.Errorf("Unexpected wrapping, got:\n%s\nexpected:\n%s",
+               t.Errorf("Unexpected wrapping, got:\n%q\nexpected:\n%q",
                        output.String(), expected)
        }
 }
@@ -1439,7 +1439,7 @@ OPTIONS:
 `
 
        if output.String() != expected {
-               t.Errorf("Unexpected wrapping, got:\n%s\nexpected: %s",
+               t.Errorf("Unexpected wrapping, got:\n%q\nexpected:\n%q",
                        output.String(), expected)
        }
 }
diff --git template.go template.go
index 9e13604..142b798 100644
--- template.go
+++ template.go
@@ -54,10 +54,14 @@ DESCRIPTION:
 
 OPTIONS:{{range .VisibleFlagCategories}}
    {{if .Name}}{{.Name}}
-   {{end}}{{range .Flags}}{{.}}{{end}}{{end}}{{else}}{{if .VisibleFlags}}
+   {{end}}{{range .Flags}}{{.}}
+{{else}}
+{{end}}{{end}}{{else}}{{if .VisibleFlags}}
 
 OPTIONS:
-   {{range .VisibleFlags}}{{.}}{{end}}{{end}}{{end}}
+   {{range .VisibleFlags}}{{.}}
+{{else}}
+{{end}}{{end}}{{end -}}
 `
 
 // SubcommandHelpTemplate is the text template for the subcommand help topic.

@aimichal
Copy link
Author

aimichal commented Oct 4, 2022

@julian7 Thanks for asking... But I don't know what "non-categorizable flags" means :-)

I will come up with an isolated example to demonstrate change. I also noticed some of the tests failed in CI. I'll take a look and try to update them or come up with a test to exercise this.

(If this fix is urgent for others, I won't be upset if we close this and go with another change.)

By the way, I believe this is fixed in the previous release, 2.17.0, but I don't understand where the bug is in the diff between that and 2.17.1.

@dearchap
Copy link
Contributor

dearchap commented Oct 5, 2022

@julian7 Yes all the current flags do implement CategoryFlag but its possible that any user created custom flags may not in which case it will hit the second OPTIONS. But yes I agree most common category is "" so if we set that to default if custom flag doesnt implement CategoryFlag then we can get rid of the second OPTIONS.

@dearchap
Copy link
Contributor

dearchap commented Oct 6, 2022

@aimichal Can you check the latest release and see if its fixed the problem ?

@aimichal
Copy link
Author

aimichal commented Oct 7, 2022

@aimichal Can you check the latest release and see if its fixed the problem ?

Unfortunately, not. I encountered this when using 2.17.1. My repro, on MacOS:

package main

import (
	"log"

	"github.com/urfave/cli/v2"
)

func main() {
	app := &cli.App{
		Commands: []*cli.Command{
			{
				Name: "xxx",
				Flags: []cli.Flag{
					&cli.StringFlag{Name: "foo", Value: "foo", Usage: "foo"},
					&cli.StringFlag{Name: "bar", Value: "bar", Usage: "bar"},
				},
			},
		},
	}

	if err := app.Run([]string{"example", "xxx", "--help"}); err != nil {
		log.Fatal(err)
	}
}

Output has:

OPTIONS:
   --bar value  bar (default: "bar")--foo value  foo (default: "foo")

This is confirmed in the playground as well: https://go.dev/play/p/dThmnjhd1U3

@aimichal
Copy link
Author

aimichal commented Oct 7, 2022

On the other hand, using the latest version in main branch (38 commits ahead of latest release, v2.17.1) works fine:

~/code/goplayground  $ go get github.com/urfave/cli/v2@main                                    
go: downloading github.com/urfave/cli/v2 v2.17.2-0.20221006152029-f64acc440459
go: upgraded github.com/urfave/cli/v2 v2.17.1 => v2.17.2-0.20221006152029-f64acc440459
~/code/goplayground  $ go build main.go && ./main                                              
NAME:
   main xxx

USAGE:
   main xxx [command options] [arguments...]

OPTIONS:
   --bar value  bar (default: "bar")
   --foo value  foo (default: "foo")
   --help, -h   show help (default: false)

So I'm closing this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sub command options are printed in single line in help text
3 participants