Skip to content

Commit 7beb22b

Browse files
authored
Merge pull request #160 from DannyBen/add/stdin-support
Add support for stdin
2 parents 63bbe6f + d95a864 commit 7beb22b

File tree

8 files changed

+160
-1
lines changed

8 files changed

+160
-1
lines changed

examples/stdin/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
src/initialize.sh
2+
cli

examples/stdin/README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# STDIN Example
2+
3+
Demonstrates how to read from STDIN.
4+
5+
This example was generated with:
6+
7+
```bash
8+
$ bashly init --minimal
9+
# ... now edit bashly.yml to match ...
10+
$ bashly generate
11+
# ... now edit *.sh to match ...
12+
$ bashly generate
13+
```
14+
15+
<!-- include: src/root_command.sh -->
16+
17+
-----
18+
19+
## `bashly.yml`
20+
21+
```yaml
22+
name: cli
23+
help: STDIN Example
24+
version: 0.1.0
25+
26+
args:
27+
- name: path
28+
29+
# Set the default path value to -, which is the standard operator for stdin.
30+
default: "-"
31+
help: Path to file (reads from stdin if empty)
32+
33+
flags:
34+
- long: --file
35+
short: -f
36+
arg: path
37+
38+
# This is also supported in flags
39+
# Note that in both cases, your script needs to handle the value '-' as there
40+
# is no special treatment in bashly, other than allowing '-' as argument.
41+
default: "-"
42+
help: Another path to file
43+
```
44+
45+
## `src/root_command.sh`
46+
47+
```bash
48+
inspect_args
49+
50+
# Since cat knows how to handle '-' as a value, it will work with both a file
51+
# path and '-' argument.
52+
cat "${args[path]}"
53+
54+
```
55+
56+
57+
## Generated script output
58+
59+
### `$ ./cli some-file`
60+
61+
```shell
62+
args:
63+
- ${args[--file]} = -
64+
- ${args[path]} = some-file
65+
some file with some content
66+
67+
68+
```
69+
70+
### `$ cat some-file | ./cli`
71+
72+
```shell
73+
args:
74+
- ${args[--file]} = -
75+
- ${args[path]} = -
76+
some file with some content
77+
78+
79+
```
80+
81+
### `$ cat some-file | ./cli -`
82+
83+
```shell
84+
args:
85+
- ${args[--file]} = -
86+
- ${args[path]} = -
87+
some file with some content
88+
89+
90+
```
91+
92+
93+

examples/stdin/some-file

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

examples/stdin/src/bashly.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: cli
2+
help: STDIN Example
3+
version: 0.1.0
4+
5+
args:
6+
- name: path
7+
8+
# Set the default path value to -, which is the standard operator for stdin.
9+
default: "-"
10+
help: Path to file (reads from stdin if empty)
11+
12+
flags:
13+
- long: --file
14+
short: -f
15+
arg: path
16+
17+
# This is also supported in flags
18+
# Note that in both cases, your script needs to handle the value '-' as there
19+
# is no special treatment in bashly, other than allowing '-' as argument.
20+
default: "-"
21+
help: Another path to file
22+

examples/stdin/src/root_command.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
inspect_args
2+
3+
# Since cat knows how to handle '-' as a value, it will work with both a file
4+
# path and '-' argument.
5+
cat "${args[path]}"

examples/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+
rm -f "src/initialize.sh"
4+
5+
set -x
6+
7+
bashly generate
8+
9+
### Try Me ###
10+
11+
./cli some-file
12+
cat some-file | ./cli
13+
cat some-file | ./cli -

lib/bashly/views/command/parse_requirements_while.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ while [[ $# -gt 0 ]]; do
77

88
% end
99

10-
-* )
10+
-?* )
1111
% if catch_all
1212
other_args+=("$1")
1313
shift

spec/approvals/examples/stdin

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
+ bashly generate
2+
creating user files in src
3+
created src/initialize.sh
4+
skipped src/root_command.sh (exists)
5+
created ./cli
6+
run ./cli --help to test your bash script
7+
+ ./cli some-file
8+
args:
9+
- ${args[--file]} = -
10+
- ${args[path]} = some-file
11+
some file with some content
12+
+ cat some-file
13+
+ ./cli
14+
args:
15+
- ${args[--file]} = -
16+
- ${args[path]} = -
17+
some file with some content
18+
+ cat some-file
19+
+ ./cli -
20+
args:
21+
- ${args[--file]} = -
22+
- ${args[path]} = -
23+
some file with some content

0 commit comments

Comments
 (0)