File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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).
8991func (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 , "" )
Original file line number Diff line number Diff 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 {
You can’t perform that action at this time.
0 commit comments