|
| 1 | +# Key Value Pairs Example |
| 2 | + |
| 3 | +Demonstrates how to implement support for a command line that accepts |
| 4 | +`key=value` pairs, either as positional arguments of repeatable flags. |
| 5 | + |
| 6 | +This example was generated with: |
| 7 | + |
| 8 | +```bash |
| 9 | +$ bashly init |
| 10 | +# ... now edit src/bashly.yml to match the example ... |
| 11 | +# ... now create and edit src/lib/extract_params.sh to match the example ... |
| 12 | +$ bashly generate |
| 13 | +# ... now edit src/root_command.sh ... |
| 14 | +$ bashly generate |
| 15 | +``` |
| 16 | + |
| 17 | +See the [src/root_command.sh](src/root_command.sh) for usage example. |
| 18 | + |
| 19 | + |
| 20 | +<!-- include: src/root_command.sh src/lib/extract_params.sh --> |
| 21 | + |
| 22 | +----- |
| 23 | + |
| 24 | +## `bashly.yml` |
| 25 | + |
| 26 | +````yaml |
| 27 | +name: cli |
| 28 | +help: Sample application to demonstrate `key=value` arguments and flags |
| 29 | +version: 0.1.0 |
| 30 | + |
| 31 | +args: |
| 32 | +- name: param |
| 33 | + required: true |
| 34 | + repeatable: true |
| 35 | + unique: true |
| 36 | + help: key=value parameters |
| 37 | + |
| 38 | +flags: |
| 39 | +- long: --option |
| 40 | + short: -o |
| 41 | + arg: option |
| 42 | + repeatable: true |
| 43 | + unique: true |
| 44 | + help: key=value parameters |
| 45 | + |
| 46 | +examples: |
| 47 | + - cli user=bob [email protected] --option page=2 --option count=20 |
| 48 | +```` |
| 49 | + |
| 50 | +## `src/root_command.sh` |
| 51 | + |
| 52 | +````bash |
| 53 | +extract_params "${args[param]}" |
| 54 | + |
| 55 | +echo "Positional Arguments:" |
| 56 | +for key in "${!params[@]}"; do |
| 57 | + echo "$key => ${params[$key]}" |
| 58 | +done |
| 59 | + |
| 60 | +extract_params "${args[--option]}" |
| 61 | + |
| 62 | +echo |
| 63 | +echo "Options (--option):" |
| 64 | +for key in "${!params[@]}"; do |
| 65 | + echo "$key => ${params[$key]}" |
| 66 | +done |
| 67 | + |
| 68 | +```` |
| 69 | + |
| 70 | +## `src/lib/extract_params.sh` |
| 71 | + |
| 72 | +````bash |
| 73 | +## Convert an array of key=value pairs to an associative array. |
| 74 | +## Calling this function will populate an associative array named `params`. |
| 75 | +function extract_params { |
| 76 | + declare -gA params=() |
| 77 | + declare -a list=() |
| 78 | + local pattern="([^=]+)=(.+)" |
| 79 | + eval "list=($*)" |
| 80 | + |
| 81 | + for pair in "${list[@]}"; do |
| 82 | + if [[ $pair =~ $pattern ]]; then |
| 83 | + key="${BASH_REMATCH[1]}" |
| 84 | + value="${BASH_REMATCH[2]}" |
| 85 | + params["$key"]="$value" |
| 86 | + fi |
| 87 | + done |
| 88 | +} |
| 89 | +```` |
| 90 | + |
| 91 | + |
| 92 | +## Output |
| 93 | + |
| 94 | +### `$ ./cli -h` |
| 95 | + |
| 96 | +````shell |
| 97 | +cli - Sample application to demonstrate `key=value` arguments and flags |
| 98 | + |
| 99 | +Usage: |
| 100 | + cli PARAM... [OPTIONS] |
| 101 | + cli --help | -h |
| 102 | + cli --version | -v |
| 103 | + |
| 104 | +Options: |
| 105 | + --option, -o OPTION (repeatable) |
| 106 | + key=value parameters |
| 107 | + |
| 108 | + --help, -h |
| 109 | + Show this help |
| 110 | + |
| 111 | + --version, -v |
| 112 | + Show version number |
| 113 | + |
| 114 | +Arguments: |
| 115 | + PARAM... |
| 116 | + key=value parameters |
| 117 | + |
| 118 | +Examples: |
| 119 | + cli user=bob [email protected] --option page=2 --option count=20 |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | +```` |
| 124 | + |
| 125 | +### `$ ./cli user=bob [email protected] --option page=2 --option count=20` |
| 126 | + |
| 127 | +````shell |
| 128 | +Positional Arguments: |
| 129 | + |
| 130 | +user => bob |
| 131 | + |
| 132 | +Options (--option): |
| 133 | +count => 20 |
| 134 | +page => 2 |
| 135 | + |
| 136 | + |
| 137 | +```` |
| 138 | + |
| 139 | + |
| 140 | + |
0 commit comments