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

Complete hidden commands flags and its subcommands #983

Closed
wants to merge 1 commit into from

Conversation

didrocks
Copy link

@didrocks didrocks commented Oct 25, 2019

This change enable to have a correct completion on hidden commands flags and
its subcommands if present, when using bash.
If an exact hidden command is typed on the command line and then the
user request completion, instead of printing other available subcommand
at the same level (which is thus incorrect), it will print any of its
subcommand if available.
Flags completion request (starting with -) will also work as expected,
as well as flag completion request on any subcommand (persistent flags
or local).

Here is an example file, with 2 commands below the root commands (one shown, one hidden), local and persistent flags, and a subcommand of the hidden command:

package main

import (
	"fmt"
	"os"

	"github.com/spf13/cobra"
)

var (
	rootCmd = &cobra.Command{
		Use: "foo",
		Run: func(cmd *cobra.Command, args []string) {},
	}

	displayed = &cobra.Command{
		Use: "displayed",
		Run: func(cmd *cobra.Command, args []string) {},
	}

	hidden = &cobra.Command{
		Use:    "hidden",
		Hidden: true,
		Run:    func(cmd *cobra.Command, args []string) {},
	}

	subHidden = &cobra.Command{
		Use: "subhidden",
		Run: func(cmd *cobra.Command, args []string) {},
	}
)

func init() {
	rootCmd.AddCommand(displayed, hidden)
	rootCmd.PersistentFlags().Bool("global", false, "")
	rootCmd.Flags().Bool("local-flag-on-root", false, "")

	hidden.AddCommand(subHidden)
	hidden.Flags().Bool("flag-on-hidden", false, "")
	subHidden.Flags().Bool("flag-on-subcmd-subhidden", false, "")
}

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

Before the patch:

$ foo [tab] # becomes "foo displayed"
# the other command is hidden
$ foo subhid[tab][tab] # -> nothing
# the command is hidden
$ foo hidden [tab] # becomes "foo hidden displayed".
# This is  invalid and likely not executable (2 commands of the same level used). The completion is then incorrect.
$ foo hidden --[tab][tab]
--global              --local-flag-on-root  
# persistent and local flag on root proposed. This is incorrect as the local flag on root isn't available on hidden. Local flag on hidden isn't proposed.
$ foo hidden subhidden --[tab][tab]
--global              --local-flag-on-root  
# Same than above. The local flag isn't avilable on subhidden and its local flag isn't proposed.

After the patch:

$ foo [tab] # becomes "foo displayed"
# the other command is hidden
$ foo subhid[tab][tab] # -> nothing
# the command is hidden
$ foo hidden [tab] # becomes "foo hidden subhidden"
# subcommand of hidden command correct completion
$ foo hidden --[tab][tab]
--flag-on-hidden  --global
# persistent flag on root and local flag on hidden command proposed      
$ foo hidden subhidden --[tab][tab]
--flag-on-subcmd-subhidden  --global 
# persistent flag on root and local flag on subhidden proposed      

As you can see, the impacts are only once you fully typed the hidden command, which will then propose the correct subcommand levels and associated flags, and prevent the invalid proposals.

This fixes the "completion part" of bug #981

This change enable to have a correct completion on hidden commands flags and
its subcommands if present, when using bash.
If an exact hidden command is typed on the command line and then the
user request completion, instead of printing other available subcommand
at the same level (which is thus incorrect), it will print any of its
subcommand if available.
Flags completion request (starting with -) will also work as expected,
as well as flag completion request on any subcommand (persistent flags
or local).
@CLAassistant
Copy link

CLAassistant commented Oct 25, 2019

CLA assistant check
All committers have signed the CLA.

didrocks added a commit to ubuntu/zsys that referenced this pull request Oct 30, 2019
We generate those using cobra handlers. Note that:
* we needed to fork the bash completion mechanism to include our fixes
  for hidden commands and file completion to match
  github.com/didrocks/cobra branch-for-completion branch.
  The goal is to fix the completion issues addressed in
  spf13/cobra#983 and file completion if no match
  was found.
  A detailed upstream bug is available at
  spf13/cobra#981.
* we post-process the markdown generation to skip SEE Also and other
  irrelevant sections and add subindentation.
@github-actions
Copy link

github-actions bot commented Apr 3, 2020

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

@github-actions github-actions bot closed this Apr 1, 2022
hoshsadiq pushed a commit to zulucmd/zulu that referenced this pull request Dec 31, 2022
This change enable to have a correct completion on hidden commands flags and
its subcommands if present, when using bash.
If an exact hidden command is typed on the command line and then the
user request completion, instead of printing other available subcommand
at the same level (which is thus incorrect), it will print any of its
subcommand if available.
Flags completion request (starting with -) will also work as expected,
as well as flag completion request on any subcommand (persistent flags
or local).

Merge spf13/cobra#983

Fixes spf13/cobra#981
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.

2 participants