|
| 1 | +# Repeatable Example |
| 2 | + |
| 3 | +Demonstrates the use of repeatable flags that allow users to run commands such |
| 4 | +as `download -d one -d "two three" -vvv`. |
| 5 | + |
| 6 | +This example was generated with: |
| 7 | + |
| 8 | +```bash |
| 9 | +$ bashly init --minimal |
| 10 | +# ... now edit src/bashly.yml to match the example ... |
| 11 | +# ... now edit src/root_command.sh to match the example ... |
| 12 | +$ bashly generate |
| 13 | +``` |
| 14 | + |
| 15 | +<!-- include: src/root_command.sh --> |
| 16 | + |
| 17 | +----- |
| 18 | + |
| 19 | +## `bashly.yml` |
| 20 | + |
| 21 | +```yaml |
| 22 | +name: download |
| 23 | +help: Sample application to demonstrate the use of repeatable flags |
| 24 | +version: 0.1.0 |
| 25 | + |
| 26 | +flags: |
| 27 | +- long: --data |
| 28 | + short: -d |
| 29 | + arg: data |
| 30 | + help: Provide data values |
| 31 | + required: true |
| 32 | + |
| 33 | + # Setting this to true on a flag with an argument means the user can type it |
| 34 | + # multiple times, like --data a --data b. |
| 35 | + # The argument will be received as a quoted and space-delimited string which |
| 36 | + # needs to be converted to an array with `eval "data=(${args[--data]})"` |
| 37 | + repeatable: true |
| 38 | + |
| 39 | +- long: --verbose |
| 40 | + short: -v |
| 41 | + help: Set verbosity level |
| 42 | + |
| 43 | + # Setting this to true on a regular flag means the user can type it multiple |
| 44 | + # times, in the form of -vvv or -v -v -v. |
| 45 | + # The argument's value will hold the number of times it was entered. |
| 46 | + repeatable: true |
| 47 | + |
| 48 | +examples: |
| 49 | + - download -d one -d "two three" -vvv |
| 50 | +``` |
| 51 | +
|
| 52 | +## `src/root_command.sh` |
| 53 | + |
| 54 | +```bash |
| 55 | +# Convert the space delimited string to an array |
| 56 | +eval "data=(${args[--data]})" |
| 57 | +
|
| 58 | +echo "Data elements:" |
| 59 | +for i in "${data[@]}"; do |
| 60 | + echo "$i" |
| 61 | +done |
| 62 | +
|
| 63 | +# The --verbose arg will contain the number of times it was used by the user |
| 64 | +verbose=${args[--verbose]} |
| 65 | +echo "" |
| 66 | +echo "Verbosity level: $verbose" |
| 67 | +echo "" |
| 68 | +
|
| 69 | +inspect_args |
| 70 | +
|
| 71 | +``` |
| 72 | + |
| 73 | + |
| 74 | +## Generated script output |
| 75 | + |
| 76 | +### `$ ./download -h` |
| 77 | + |
| 78 | +```shell |
| 79 | +download - Sample application to demonstrate use of repeatable flags |
| 80 | +
|
| 81 | +Usage: |
| 82 | + download [options] |
| 83 | + download --help | -h |
| 84 | + download --version |
| 85 | +
|
| 86 | +Options: |
| 87 | + --help, -h |
| 88 | + Show this help |
| 89 | +
|
| 90 | + --version |
| 91 | + Show version number |
| 92 | +
|
| 93 | + --data, -d DATA (required) (repeatable) |
| 94 | + Provide data values |
| 95 | +
|
| 96 | + --verbose, -v (repeatable) |
| 97 | + Set verbosity level |
| 98 | +
|
| 99 | +Examples: |
| 100 | + download -d one -d "two three" -vvv |
| 101 | +
|
| 102 | +
|
| 103 | +
|
| 104 | +``` |
| 105 | + |
| 106 | +### `$ ./download -d one -d "two three" -vvv` |
| 107 | + |
| 108 | +```shell |
| 109 | +Data elements: |
| 110 | +one |
| 111 | +two three |
| 112 | +
|
| 113 | +Verbosity level: 3 |
| 114 | +
|
| 115 | +args: |
| 116 | +- ${args[--data]} = "one" "two three" |
| 117 | +- ${args[--verbose]} = 3 |
| 118 | +
|
| 119 | +
|
| 120 | +``` |
| 121 | + |
| 122 | + |
| 123 | + |
0 commit comments