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

Proposal: Make command suggestion messages configurable #1394

Open
MarvinJWendt opened this issue May 10, 2021 · 4 comments
Open

Proposal: Make command suggestion messages configurable #1394

MarvinJWendt opened this issue May 10, 2021 · 4 comments
Labels
area/docs-generation Generation of docs via Cobra kind/feature A feature request for cobra; new or enhanced behavior lifecycle/needs-pr Ready for a PR from the community

Comments

@MarvinJWendt
Copy link

MarvinJWendt commented May 10, 2021

Hi, we have a project where clear definitions were made, what the output of the CLI has to look like. We appreciate the automatic suggestions if a command was misspelled. Sadly it seems like we cannot customize the suggestion message and the default message doesn't really fit with the rest of the CLI.

Is there a possibility to make a SetSuggestFunc() like it's been done with SetHelpFunc()?

This could be used to change the suggestion message:

rootCmd.SetSuggestFunc(func(suggestedCommands []*cobra.Command) {
    var commandNames string
    for _, command := range suggestedCommands {
        	commandNames += command.Use + " "
    }
    fmt.Printf("Did you mean any of [%s]", commandNames)
})
@github-actions
Copy link

This issue is being marked as stale due to a long period of inactivity

@MarvinJWendt
Copy link
Author

Not stale

@johnSchnake johnSchnake added kind/feature A feature request for cobra; new or enhanced behavior area/docs-generation Generation of docs via Cobra lifecycle/needs-pr Ready for a PR from the community and removed kind/stale labels Mar 16, 2022
@johnSchnake
Copy link
Collaborator

Seems narrowly focused, clear, and consistent with other features we've implemented. 👍

@zanvd
Copy link

zanvd commented Dec 2, 2024

Hi, I started working on this one and wonder if it's fine to make the suggestion function extendable just on the command.go level and leave the wrappers as are.
The wrappers are:

With leaving wrappers intact, the message would be something along the lines of:

Error: unknown command "something" for "root"\nArg: "something"\nRun 'root --help' for usage.\n

Custom suggestion function used for the above message:

rootCmd.SetSuggestFunc(func(a string) string {
	return fmt.Sprintf("\nArg: %q", a)
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/docs-generation Generation of docs via Cobra kind/feature A feature request for cobra; new or enhanced behavior lifecycle/needs-pr Ready for a PR from the community
Projects
None yet
Development

No branches or pull requests

3 participants