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

How to remove the default use: XXX [command] #1499

Closed
moshangxi opened this issue Sep 30, 2021 · 5 comments
Closed

How to remove the default use: XXX [command] #1499

moshangxi opened this issue Sep 30, 2021 · 5 comments

Comments

@moshangxi
Copy link

This is a problem that has bothered me for a long time. I found some solutions, which are not ideal. Therefore, I want to ask how to solve it: when there are sub commands, the parent command help information will generate XXX [command] by default. for example:

var RootCmd = &cobra.Command{
	Use:   "father",
	Short: "a root command",
	Run: func(command *cobra.Command, args []string) {
		fmt.Println("this is father")
	},
}

var grandson = &cobra.Command{
	Use:   "grandson",
	Short: "a grandson command",
	Run: func(command *cobra.Command, args []string) {
		fmt.Println("this is grandson")
	},
}

var subCmd = &cobra.Command{
	Use:   "child",
	Short: "a sub command",
	Run: func(command *cobra.Command, args []string) {
		fmt.Println("this is child")
	},
}

func init() {
	subCmd.AddCommand(grandson)
	RootCmd.AddCommand(subCmd)
}

func main(){
	if err := RootCmd.Execute(); err != nil {
		os.Exit(1)
	}
}

Input:

$ main.exe -h

Output results:

a root command

Usage:
  father [flags]
  father [command]

Available Commands:
  child       a sub command
  completion  generate the autocompletion script for the specified shell
  help        Help about any command

Flags:
  -h, --help   help for father

Something unexpected has occurred:Usage : father [command]. How do I eliminate it ?

@marckhouzam
Copy link
Collaborator

Hi @moshangxi.

Something unexpected has occurred:Usage : father [command].

Why do you feel this is unexpected? The example code specified that the root command is named father and that it has subcommands, therefore Cobra indicates that the user can call the program using father [command] where the choice of commands is listed in the rest of the help output.

@umarcor
Copy link
Contributor

umarcor commented Oct 2, 2021

There is work in progress in #842 for reworking the usage and the error messages. However, it is on hold because it's based on #841.

@moshangxi
Copy link
Author

Hi @moshangxi.

Something unexpected has occurred:Usage : father [command].

Why do you feel this is unexpected? The example code specified that the root command is named father and that it has subcommands, therefore Cobra indicates that the user can call the program using father [command] where the choice of commands is listed in the rest of the help output.

@moshangxi
Copy link
Author

I want show it like this: Usage : father [command][flages]

@moshangxi moshangxi reopened this Oct 17, 2021
@moshangxi
Copy link
Author

This problem is solved by SetHelpTemplate(template) , see #1505 for more details.

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

No branches or pull requests

3 participants