Skip to content

Commit 5c5f447

Browse files
committed
add catch_all + stdin example
1 parent cfb8093 commit 5c5f447

File tree

10 files changed

+158
-0
lines changed

10 files changed

+158
-0
lines changed

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Each of these examples demonstrates one aspect or feature of bashly.
2121

2222
- [catch-all](catch-all#readme) - a command that can receive an arbitrary number of arguments
2323
- [catch-all-advanced](catch-all-advanced#readme) - another example for the `catch_all` option
24+
- [catch-all-stdin](catch-all-stdin#readme) - combining `catch_all` with `stdin` to read multiple files
2425
- [extensible](extensible#readme) - letting your script's users extend the script
2526
- [extensible-delegate](extensible-delegate#readme) - extending your script by delegating commands to an external executable
2627
- [whitelist](whitelist#readme) - arguments and flags with a predefined allowed list of values
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cli

examples/catch-all-stdin/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Catch All with stdin input
2+
3+
Demonstrates how to process multiple files as arguments, or from stdin.
4+
5+
This example was generated with:
6+
7+
```bash
8+
$ bashly init
9+
# ... now edit src/bashly.yml to match the example ...
10+
# ... now edit src/root_command.yml to match the example ...
11+
$ bashly generate
12+
```
13+
14+
-----
15+

examples/catch-all-stdin/file1

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

examples/catch-all-stdin/file2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
file2 content
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: cli
2+
help: Sample application
3+
version: 0.1.0
4+
5+
catch_all:
6+
label: file
7+
help: Path to one or more files. Reads from stdin if empty or "-".
8+
9+
flags:
10+
- long: --format
11+
short: -f
12+
arg: format
13+
help: Specify file format
14+
default: json
15+
allowed: [csv, json]
16+
17+
examples:
18+
- cli file1 file2 --format csv
19+
- cli --format csv file1 file2
20+
- cat file1 | cli --format csv
21+
- cat file* | cli - --format csv
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## Code here runs inside the initialize() function
2+
## Use it for anything that you need to run before any other function, like
3+
## setting environment vairables:
4+
## CONFIG_FILE=settings.ini
5+
##
6+
## Feel free to empty (but not delete) this file.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
inspect_args
2+
3+
# Collect the catch_all
4+
files=( ${other_args[*]} )
5+
6+
# Read contents of the provided file(s)
7+
content=""
8+
for file in "${files[@]}"; do
9+
content+="$(cat "$file")"
10+
content+=$'\n'
11+
done
12+
13+
# Read stdin if file(s) not provided as arguments
14+
if [[ -z "$content" ]]; then
15+
content="$(cat -)"
16+
fi
17+
18+
echo "Collected file contents:"
19+
echo "$content"
20+
21+

examples/catch-all-stdin/test.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
3+
set -x
4+
5+
bashly generate
6+
7+
### Try Me ###
8+
9+
./cli -h
10+
./cli file1 file2 --format csv
11+
./cli -f=csv file1 file2
12+
cat file1 | ./cli --format csv
13+
cat file* | ./cli -
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
+ bashly generate
2+
creating user files in src
3+
skipped src/initialize.sh (exists)
4+
skipped src/root_command.sh (exists)
5+
created ./cli
6+
run ./cli --help to test your bash script
7+
+ ./cli -h
8+
cli - Sample application
9+
10+
Usage:
11+
cli [options] [FILE...]
12+
cli --help | -h
13+
cli --version | -v
14+
15+
Options:
16+
--help, -h
17+
Show this help
18+
19+
--version, -v
20+
Show version number
21+
22+
--format, -f FORMAT
23+
Specify file format
24+
Allowed: csv, json
25+
Default: json
26+
27+
Arguments:
28+
FILE...
29+
Path to one or more files. Reads from stdin if empty or "-".
30+
31+
Examples:
32+
cli file1 file2 --format csv
33+
cli --format csv file1 file2
34+
cat file1 | cli --format csv
35+
cat file* | cli - --format csv
36+
37+
+ ./cli file1 file2 --format csv
38+
args:
39+
- ${args[--format]} = csv
40+
41+
other_args:
42+
- ${other_args[*]} = file1 file2
43+
- ${other_args[0]} = file1
44+
- ${other_args[1]} = file2
45+
Collected file contents:
46+
file1 content
47+
file2 content
48+
49+
+ ./cli -f=csv file1 file2
50+
args:
51+
- ${args[--format]} = csv
52+
53+
other_args:
54+
- ${other_args[*]} = file1 file2
55+
- ${other_args[0]} = file1
56+
- ${other_args[1]} = file2
57+
Collected file contents:
58+
file1 content
59+
file2 content
60+
61+
+ cat file1
62+
+ ./cli --format csv
63+
args:
64+
- ${args[--format]} = csv
65+
Collected file contents:
66+
file1 content
67+
+ ./cli -
68+
+ cat file1 file2
69+
args:
70+
- ${args[--format]} = json
71+
72+
other_args:
73+
- ${other_args[*]} = -
74+
- ${other_args[0]} = -
75+
Collected file contents:
76+
file1 content
77+
file2 content
78+

0 commit comments

Comments
 (0)