Skip to content

Commit d77067b

Browse files
committed
add repeatable args example
1 parent 74f0ce3 commit d77067b

File tree

16 files changed

+179
-3
lines changed

16 files changed

+179
-3
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
File renamed without changes.

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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Convert the space delimited string to an array
2+
eval "files=(${args[file]})"
3+
4+
echo
5+
echo "files:"
6+
for i in "${files[@]}"; do
7+
echo " path: $i:"
8+
content="$(cat "$i")"
9+
echo " content: ${content}"
10+
echo " upcase: ${content^^}"
11+
done
12+
13+
echo
14+
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*

0 commit comments

Comments
 (0)