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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix documentation inconsistencies #2043

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion command.go
Expand Up @@ -568,7 +568,7 @@ Additional Commands:{{range $cmds}}{{if (and (eq .GroupID "") (or .IsAvailableCo
Flags:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}

Global Flags:
Flags inherited from parent commands:
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not a fan of this change: while you're technically correct (what we're calling global flags are just flags inherited from the top level command), in spirit, they are global flags.

This would be a pretty confusing change for people who've used cobra for years.

Copy link
Author

Choose a reason for hiding this comment

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

They are not necessarily inherited from top level command, just a parent command, which makes the "Global Flags" statement even more confusing when having multiple levels of sub commands.

I would argue that it's more confusing calling them something they really are not, i.e. "Global Flags", then calling them what they actually are, technically and factually. Whether they may be global "in spirit" or not, I think actual usage is more important, and, at least from our point of view, they are not treated as global for sure.

I guess a compromise would be something like "Global flags inherited from parent command" or something like that.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmmm that's a very fair point: this is probably one of those legacy inconsistencies that has popped up as we've expanded the command / flag system. It's not really global, like you said, since it is inherited from some parent.

Anyone else have opinions here.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The point is valid but I don鈥檛 think it justifies changing a formulation that tools have been using for years.

In fact, I wonder if many tools aren鈥檛 putting the inherited flags mostly on the root command which would make them global flags so although this printout is not technically correct, it is in practice the right thing, and what existing tools are expecting

Copy link
Author

Choose a reason for hiding this comment

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

It's fair to say that this change would be somewhat of an inconvenience for developers using the tool and the cost may be a bit high for this change. I'll revert the print out because of that in this PR.

I think it's good though to consider that application developers aren't the end users for cobra. The end users are the ones actually using the application and the ones that would actually read the help documentation. In our case, the "Global Flags" print has actually caused a bit of confusion.

Anyway, dealing with this slight inconsistency between markdown and help func is the point of this PR and I'll revert it back for this. How this is worded would probably a discussion to have at some other point.

{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}

Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
Expand Down
2 changes: 1 addition & 1 deletion command_test.go
Expand Up @@ -921,7 +921,7 @@ Flags:
--foo child foo usage
-h, --help help for child

Global Flags:
Flags inherited from parent commands:
--bar parent bar usage
`

Expand Down
4 changes: 2 additions & 2 deletions doc/md_docs.go
Expand Up @@ -31,15 +31,15 @@ func printOptions(buf *bytes.Buffer, cmd *cobra.Command, name string) error {
flags := cmd.NonInheritedFlags()
flags.SetOutput(buf)
if flags.HasAvailableFlags() {
buf.WriteString("### Options\n\n```\n")
buf.WriteString("### Flags\n\n```\n")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Changing Options to Flags in the markdown docs seems fine. Although this introduces another inconsistency between what we internally call "options" in the docs generation. I.e., look at what we do for yaml docs:

InheritedOptions []cmdOption `yaml:"inherited_options,omitempty"`

so this makes me abit hesitant since that struct is a public API and I would rather not change that too

Copy link
Author

Choose a reason for hiding this comment

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

Yes, I see there are more inconsistencies there, but I think it would be best to handle those in another branch. Especially when there's a function that may need to change.

Copy link
Author

Choose a reason for hiding this comment

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

I also discovered that the inconsistencies are present in the man tree generation as well:

cobra/doc/man_docs.go

Lines 187 to 200 in a0a6ae0

func manPrintOptions(buf io.StringWriter, command *cobra.Command) {
flags := command.NonInheritedFlags()
if flags.HasAvailableFlags() {
cobra.WriteStringAndCheck(buf, "# OPTIONS\n")
manPrintFlags(buf, flags)
cobra.WriteStringAndCheck(buf, "\n")
}
flags = command.InheritedFlags()
if flags.HasAvailableFlags() {
cobra.WriteStringAndCheck(buf, "# OPTIONS INHERITED FROM PARENT COMMANDS\n")
manPrintFlags(buf, flags)
cobra.WriteStringAndCheck(buf, "\n")
}
}

flags.PrintDefaults()
buf.WriteString("```\n\n")
}

parentFlags := cmd.InheritedFlags()
parentFlags.SetOutput(buf)
if parentFlags.HasAvailableFlags() {
buf.WriteString("### Options inherited from parent commands\n\n```\n")
buf.WriteString("### Flags inherited from parent commands\n\n```\n")
parentFlags.PrintDefaults()
buf.WriteString("```\n\n")
}
Expand Down
6 changes: 3 additions & 3 deletions doc/md_docs_test.go
Expand Up @@ -39,7 +39,7 @@ func TestGenMdDoc(t *testing.T) {
checkStringContains(t, output, rootCmd.Short)
checkStringContains(t, output, echoSubCmd.Short)
checkStringOmits(t, output, deprecatedCmd.Short)
checkStringContains(t, output, "Options inherited from parent commands")
checkStringContains(t, output, "Flags inherited from parent commands")
}

func TestGenMdDocWithNoLongOrSynopsis(t *testing.T) {
Expand All @@ -52,7 +52,7 @@ func TestGenMdDocWithNoLongOrSynopsis(t *testing.T) {

checkStringContains(t, output, dummyCmd.Example)
checkStringContains(t, output, dummyCmd.Short)
checkStringContains(t, output, "Options inherited from parent commands")
checkStringContains(t, output, "Flags inherited from parent commands")
checkStringOmits(t, output, "### Synopsis")
}

Expand All @@ -76,7 +76,7 @@ func TestGenMdNoHiddenParents(t *testing.T) {
checkStringContains(t, output, rootCmd.Short)
checkStringContains(t, output, echoSubCmd.Short)
checkStringOmits(t, output, deprecatedCmd.Short)
checkStringOmits(t, output, "Options inherited from parent commands")
checkStringOmits(t, output, "Flags inherited from parent commands")
}

func TestGenMdNoTag(t *testing.T) {
Expand Down