Skip to content

Commit dfa1a2b

Browse files
authored
Merge pull request #654 from bashly-framework/add/input-override
Add ability to override raw input
2 parents 9e85ff9 + 4048afc commit dfa1a2b

File tree

25 files changed

+231
-32
lines changed

25 files changed

+231
-32
lines changed

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Each of these examples demonstrates one aspect or feature of bashly.
4343
- [key-value-pairs](key-value-pairs#readme) - parsing key=value arguments and flags
4444
- [command-examples-on-error](command-examples-on-error#readme) - showing examples on error
4545
- [internal-run](internal-run#readme) - calling other commands internally
46+
- [command-line-manipulation](command-line-manipulation#readme) - read or modify the raw command line
4647

4748
## Customization
4849

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
download
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## after hook
2+
##
3+
## Any code here will be placed inside an `after_hook()` function and called
4+
## after running any command.
5+
##
6+
## You can safely delete this file if you do not need it.
7+
echo "==[ After Hook Called ]=="
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: download
2+
help: Sample minimal application without commands
3+
version: 0.1.0
4+
5+
args:
6+
- name: source
7+
required: true
8+
help: URL to download from
9+
- name: target
10+
help: "Target filename (default: same as source)"
11+
12+
flags:
13+
- long: --force
14+
short: -f
15+
help: Overwrite existing files
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
echo "==[ Before Hook Called ]=="
2+
3+
echo "Read-only copy of the raw input array: ${input[*]}"
4+
inspect_args
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
echo "==[ Initialize Called ]=="
2+
3+
# Override the command line completely if the first argument is 'debug'
4+
if [[ "${command_line_args[0]:-""}" = "debug" ]]; then
5+
command_line_args=("modified" "args" "--force")
6+
fi
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
echo "# This file is located at 'src/root_command.sh'."
2+
echo "# It contains the implementation for the 'download' command."
3+
echo "# The code you write here will be wrapped by a function named 'download_command()'."
4+
echo "# Feel free to edit this file; your changes will persist when regenerating."
5+
inspect_args
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
3+
set -x
4+
5+
bashly generate
6+
7+
### Try Me ###
8+
9+
./download
10+
./download debug

examples/dependencies-alt/README.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,6 @@ commands:
5252
### `$ ./cli download`
5353

5454
````shell
55-
# This file is located at 'src/download_command.sh'.
56-
# It contains the implementation for the 'cli download' command.
57-
# The code you write here will be wrapped by a function named 'cli_download_command()'.
58-
# Feel free to edit this file; your changes will persist when regenerating.
59-
args: none
60-
61-
deps:
62-
- ${deps[git]} = /usr/bin/git
63-
- ${deps[http_client]} = /usr/bin/curl
64-
- ${deps[ruby]} = /home/vagrant/.rbenv/versions/3.4.1/bin/ruby
6555

6656

6757
````

0 commit comments

Comments
 (0)