@@ -24,6 +24,7 @@ name: cli
2424help : Sample application with bash completions
2525version : 0.1.0
2626
27+ # All commands and flags will be automatically used in the completions script
2728commands :
2829- name : completions
2930 help : |-
@@ -33,6 +34,9 @@ commands:
3334 - name : download
3435 alias : d
3536 help : Download a file
37+
38+ # Adding custom completions for a command. In this case, typing
39+ # `cli download <TAB>` will suggest files.
3640 completions :
3741 - <file>
3842
@@ -47,6 +51,14 @@ commands:
4751 - long : --force
4852 short : -f
4953 help : Overwrite existing files
54+ - long : --handler
55+ arg : command
56+
57+ # The allowed flag arg whitelist will be added automatically. In this case,
58+ # typing `cli download --handler <tab>` will suggest curl or wget
59+ allowed :
60+ - curl
61+ - wget
5062
5163 examples :
5264 - cli download example.com
@@ -59,20 +71,36 @@ commands:
5971- name : upload
6072 alias : u
6173 help : Upload a file
74+
75+ # Add directories and users to the suggested completions.
6276 completions :
6377 - <directory>
6478 - <user>
79+
6580 args :
6681 - name : source
6782 required : true
6883 help : File to upload
6984
85+ # The allowed argument whitelist will be added automatically. In this case
86+ # typing `cli upload <TAB>` will suggest these files.
87+ allowed :
88+ - README.md
89+ - CHANGELOG.md
90+
7091 flags :
7192 - long : --user
7293 short : -u
7394 arg : user
7495 help : Username to use for logging in
7596 required : true
97+
98+ # Adding completions to a flag with an argument will add it to the suggested
99+ # words list. In this case typing `cli upload --user <TAB>` will suggest
100+ # users.
101+ completions :
102+ - <user>
103+
76104 - long : --password
77105 short : -p
78106 arg : password
@@ -165,25 +193,37 @@ Options:
165193### ` $ ./cli completions `
166194
167195``` shell
168- #! /usr/bin/env bash
196+ # cli completion -*- shell-script -*-
169197
170198# This bash completions script was generated by
171199# completely (https://github.com/dannyben/completely)
172200# Modifying it manually is not recommended
173- _cli_completions () {
174- local cur=${COMP_WORDS[COMP_CWORD]}
175- local comp_line=" ${COMP_WORDS[@]: 1} "
176201
177- case " $comp_line " in
202+ _cli_completions () {
203+ local cur compline
204+ _init_completion -s || return
205+ cur=${COMP_WORDS[COMP_CWORD]}
206+ compline=" ${COMP_WORDS[@]: 1: $COMP_CWORD -1} "
207+
208+ case " $compline " in
209+ ' download' * ' --handler' ) COMPREPLY=($( compgen -W " curl wget" -- " $cur " ) ) ;;
210+ ' upload' * ' --user' ) COMPREPLY=($( compgen -A user -- " $cur " ) ) ;;
178211 ' completions' * ) COMPREPLY=($( compgen -W " --help -h" -- " $cur " ) ) ;;
179- ' download' * ) COMPREPLY=($( compgen -A file -W " --force --help -f -h" -- " $cur " ) ) ;;
180- ' upload' * ) COMPREPLY=($( compgen -A directory -A user -W " --help --password --user -h -p -u" -- " $cur " ) ) ;;
181- ' ' * ) COMPREPLY=($( compgen -W " --help --version -h -v completions download upload" -- " $cur " ) ) ;;
212+ ' d' * ' --handler' ) COMPREPLY=($( compgen -W " curl wget" -- " $cur " ) ) ;;
213+ ' upload' * ' -u' ) COMPREPLY=($( compgen -A user -- " $cur " ) ) ;;
214+ ' download' * ) COMPREPLY=($( compgen -A file -W " --force --handler --help -f -h" -- " $cur " ) ) ;;
215+ ' u' * ' --user' ) COMPREPLY=($( compgen -A user -- " $cur " ) ) ;;
216+ ' upload' * ) COMPREPLY=($( compgen -A directory -A user -W " --help --password --user -h -p -u CHANGELOG.md README.md" -- " $cur " ) ) ;;
217+ ' u' * ' -u' ) COMPREPLY=($( compgen -A user -- " $cur " ) ) ;;
218+ ' d' * ) COMPREPLY=($( compgen -A file -W " --force --handler --help -f -h" -- " $cur " ) ) ;;
219+ ' u' * ) COMPREPLY=($( compgen -A directory -A user -W " --help --password --user -h -p -u CHANGELOG.md README.md" -- " $cur " ) ) ;;
220+ * ) COMPREPLY=($( compgen -W " --help --version -h -v completions d download u upload" -- " $cur " ) ) ;;
182221 esac
183- }
184-
222+ } &&
185223complete -F _cli_completions cli
186224
225+ # ex: filetype=sh
226+
187227
188228```
189229
0 commit comments