|
| 1 | +# Advanced Runtime Completions Example |
| 2 | + |
| 3 | +Demonstrates configured runtime completions, including static candidates, |
| 4 | +dynamic external commands and internal functions, file and directory sources, |
| 5 | +and the `no-space` option. |
| 6 | + |
| 7 | +Runtime completions are enabled in `settings.yml`. Users can load the generated |
| 8 | +Bash wrapper with: |
| 9 | + |
| 10 | +```bash |
| 11 | +source <(cli completions) |
| 12 | +``` |
| 13 | + |
| 14 | +<!-- include: settings.yml src/completions_command.sh src/lib/completions.sh --> |
| 15 | + |
| 16 | +----- |
| 17 | + |
| 18 | +## `bashly.yml` |
| 19 | + |
| 20 | +````yaml |
| 21 | +name: cli |
| 22 | +help: Advanced runtime completions example |
| 23 | +version: 0.1.0 |
| 24 | + |
| 25 | +commands: |
| 26 | +- name: completions |
| 27 | + help: Generate a shell completion script |
| 28 | + args: |
| 29 | + - name: shell |
| 30 | + help: Shell to generate completions for |
| 31 | + allowed: [bash] |
| 32 | + default: bash |
| 33 | + |
| 34 | +- name: deploy |
| 35 | + help: Deploy a branch |
| 36 | + args: |
| 37 | + - name: branch |
| 38 | + help: Branch to deploy |
| 39 | + required: true |
| 40 | + |
| 41 | + # Run an external Bash command and add each output line as a candidate. |
| 42 | + completions: |
| 43 | + dynamic: |
| 44 | + - git branch --format='%(refname:short)' |
| 45 | + - name: environment |
| 46 | + help: Environment to deploy to |
| 47 | + |
| 48 | + # Combine literal candidates with an internal function. Prevent the shell |
| 49 | + # from appending a space after the selected completion. |
| 50 | + completions: |
| 51 | + static: [staging, production] |
| 52 | + dynamic: [completion_environments] |
| 53 | + options: [no-space] |
| 54 | + flags: |
| 55 | + - long: --config |
| 56 | + arg: file |
| 57 | + help: Deployment configuration file |
| 58 | + |
| 59 | + # Ask the shell to add file and directory candidates. |
| 60 | + completions: |
| 61 | + options: [files] |
| 62 | + - long: --directory |
| 63 | + arg: path |
| 64 | + help: Deployment directory |
| 65 | + |
| 66 | + # Ask the shell to add directory candidates only. |
| 67 | + completions: |
| 68 | + options: [directories] |
| 69 | +```` |
| 70 | +## `settings.yml` |
| 71 | + |
| 72 | +````yaml |
| 73 | +enable_completions: always |
| 74 | + |
| 75 | +```` |
| 76 | + |
| 77 | +## `src/completions_command.sh` |
| 78 | + |
| 79 | +````bash |
| 80 | +send_completions "${args[shell]}" |
| 81 | + |
| 82 | +```` |
| 83 | + |
| 84 | +## `src/lib/completions.sh` |
| 85 | + |
| 86 | +````bash |
| 87 | +completion_environments() { |
| 88 | + printf 'development\nstaging\n' |
| 89 | +} |
| 90 | + |
| 91 | +```` |
| 92 | + |
| 93 | + |
| 94 | +## Output |
| 95 | + |
| 96 | +### `$ ./cli completions | head -n3` |
| 97 | + |
| 98 | +````shell |
| 99 | +_cli_completions() { |
| 100 | + local completion_command="${COMP_WORDS[0]}" |
| 101 | + local completion_current="${COMP_WORDS[COMP_CWORD]:-}" |
| 102 | + |
| 103 | + |
| 104 | +```` |
| 105 | + |
| 106 | +### `$ ./cli __complete ""` |
| 107 | + |
| 108 | +````shell |
| 109 | +completions |
| 110 | +deploy |
| 111 | +:options= |
| 112 | + |
| 113 | + |
| 114 | +```` |
| 115 | + |
| 116 | +### `$ ./cli __complete deploy main st` |
| 117 | + |
| 118 | +````shell |
| 119 | +staging |
| 120 | +:options=no-space |
| 121 | + |
| 122 | + |
| 123 | +```` |
| 124 | + |
| 125 | +### `$ ./cli __complete deploy --config ""` |
| 126 | + |
| 127 | +````shell |
| 128 | +:options=files |
| 129 | + |
| 130 | + |
| 131 | +```` |
| 132 | + |
| 133 | +### `$ ./cli __complete deploy --directory ""` |
| 134 | + |
| 135 | +````shell |
| 136 | +:options=directories |
| 137 | + |
| 138 | + |
| 139 | +```` |
0 commit comments