Skip to content

Commit 2c13c5e

Browse files
authored
Merge pull request #202 from DannyBen/add/repeatable-args
Add support for repeatable args
2 parents 011c648 + f6c4797 commit 2c13c5e

File tree

22 files changed

+268
-8
lines changed

22 files changed

+268
-8
lines changed

examples/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ Each of these examples demonstrates one aspect or feature of bashly.
2525
- [extensible](extensible#readme) - letting your script's users extend the script
2626
- [extensible-delegate](extensible-delegate#readme) - extending your script by delegating commands to an external executable
2727
- [whitelist](whitelist#readme) - arguments and flags with a predefined allowed list of values
28-
- [repeatable](repeatable#readme) - allowing flags to be provided multiple times
28+
- [repeatable-arg](repeatable-arg#readme) - allowing args to be provided multiple times
29+
- [repeatable-flag](repeatable-flag#readme) - allowing flags to be provided multiple times
2930
- [conflicts](conflicts#readme) - defining mutually exclusive flags
3031
- [command-private](command-private#readme) - hiding commands from the command list
3132
- [stdin](stdin#readme) - reading input from stdin

examples/repeatable-arg/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
upcase

examples/repeatable-arg/README.md

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Repeatable Argument Example
2+
3+
Demonstrates the use of repeatable arguments that allow users to run commands
4+
such `convert *.png` or `convert 1.png 2.png 3.png`.
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 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: upcase
23+
help: Sample application to demonstrate the use of repeatable arguments
24+
version: 0.1.0
25+
26+
args:
27+
- name: file
28+
help: One or more files to process
29+
required: true
30+
31+
# Setting repeatable to true means that the user can provide multiple arguments
32+
# for it.
33+
# The argument will be received as a quoted and space-delimited string which
34+
# needs to be converted to an array with `eval "data=(${args[file]})"`
35+
repeatable: true
36+
37+
examples:
38+
- upcase README.md LICENSE
39+
- upcase *.md
40+
```
41+
42+
## `src/root_command.sh`
43+
44+
```bash
45+
# Convert the space delimited string to an array
46+
files=''
47+
eval "files=(${args[file]})"
48+
49+
echo
50+
echo "files:"
51+
for i in "${files[@]}"; do
52+
echo " path: $i:"
53+
content="$(cat "$i")"
54+
echo " content: ${content}"
55+
echo " upcase: ${content^^}"
56+
done
57+
58+
echo
59+
inspect_args
60+
61+
```
62+
63+
64+
## Generated script output
65+
66+
### `$ ./upcase -h`
67+
68+
```shell
69+
upcase - Sample application to demonstrate the use of repeatable arguments
70+
71+
Usage:
72+
upcase FILE...
73+
upcase --help | -h
74+
upcase --version | -v
75+
76+
Options:
77+
--help, -h
78+
Show this help
79+
80+
--version, -v
81+
Show version number
82+
83+
Arguments:
84+
FILE...
85+
One or more files to process
86+
87+
Examples:
88+
upcase README.md LICENSE
89+
upcase *.md
90+
91+
92+
93+
```
94+
95+
### `$ ./upcase file1`
96+
97+
```shell
98+
99+
files:
100+
path: file1:
101+
content: content of file1
102+
upcase: CONTENT OF FILE1
103+
104+
args:
105+
- ${args[file]} = "file1"
106+
107+
108+
```
109+
110+
### `$ ./upcase file*`
111+
112+
```shell
113+
114+
files:
115+
path: file1:
116+
content: content of file1
117+
upcase: CONTENT OF FILE1
118+
path: file2:
119+
content: content of file2
120+
upcase: CONTENT OF FILE2
121+
122+
args:
123+
- ${args[file]} = "file1" "file2"
124+
125+
126+
```
127+
128+
129+

examples/repeatable-arg/file1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
content of file1

examples/repeatable-arg/file2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
content of file2
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: upcase
2+
help: Sample application to demonstrate the use of repeatable arguments
3+
version: 0.1.0
4+
5+
args:
6+
- name: file
7+
help: One or more files to process
8+
required: true
9+
10+
# Setting repeatable to true means that the user can provide multiple arguments
11+
# for it.
12+
# The argument will be received as a quoted and space-delimited string which
13+
# needs to be converted to an array with `eval "data=(${args[file]})"`
14+
repeatable: true
15+
16+
examples:
17+
- upcase README.md LICENSE
18+
- upcase *.md
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Convert the space delimited string to an array
2+
files=''
3+
eval "files=(${args[file]})"
4+
5+
echo
6+
echo "files:"
7+
for i in "${files[@]}"; do
8+
echo " path: $i:"
9+
content="$(cat "$i")"
10+
echo " content: ${content}"
11+
echo " upcase: ${content^^}"
12+
done
13+
14+
echo
15+
inspect_args

examples/repeatable-arg/test.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bash
2+
3+
set -x
4+
5+
bashly generate
6+
7+
### Try Me ###
8+
9+
./upcase -h
10+
./upcase file1
11+
./upcase file*
File renamed without changes.

0 commit comments

Comments
 (0)