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

Hide auto-generated completion command #1507

Closed
blacktop opened this issue Oct 18, 2021 · 7 comments · Fixed by #1541
Closed

Hide auto-generated completion command #1507

blacktop opened this issue Oct 18, 2021 · 7 comments · Fixed by #1541
Milestone

Comments

@blacktop
Copy link

Is there a way to hide the completion cmd? I like having it, but don't want it to show up in --help

Thanks!

@marckhouzam
Copy link
Collaborator

Hi @blacktop, there is currently no way to hide the completion command provided by Cobra. You would need to define your own completion and mark it hidden.

I think it would be nice to add such an option to Cobra.

@marcellmars
Copy link

marcellmars commented Oct 29, 2021

one of the ways to keep completion in as a command but only hide it from the help/usage output could be by using SetHelpTemplate or SetUsageTemplate...

for example if you just copy the template from command.go and paste it into your custom template. just change what is in the line 501:

Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}

...into this1:

Available commands:
{{range .Commands}}
  {{if and (ne .Name "completion") .IsAvailableCommand}}
    {{rpad .Name .NamePadding }} {{.Short}}{{end}}
   {{end}}
 {{end}}
 {{if .HasAvailableLocalFlags}}

it should hide completion from listing it under "Available commands:"..

Footnotes

  1. here I also got rid off the help as a [command] and unfolded the change into multiline snippet just here on github so it shows better what's done there...

@jpmcb
Copy link
Collaborator

jpmcb commented Nov 1, 2021

@marckhouzam - is this something we could tackle in the next release? That would be great

@jpmcb jpmcb added this to the Next milestone Nov 1, 2021
@marckhouzam
Copy link
Collaborator

I was hoping to make the command public so that people could adapt it any way they want, but IIRC it is not that easy.

I'll try again and if I can't figure it out, I'll add an new completionOption to allow hiding the command.

Glad to have you back @jpmcb 😄

marckhouzam added a commit to VilledeMontreal/cobra that referenced this issue Nov 3, 2021
Fixes spf13#1507

This allows a program to set whatever field it wants on the default
completion command before calling rootCmd.Execute().  For example,

  cobra.CompletionCmd.Hidden = true  // To make the command hidden

or/and

  cobra.CompletionCmd.Use = "shellcomplete" // To rename the command

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
marckhouzam added a commit to VilledeMontreal/cobra that referenced this issue Nov 3, 2021
Fixes spf13#1507

This allows a program to set whatever field it wants on the default
completion command before calling rootCmd.Execute().  For example,

  cobra.CompletionCmd.Hidden = true  // To make the command hidden

or/and

  cobra.CompletionCmd.Use = "shellcomplete" // To rename the command

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
marckhouzam added a commit to VilledeMontreal/cobra that referenced this issue Nov 3, 2021
Fixes spf13#1507

This allows a program to set whatever field it wants on the default
completion command before calling rootCmd.Execute().  For example,

  cobra.CompletionCmd.Hidden = true  // To make the command hidden

or/and

  cobra.CompletionCmd.Use = "shellcomplete" // To rename the command

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
marckhouzam added a commit to VilledeMontreal/cobra that referenced this issue Nov 24, 2021
Fixes spf13#1507

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
umarcor pushed a commit to umarcor/cobra that referenced this issue Nov 24, 2021
Fixes spf13#1507

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
umarcor pushed a commit to umarcor/cobra that referenced this issue Nov 25, 2021
Fixes spf13#1507

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
umarcor pushed a commit to umarcor/cobra that referenced this issue Nov 25, 2021
Fixes spf13#1507

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
umarcor pushed a commit to umarcor/cobra that referenced this issue Dec 7, 2021
Fixes spf13#1507

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
jpmcb pushed a commit that referenced this issue Dec 7, 2021
Fixes #1507

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
@zliang90
Copy link

zliang90 commented Jul 12, 2023

@blacktop i rewrite the completion command, can hide the completion subcommand, code examples:

func completionCommand() *cobra.Command {
	return &cobra.Command{
		Use:   "completion",
		Short: "Generate the autocompletion script for the specified shell",
	}
}

func init() {
	completion := completionCommand()

       // mark completion hidden
	completion.Hidden = true
	rootCommand.AddCommand(completion)

        // other subcommands
	rootCommand.AddCommand(...)
}

@abs007
Copy link

abs007 commented Jan 8, 2024

@marckhouzam Are there new docs explaining how to hide the "completion" sub-command?

AFAI can see, the line: rootCmd.Root().CompletionOptions.DisableDefaultCmd = true needs to be added to the rootCmd's init() func

@marckhouzam
Copy link
Collaborator

@abs007 here you go: https://github.com/spf13/cobra/blob/main/site/content/completions/_index.md#adapting-the-default-completion-command

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