|
| 1 | +# Bash Completions Example |
| 2 | + |
| 3 | +Demonstrates how to build a script that supports bash completions. |
| 4 | + |
| 5 | +This example was generated with: |
| 6 | + |
| 7 | + $ bashly init |
| 8 | + $ bashly add comp function |
| 9 | + $ bashly generate |
| 10 | + |
| 11 | +----- |
| 12 | + |
| 13 | +## `bashly.yml` |
| 14 | + |
| 15 | +```yaml |
| 16 | +name: cli |
| 17 | +help: Sample application with bash completions |
| 18 | +version: 0.1.0 |
| 19 | + |
| 20 | +commands: |
| 21 | +- name: completions |
| 22 | + help: |- |
| 23 | + Generate bash completions |
| 24 | + Usage: eval "\$(cli completions)" |
| 25 | +
|
| 26 | +- name: download |
| 27 | + short: d |
| 28 | + help: Download a file |
| 29 | + |
| 30 | + args: |
| 31 | + - name: source |
| 32 | + required: true |
| 33 | + help: URL to download from |
| 34 | + - name: target |
| 35 | + help: "Target filename (default: same as source)" |
| 36 | + |
| 37 | + flags: |
| 38 | + - long: --force |
| 39 | + short: -f |
| 40 | + help: Overwrite existing files |
| 41 | + |
| 42 | + examples: |
| 43 | + - cli download example.com |
| 44 | + - cli download example.com ./output -f |
| 45 | + |
| 46 | + environment_variables: |
| 47 | + - name: default_target_location |
| 48 | + help: Set the default location to download to |
| 49 | + |
| 50 | +- name: upload |
| 51 | + short: u |
| 52 | + help: Upload a file |
| 53 | + args: |
| 54 | + - name: source |
| 55 | + required: true |
| 56 | + help: File to upload |
| 57 | + |
| 58 | + flags: |
| 59 | + - long: --user |
| 60 | + short: -u |
| 61 | + arg: user |
| 62 | + help: Username to use for logging in |
| 63 | + required: true |
| 64 | + - long: --password |
| 65 | + short: -p |
| 66 | + arg: password |
| 67 | + help: Password to use for logging in |
| 68 | +``` |
| 69 | +
|
| 70 | +## Generated script output |
| 71 | +
|
| 72 | +### `$ ./cli` |
| 73 | + |
| 74 | +```shell |
| 75 | +cli - Sample application with bash completions |
| 76 | +
|
| 77 | +Usage: |
| 78 | + cli [command] |
| 79 | + cli [command] --help | -h |
| 80 | + cli --version | -v |
| 81 | +
|
| 82 | +Commands: |
| 83 | + completions Generate bash completions |
| 84 | + download Download a file |
| 85 | + upload Upload a file |
| 86 | +
|
| 87 | +
|
| 88 | +
|
| 89 | +``` |
| 90 | + |
| 91 | +### `$ ./cli -h` |
| 92 | + |
| 93 | +```shell |
| 94 | +cli - Sample application with bash completions |
| 95 | +
|
| 96 | +Usage: |
| 97 | + cli [command] |
| 98 | + cli [command] --help | -h |
| 99 | + cli --version | -v |
| 100 | +
|
| 101 | +Commands: |
| 102 | + completions Generate bash completions |
| 103 | + download Download a file |
| 104 | + upload Upload a file |
| 105 | +
|
| 106 | +Options: |
| 107 | + --help, -h |
| 108 | + Show this help |
| 109 | +
|
| 110 | + --version, -v |
| 111 | + Show version number |
| 112 | +
|
| 113 | +
|
| 114 | +
|
| 115 | +``` |
| 116 | + |
| 117 | +### `$ ./cli completions -h` |
| 118 | + |
| 119 | +```shell |
| 120 | +cli completions |
| 121 | +
|
| 122 | + Generate bash completions |
| 123 | + Usage: eval "$(cli completions)" |
| 124 | +
|
| 125 | +Usage: |
| 126 | + cli completions |
| 127 | + cli completions --help | -h |
| 128 | +
|
| 129 | +Options: |
| 130 | + --help, -h |
| 131 | + Show this help |
| 132 | +
|
| 133 | +
|
| 134 | +
|
| 135 | +``` |
| 136 | + |
| 137 | +### `$ ./cli completions` |
| 138 | + |
| 139 | +```shell |
| 140 | +#!/usr/bin/env bash |
| 141 | +
|
| 142 | +# This bash completions script was generated by |
| 143 | +# completely (https://github.com/dannyben/completely) |
| 144 | +# Modifying it manually is not recommended |
| 145 | +_cli_completions() { |
| 146 | + local cur=${COMP_WORDS[COMP_CWORD]} |
| 147 | +
|
| 148 | + 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")) ;; |
| 152 | + esac |
| 153 | +} |
| 154 | +
|
| 155 | +complete -F _cli_completions cli |
| 156 | +
|
| 157 | +
|
| 158 | +``` |
| 159 | + |
| 160 | + |
| 161 | + |
0 commit comments