Skip to content

Commit

Permalink
standalone: prevent local _carapace completion
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Oct 2, 2023
1 parent 0e0f241 commit 13d84d1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 5 additions & 0 deletions carapace.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ func (c Carapace) Standalone() {
DisableDefaultCmd: true,
}

if c.cmd.Annotations == nil {
c.cmd.Annotations = make(map[string]string)
}
c.cmd.Annotations["carapace_standalone"] = "true"

c.PreRun(func(cmd *cobra.Command, args []string) {
if f := cmd.Flag("help"); f == nil {
cmd.Flags().Bool("help", false, "")
Expand Down
7 changes: 6 additions & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ func addCompletionCommand(cmd *cobra.Command) {
panic("missing parent command") // this should never happen
}

if s, err := complete(cmd.Parent(), args); err != nil {
parentCmd := cmd.Parent()
if parentCmd.Annotations["carapace_standalone"] == "true" {
parentCmd.RemoveCommand(cmd)
}

if s, err := complete(parentCmd, args); err != nil {
fmt.Fprintln(io.MultiWriter(cmd.OutOrStderr(), LOG.Writer()), err.Error())
} else {
fmt.Fprintln(io.MultiWriter(cmd.OutOrStdout(), LOG.Writer()), s)
Expand Down

0 comments on commit 13d84d1

Please sign in to comment.