Skip to content

Commit 7ec6748

Browse files
committed
readme update for completions
1 parent 6708dd2 commit 7ec6748

File tree

7 files changed

+100
-13
lines changed

7 files changed

+100
-13
lines changed

README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Create beautiful bash scripts from simple YAML configuration
3131
- [Flag options](#flag-options)
3232
- [Environment Variable options](#environment-variable-options)
3333
- [Extensible Scripts](#extensible-scripts)
34+
- [Bash Completions](#bash-completions)
3435
- [Real World Examples](#real-world-examples)
3536
- [Contributing / Support](#contributing--support)
3637

@@ -75,13 +76,15 @@ Bahsly is responsible for:
7576
- Optional or required **option flags** (with or without flag arguments).
7677
- **Commands** (and subcommands).
7778
- Standard flags (like **--help** and **--version**).
79+
- Preventing your script from running unless the command line is valid.
7880
- Providing you with a place to input your code for each of the functions
7981
your tool performs, and merging it back to the final script.
8082
- Providing you with additional (optional) framework-style, standard
8183
library functions:
8284
- **Color output**.
8385
- **Config file management** (INI format).
8486
- **YAML parsing**.
87+
- **Bash completions**.
8588
- and more.
8689

8790

@@ -198,6 +201,7 @@ command and subcommands (under the `commands` definition).
198201
`commands` | Specify the array of [commands](#command-options). Each command will have its own args and flags. Note: if `commands` is provided, you cannot specify flags or args at the same level.
199202
`args` | Specify the array of [positional arguments](#argument-options) this script needs.
200203
`flags` | Specify the array of option [flags](#flag-options) this script needs.
204+
`completions` | Specify an array of additional completion suggestions when used in conjunction with `bashly add comp`. See [Bash Completions](#bash-completions).
201205
`catch_all` | Specify that this command should allow for additional arbitrary arguments or flags. It can be set in one of three ways:<br>- Set to `true` to just enable it.<br>- Set to a string, to use this string in the usage help text.<br>- Set to a hash containing `label` and `help` keys, to show a detailed help for it when running with `--help`.
202206
`dependencies` | Specify an array of any required external dependencies (commands). The script execution will be halted with a friendly error unless all dependency commands exist.
203207
`group` | In case you have many commands, use this option to specify a caption to display before this command. This option is purely for display purposes, and needs to be specified only for the first command in each group.
@@ -326,6 +330,71 @@ The generated script will execute `git status`.
326330
See the [extensible-delegate example](examples/extensible-delegate).
327331

328332

333+
## Bash Completions
334+
335+
Bashly comes with built-in bash completions generator, provided by the
336+
[completely][completely] gem.
337+
338+
By running any of the `bashly add comp` commands, you can add this
339+
functionality to your script in one of three ways:
340+
341+
- `bashly add comp function` - creates a function in your `./src/lib` directory
342+
that echoes a completion script. You can then call this function from any
343+
command (for example `yourcli completions`) and your users will be able to
344+
install the completions by running `eval "$(yourcli completions)"`.
345+
- `bashly add comp script` - creates a standalone completion script that can be
346+
sourced or copies to the system's bash completions directory.
347+
- `bashly add comp yaml` - creates the "raw data" YAML file. This is intended
348+
mainly for development purposes.
349+
350+
The bash completions generation is completely automatic, and you will have to
351+
rerun the `bashly add comp *` command whenever you change your `bashly.yml`
352+
script.
353+
354+
In addition to suggesting subcommands and flags, you can instruct bashly to
355+
also suggest files, directories, users and more. To do this, add another option
356+
in your `bashly.yml` on the command you wish to alter:
357+
358+
```yaml
359+
# bashly.yml
360+
commands:
361+
- name: upload
362+
help: Upload a file
363+
completions: [directory, user]
364+
365+
```
366+
367+
Valid completion additions are:
368+
369+
| Keyword | Meaning
370+
|-------------|---------------------
371+
| `alias` | Alias names
372+
| `arrayvar` | Array variable names
373+
| `binding` | Readline key binding names
374+
| `builtin` | Names of shell builtin commands
375+
| `command` | Command names
376+
| `directory` | Directory names
377+
| `disabled` | Names of disabled shell builtins
378+
| `enabled` | Names of enabled shell builtins
379+
| `export` | Names of exported shell variables
380+
| `file` | File names
381+
| `function` | Names of shell functions
382+
| `group` | Group names
383+
| `helptopic` | Help topics as accepted by the help builtin
384+
| `hostname` | Hostnames, as taken from the file specified by the HOSTFILE shell variable
385+
| `job` | Job names
386+
| `keyword` | Shell reserved words
387+
| `running` | Names of running jobs
388+
| `service` | Service names
389+
| `signal` | Signal names
390+
| `stopped` | Names of stopped jobs
391+
| `user` | User names
392+
| `variable` | Names of all shell variables
393+
394+
Note that these are taken from the [Programmable Completion Builtin][compgen],
395+
and will simply be added using the `compgen -A action` command.
396+
397+
329398
## Real World Examples
330399

331400
- [Rush][rush] - a Personal Package Manager
@@ -344,3 +413,5 @@ to contribute, feel free to [open an issue][issues].
344413
[rush]: https://github.com/DannyBen/rush-cli
345414
[alf]: https://github.com/DannyBen/alf
346415
[git-changelog]: https://github.com/DannyBen/git-changelog
416+
[completely]: https://github.com/DannyBen/completely
417+
[compgen]: https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion-Builtins.html

examples/completions/README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ commands:
2626
- name: download
2727
short: d
2828
help: Download a file
29+
completions: [file]
2930

3031
args:
3132
- name: source
@@ -50,6 +51,7 @@ commands:
5051
- name: upload
5152
short: u
5253
help: Upload a file
54+
completions: [directory, user]
5355
args:
5456
- name: source
5557
required: true
@@ -146,9 +148,10 @@ _cli_completions() {
146148
local cur=${COMP_WORDS[COMP_CWORD]}
147149
148150
case "$COMP_LINE" in
149-
'cli download'*) COMPREPLY=($(compgen -W "--force --help -f -h" -- "$cur")) ;;
150-
'cli upload'*) COMPREPLY=($(compgen -W "--help --password --user -h -p -u" -- "$cur")) ;;
151-
'cli'*) COMPREPLY=($(compgen -W "--help --version -h -v download upload" -- "$cur")) ;;
151+
'cli completions'*) COMPREPLY=($(compgen -W "--help -h" -- "$cur")) ;;
152+
'cli download'*) COMPREPLY=($(compgen -A file -W "--force --help -f -h" -- "$cur")) ;;
153+
'cli upload'*) COMPREPLY=($(compgen -A directory -A user -W "--help --password --user -h -p -u" -- "$cur")) ;;
154+
'cli'*) COMPREPLY=($(compgen -W "--help --version -h -v completions download upload" -- "$cur")) ;;
152155
esac
153156
}
154157

examples/completions/cli

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,10 @@ send_completions() {
204204
echo $' local cur=${COMP_WORDS[COMP_CWORD]}'
205205
echo $''
206206
echo $' case "$COMP_LINE" in'
207-
echo $' \'cli download\'*) COMPREPLY=($(compgen -W "--force --help -f -h" -- "$cur")) ;;'
208-
echo $' \'cli upload\'*) COMPREPLY=($(compgen -W "--help --password --user -h -p -u" -- "$cur")) ;;'
209-
echo $' \'cli\'*) COMPREPLY=($(compgen -W "--help --version -h -v download upload" -- "$cur")) ;;'
207+
echo $' \'cli completions\'*) COMPREPLY=($(compgen -W "--help -h" -- "$cur")) ;;'
208+
echo $' \'cli download\'*) COMPREPLY=($(compgen -A file -W "--force --help -f -h" -- "$cur")) ;;'
209+
echo $' \'cli upload\'*) COMPREPLY=($(compgen -A directory -A user -W "--help --password --user -h -p -u" -- "$cur")) ;;'
210+
echo $' \'cli\'*) COMPREPLY=($(compgen -W "--help --version -h -v completions download upload" -- "$cur")) ;;'
210211
echo $' esac'
211212
echo $'}'
212213
echo $''

examples/completions/src/bashly.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ commands:
1111
- name: download
1212
short: d
1313
help: Download a file
14+
completions: [file]
1415

1516
args:
1617
- name: source
@@ -35,6 +36,7 @@ commands:
3536
- name: upload
3637
short: u
3738
help: Upload a file
39+
completions: [directory, user]
3840
args:
3941
- name: source
4042
required: true

examples/completions/src/lib/send_completions.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ send_completions() {
88
echo $' local cur=${COMP_WORDS[COMP_CWORD]}'
99
echo $''
1010
echo $' case "$COMP_LINE" in'
11-
echo $' \'cli download\'*) COMPREPLY=($(compgen -W "--force --help -f -h" -- "$cur")) ;;'
12-
echo $' \'cli upload\'*) COMPREPLY=($(compgen -W "--help --password --user -h -p -u" -- "$cur")) ;;'
13-
echo $' \'cli\'*) COMPREPLY=($(compgen -W "--help --version -h -v download upload" -- "$cur")) ;;'
11+
echo $' \'cli completions\'*) COMPREPLY=($(compgen -W "--help -h" -- "$cur")) ;;'
12+
echo $' \'cli download\'*) COMPREPLY=($(compgen -A file -W "--force --help -f -h" -- "$cur")) ;;'
13+
echo $' \'cli upload\'*) COMPREPLY=($(compgen -A directory -A user -W "--help --password --user -h -p -u" -- "$cur")) ;;'
14+
echo $' \'cli\'*) COMPREPLY=($(compgen -W "--help --version -h -v completions download upload" -- "$cur")) ;;'
1415
echo $' esac'
1516
echo $'}'
1617
echo $''

examples/completions/test.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
set -x
44

5+
bashly add comp function
56
bashly generate
67

78
./cli
89
./cli -h
910
./cli completions -h
10-
./cli completions
11+
./cli completions

spec/approvals/examples/completions

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
+ bashly add comp function
2+
created src/lib/send_completions.sh
3+
4+
In order to use it in your script, create a command or a flag (for example: cli completions or cli --completions) that calls the send_completions function.
5+
Your users can then run something like this to enable completions:
6+
7+
$ eval "$(cli completions)"
18
+ bashly generate
29
creating user files in src
310
skipped src/initialize.sh (exists)
@@ -63,9 +70,10 @@ _cli_completions() {
6370
local cur=${COMP_WORDS[COMP_CWORD]}
6471

6572
case "$COMP_LINE" in
66-
'cli download'*) COMPREPLY=($(compgen -W "--force --help -f -h" -- "$cur")) ;;
67-
'cli upload'*) COMPREPLY=($(compgen -W "--help --password --user -h -p -u" -- "$cur")) ;;
68-
'cli'*) COMPREPLY=($(compgen -W "--help --version -h -v download upload" -- "$cur")) ;;
73+
'cli completions'*) COMPREPLY=($(compgen -W "--help -h" -- "$cur")) ;;
74+
'cli download'*) COMPREPLY=($(compgen -A file -W "--force --help -f -h" -- "$cur")) ;;
75+
'cli upload'*) COMPREPLY=($(compgen -A directory -A user -W "--help --password --user -h -p -u" -- "$cur")) ;;
76+
'cli'*) COMPREPLY=($(compgen -W "--help --version -h -v completions download upload" -- "$cur")) ;;
6977
esac
7078
}
7179

0 commit comments

Comments
 (0)