feat: make InitDefaultCompletionCmd public

This commit is contained in:
Gustavo Bazan 2021-08-03 12:37:19 +01:00
parent 56060d19f8
commit 53a147170c
No known key found for this signature in database
GPG key ID: D02D8BF462322985
7 changed files with 8 additions and 4 deletions

View file

@ -930,7 +930,7 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
// initialize help at the last point to allow for user overriding // initialize help at the last point to allow for user overriding
c.InitDefaultHelpCmd() c.InitDefaultHelpCmd()
// initialize completion at the last point to allow for user overriding // initialize completion at the last point to allow for user overriding
c.initDefaultCompletionCmd() c.InitDefaultCompletionCmd()
args := c.args args := c.args

View file

@ -568,12 +568,12 @@ func checkIfFlagCompletion(finalCmd *Command, args []string, lastArg string) (*p
return flag, trimmedArgs, lastArg, nil return flag, trimmedArgs, lastArg, nil
} }
// initDefaultCompletionCmd adds a default 'completion' command to c. // InitDefaultCompletionCmd adds a default 'completion' command to c.
// This function will do nothing if any of the following is true: // This function will do nothing if any of the following is true:
// 1- the feature has been explicitly disabled by the program, // 1- the feature has been explicitly disabled by the program,
// 2- c has no subcommands (to avoid creating one), // 2- c has no subcommands (to avoid creating one),
// 3- c already has a 'completion' command provided by the program. // 3- c already has a 'completion' command provided by the program.
func (c *Command) initDefaultCompletionCmd() { func (c *Command) InitDefaultCompletionCmd() {
if c.CompletionOptions.DisableDefaultCmd || !c.HasSubCommands() { if c.CompletionOptions.DisableDefaultCmd || !c.HasSubCommands() {
return return
} }

View file

@ -201,6 +201,7 @@ func manPrintOptions(buf io.StringWriter, command *cobra.Command) {
func genMan(cmd *cobra.Command, header *GenManHeader) []byte { func genMan(cmd *cobra.Command, header *GenManHeader) []byte {
cmd.InitDefaultHelpCmd() cmd.InitDefaultHelpCmd()
cmd.InitDefaultHelpFlag() cmd.InitDefaultHelpFlag()
cmd.InitDefaultCompletionCmd()
// something like `rootcmd-subcmd1-subcmd2` // something like `rootcmd-subcmd1-subcmd2`
dashCommandName := strings.Replace(cmd.CommandPath(), " ", "-", -1) dashCommandName := strings.Replace(cmd.CommandPath(), " ", "-", -1)

View file

@ -131,7 +131,7 @@ func TestGenManSeeAlso(t *testing.T) {
if err := assertNextLineEquals(scanner, ".PP"); err != nil { if err := assertNextLineEquals(scanner, ".PP"); err != nil {
t.Fatalf("First line after SEE ALSO wasn't break-indent: %v", err) t.Fatalf("First line after SEE ALSO wasn't break-indent: %v", err)
} }
if err := assertNextLineEquals(scanner, `\fBroot\-bbb(1)\fP, \fBroot\-ccc(1)\fP`); err != nil { if err := assertNextLineEquals(scanner, `\fBroot\-bbb(1)\fP, \fBroot\-ccc(1)\fP, \fBroot\-completion(1)\fP`); err != nil {
t.Fatalf("Second line after SEE ALSO wasn't correct: %v", err) t.Fatalf("Second line after SEE ALSO wasn't correct: %v", err)
} }
} }

View file

@ -54,6 +54,7 @@ func GenMarkdown(cmd *cobra.Command, w io.Writer) error {
func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) error { func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) error {
cmd.InitDefaultHelpCmd() cmd.InitDefaultHelpCmd()
cmd.InitDefaultHelpFlag() cmd.InitDefaultHelpFlag()
cmd.InitDefaultCompletionCmd()
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
name := cmd.CommandPath() name := cmd.CommandPath()

View file

@ -61,6 +61,7 @@ func GenReST(cmd *cobra.Command, w io.Writer) error {
func GenReSTCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string, string) string) error { func GenReSTCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string, string) string) error {
cmd.InitDefaultHelpCmd() cmd.InitDefaultHelpCmd()
cmd.InitDefaultHelpFlag() cmd.InitDefaultHelpFlag()
cmd.InitDefaultCompletionCmd()
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
name := cmd.CommandPath() name := cmd.CommandPath()

View file

@ -92,6 +92,7 @@ func GenYaml(cmd *cobra.Command, w io.Writer) error {
func GenYamlCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) error { func GenYamlCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) error {
cmd.InitDefaultHelpCmd() cmd.InitDefaultHelpCmd()
cmd.InitDefaultHelpFlag() cmd.InitDefaultHelpFlag()
cmd.InitDefaultCompletionCmd()
yamlDoc := cmdDoc{} yamlDoc := cmdDoc{}
yamlDoc.Name = cmd.CommandPath() yamlDoc.Name = cmd.CommandPath()