Skip to content

Commit

Permalink
Merge pull request #43 from criteo/custom-auto-complete-directive
Browse files Browse the repository at this point in the history
Custom auto complete directive
  • Loading branch information
ride2ubaye authored Jun 7, 2022
2 parents 5c325e4 + 070217a commit 02222d0
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,15 +323,25 @@ func addCommands(groups []command.Command, executables []command.Command) {
parts := strings.Split(output, "\n")
if len(parts) > 0 {
if strings.HasPrefix(parts[0], "#") { // skip the first control line, for further controls
return parts[1:], cobra.ShellCompDirectiveNoFileComp
// the first line starting with # is the control line, it controls the completion behavior when the return body is empty
shellDirective := cobra.ShellCompDirectiveNoFileComp
switch strings.TrimSpace(strings.TrimLeft(parts[0], "#")) {
case "dir-completion-only":
shellDirective = cobra.ShellCompDirectiveFilterDirs
case "default":
shellDirective = cobra.ShellCompDirectiveDefault
case "no-file-completion":
shellDirective = cobra.ShellCompDirectiveNoFileComp
}
return parts[1:], shellDirective
}
return parts, cobra.ShellCompDirectiveNoFileComp
}
}
if len(validArgs) > 0 {
return validArgs, cobra.ShellCompDirectiveNoFileComp
}
return []string{}, cobra.ShellCompDirectiveNoFileComp
return []string{}, cobra.ShellCompDirectiveDefault
}

if v.Group() == "" {
Expand Down

0 comments on commit 02222d0

Please sign in to comment.