Skip to content

Commit 737173a

Browse files
authored
Merge pull request #926 from rsteube/standalone-prevent-carapace
standalone: prevent local `_carapace` completion
2 parents a3d566d + 7d16449 commit 737173a

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

carapace.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,19 @@ func (c Carapace) FlagCompletion(actions ActionMap) {
8585
}
8686
}
8787

88+
const annotation_standalone = "carapace_standalone"
89+
8890
// Standalone prevents cobra defaults interfering with standalone mode (e.g. implicit help command).
8991
func (c Carapace) Standalone() {
9092
c.cmd.CompletionOptions = cobra.CompletionOptions{
9193
DisableDefaultCmd: true,
9294
}
9395

96+
if c.cmd.Annotations == nil {
97+
c.cmd.Annotations = make(map[string]string)
98+
}
99+
c.cmd.Annotations[annotation_standalone] = "true"
100+
94101
c.PreRun(func(cmd *cobra.Command, args []string) {
95102
if f := cmd.Flag("help"); f == nil {
96103
cmd.Flags().Bool("help", false, "")

command.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,16 @@ func addCompletionCommand(cmd *cobra.Command) {
3232
panic("missing parent command") // this should never happen
3333
}
3434

35-
if s, err := complete(cmd.Parent(), args); err != nil {
36-
fmt.Fprintln(io.MultiWriter(cmd.OutOrStderr(), LOG.Writer()), err.Error())
35+
parentCmd := cmd.Parent()
36+
if parentCmd.Annotations[annotation_standalone] == "true" {
37+
// TODO how to handle an explicit `_carapace` command?
38+
parentCmd.RemoveCommand(cmd) // don't complete local `_carapace` in standalone mode
39+
}
40+
41+
if s, err := complete(parentCmd, args); err != nil {
42+
fmt.Fprintln(io.MultiWriter(parentCmd.OutOrStderr(), LOG.Writer()), err.Error())
3743
} else {
38-
fmt.Fprintln(io.MultiWriter(cmd.OutOrStdout(), LOG.Writer()), s)
44+
fmt.Fprintln(io.MultiWriter(parentCmd.OutOrStdout(), LOG.Writer()), s)
3945
}
4046
},
4147
FParseErrWhitelist: cobra.FParseErrWhitelist{

0 commit comments

Comments
 (0)