InitDefaultCompletionCmd adds a default 'completion' command to c. This function will do nothing if any of the following is true: 1- the feature has been explicitly disabled by the program, 2- c has no subcommands (to avoid creating one), 3- c already has a 'completion' command provided by the progr
(args ...string)
| 743 | // 2- c has no subcommands (to avoid creating one), |
| 744 | // 3- c already has a 'completion' command provided by the program. |
| 745 | func (c *Command) InitDefaultCompletionCmd(args ...string) { |
| 746 | if c.CompletionOptions.DisableDefaultCmd { |
| 747 | return |
| 748 | } |
| 749 | |
| 750 | for _, cmd := range c.commands { |
| 751 | if cmd.Name() == compCmdName || cmd.HasAlias(compCmdName) { |
| 752 | // A completion command is already available |
| 753 | return |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | haveNoDescFlag := !c.CompletionOptions.DisableNoDescFlag && !c.CompletionOptions.DisableDescriptions |
| 758 | |
| 759 | // Special case to know if there are sub-commands or not. |
| 760 | hasSubCommands := false |
| 761 | for _, cmd := range c.commands { |
| 762 | if cmd.Name() != ShellCompRequestCmd && cmd.Name() != helpCommandName { |
| 763 | // We found a real sub-command (not 'help' or '__complete') |
| 764 | hasSubCommands = true |
| 765 | break |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | completionCmd := &Command{ |
| 770 | Use: compCmdName, |
| 771 | Short: "Generate the autocompletion script for the specified shell", |
| 772 | Long: fmt.Sprintf(`Generate the autocompletion script for %[1]s for the specified shell. |
| 773 | See each sub-command's help for details on how to use the generated script. |
| 774 | `, c.Root().Name()), |
| 775 | Args: NoArgs, |
| 776 | ValidArgsFunction: NoFileCompletions, |
| 777 | Hidden: c.CompletionOptions.HiddenDefaultCmd, |
| 778 | GroupID: c.completionCommandGroupID, |
| 779 | } |
| 780 | c.AddCommand(completionCmd) |
| 781 | |
| 782 | if !hasSubCommands { |
| 783 | // If the 'completion' command will be the only sub-command, |
| 784 | // we only create it if it is actually being called. |
| 785 | // This avoids breaking programs that would suddenly find themselves with |
| 786 | // a subcommand, which would prevent them from accepting arguments. |
| 787 | // We also create the 'completion' command if the user is triggering |
| 788 | // shell completion for it (prog __complete completion '') |
| 789 | subCmd, cmdArgs, err := c.Find(args) |
| 790 | if err != nil || subCmd.Name() != compCmdName && |
| 791 | (subCmd.Name() != ShellCompRequestCmd || len(cmdArgs) <= 1 || cmdArgs[0] != compCmdName) { |
| 792 | // The completion command is not being called or being completed so we remove it. |
| 793 | c.RemoveCommand(completionCmd) |
| 794 | return |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | out := c.OutOrStdout() |
| 799 | noDesc := c.CompletionOptions.DisableDescriptions |
| 800 | shortDesc := "Generate the autocompletion script for %s" |
| 801 | bash := &Command{ |
| 802 | Use: "bash", |