Skip to content

Commit 388ab19

Browse files
committed
- Add Command Line Manipulation example
1 parent d2d7ba0 commit 388ab19

File tree

10 files changed

+177
-0
lines changed

10 files changed

+177
-0
lines changed
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[0]:-""}" = "debug" ]]; then
50+
command_line=("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[0]:-""}" = "debug" ]]; then
5+
command_line=("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

lib/bashly/views/command/initialize.gtx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
> {{ Settings.function_name :initialize }}() {
44
> declare -g version="<%= version %>"
5+
> declare -g command_line
56
> {{ Settings.strict_string }}
67

78
if root_command?
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
+ bashly generate
2+
creating user files in src
3+
skipped src/root_command.sh (exists)
4+
created ./download
5+
run ./download --help to test your bash script
6+
+ ./download
7+
==[ Initialize Called ]==
8+
missing required argument: SOURCE
9+
usage: download SOURCE [TARGET] [OPTIONS]
10+
+ ./download debug
11+
==[ Initialize Called ]==
12+
==[ Before Hook Called ]==
13+
Read-only copy of the raw input array: modified args --force
14+
args:
15+
- ${args[--force]} = 1
16+
- ${args[source]} = modified
17+
- ${args[target]} = args
18+
# This file is located at 'src/root_command.sh'.
19+
# It contains the implementation for the 'download' command.
20+
# The code you write here will be wrapped by a function named 'download_command()'.
21+
# Feel free to edit this file; your changes will persist when regenerating.
22+
args:
23+
- ${args[--force]} = 1
24+
- ${args[source]} = modified
25+
- ${args[target]} = args
26+
==[ After Hook Called ]==

0 commit comments

Comments
 (0)