|
| 1 | +# Command Line Manipulation |
| 2 | + |
| 3 | +Demonstrates how to read or override the raw input command line. |
| 4 | + |
| 5 | +Note that this is *not needed nor recommended* under most circumstances - it |
| 6 | +is provided as an edge case utility. |
| 7 | + |
| 8 | +This example was generated with: |
| 9 | + |
| 10 | +```bash |
| 11 | +$ bashly init --minimal |
| 12 | +$ bashly add hooks |
| 13 | +# ... now edit src/bashly.yml to match the example ... |
| 14 | +# ... now edit src/initialize.sh to match the example ... |
| 15 | +# ... now edit src/before.sh to match the example ... |
| 16 | +$ bashly generate |
| 17 | +``` |
| 18 | + |
| 19 | +<!-- include: src/initialize.sh src/before.sh --> |
| 20 | + |
| 21 | +----- |
| 22 | + |
| 23 | +## `bashly.yml` |
| 24 | + |
| 25 | +````yaml |
| 26 | +name: download |
| 27 | +help: Sample minimal application without commands |
| 28 | +version: 0.1.0 |
| 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 | + |
| 43 | +## `src/initialize.sh` |
| 44 | + |
| 45 | +````bash |
| 46 | +echo "==[ Initialize Called ]==" |
| 47 | + |
| 48 | +# Override the command line completely if the first argument is 'debug' |
| 49 | +if [[ "${command_line_args[0]:-""}" = "debug" ]]; then |
| 50 | + command_line_args=("modified" "args" "--force") |
| 51 | +fi |
| 52 | + |
| 53 | +```` |
| 54 | + |
| 55 | +## `src/before.sh` |
| 56 | + |
| 57 | +````bash |
| 58 | +echo "==[ Before Hook Called ]==" |
| 59 | + |
| 60 | +echo "Read-only copy of the raw input array: ${input[*]}" |
| 61 | +inspect_args |
| 62 | + |
| 63 | +```` |
| 64 | + |
| 65 | + |
| 66 | +## Output |
| 67 | + |
| 68 | +### `$ ./download ` |
| 69 | + |
| 70 | +````shell |
| 71 | +==[ Initialize Called ]== |
| 72 | +missing required argument: SOURCE |
| 73 | +usage: download SOURCE [TARGET] [OPTIONS] |
| 74 | + |
| 75 | + |
| 76 | +```` |
| 77 | + |
| 78 | +### `$ ./download debug` |
| 79 | + |
| 80 | +````shell |
| 81 | +==[ Initialize Called ]== |
| 82 | +==[ Before Hook Called ]== |
| 83 | +Read-only copy of the raw input array: modified args --force |
| 84 | +args: |
| 85 | +- ${args[--force]} = 1 |
| 86 | +- ${args[source]} = modified |
| 87 | +- ${args[target]} = args |
| 88 | +# This file is located at 'src/root_command.sh'. |
| 89 | +# It contains the implementation for the 'download' command. |
| 90 | +# The code you write here will be wrapped by a function named 'download_command()'. |
| 91 | +# Feel free to edit this file; your changes will persist when regenerating. |
| 92 | +args: |
| 93 | +- ${args[--force]} = 1 |
| 94 | +- ${args[source]} = modified |
| 95 | +- ${args[target]} = args |
| 96 | +==[ After Hook Called ]== |
| 97 | + |
| 98 | + |
| 99 | +```` |
| 100 | + |
| 101 | + |
| 102 | + |
0 commit comments