Skip to content

Commit 1048437

Browse files
committed
add reusable flags example
1 parent 0432d29 commit 1048437

File tree

5 files changed

+202
-0
lines changed

5 files changed

+202
-0
lines changed

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Each of these examples demonstrates one aspect or feature of bashly.
2828
- [whitelist](whitelist#readme) - arguments and flags with a predefined allowed list of values
2929
- [repeatable-arg](repeatable-arg#readme) - allowing arguments to be provided multiple times
3030
- [repeatable-flag](repeatable-flag#readme) - allowing flags to be provided multiple times
31+
- [reusable-flags](reusable-flags#readme) - reuse flag definition for multiple commands
3132
- [conflicts](conflicts#readme) - defining mutually exclusive flags
3233
- [command-private](command-private#readme) - hiding commands from the command list
3334
- [stdin](stdin#readme) - reading input from stdin

examples/reusable-flags/.gitignore

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

examples/reusable-flags/README.md

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
# Reusable Flags Example
2+
3+
Demonstrates how to use YAML anchors to apply the same flags to multiple
4+
commands. Note that this is a standard YAML feature, and is not bashly specific.
5+
6+
This example was generated with:
7+
8+
```bash
9+
$ bashly init
10+
# ... now edit src/bashly.yml to match the example ...
11+
$ bashly generate
12+
```
13+
14+
-----
15+
16+
## `bashly.yml`
17+
18+
```yaml
19+
name: cli
20+
help: Sample application
21+
version: 0.1.0
22+
23+
commands:
24+
- name: download
25+
help: Download a file
26+
27+
# Label these flags (&force, &debug) so we can reference it later
28+
flags:
29+
- &force
30+
long: --force
31+
short: -f
32+
help: Overwrite existing files
33+
34+
- &debug
35+
long: --debug
36+
short: -d
37+
help: Show debug information
38+
39+
- name: upload
40+
help: Upload a file
41+
42+
# Reuse previously defined flags, and then add new ones
43+
flags:
44+
- *force
45+
- *debug
46+
- long: --password
47+
short: -p
48+
arg: password
49+
help: Password to use for logging in
50+
```
51+
52+
53+
54+
## Generated script output
55+
56+
### `$ ./cli -h`
57+
58+
```shell
59+
cli - Sample application
60+
61+
Usage:
62+
cli COMMAND
63+
cli [COMMAND] --help | -h
64+
cli --version | -v
65+
66+
Commands:
67+
download Download a file
68+
upload Upload a file
69+
70+
Options:
71+
--help, -h
72+
Show this help
73+
74+
--version, -v
75+
Show version number
76+
77+
78+
79+
```
80+
81+
### `$ ./cli download -h`
82+
83+
```shell
84+
cli download - Download a file
85+
86+
Usage:
87+
cli download [OPTIONS]
88+
cli download --help | -h
89+
90+
Options:
91+
--help, -h
92+
Show this help
93+
94+
--force, -f
95+
Overwrite existing files
96+
97+
--debug, -d
98+
Show debug information
99+
100+
101+
102+
```
103+
104+
### `$ ./cli upload -h`
105+
106+
```shell
107+
cli upload - Upload a file
108+
109+
Usage:
110+
cli upload [OPTIONS]
111+
cli upload --help | -h
112+
113+
Options:
114+
--help, -h
115+
Show this help
116+
117+
--force, -f
118+
Overwrite existing files
119+
120+
--debug, -d
121+
Show debug information
122+
123+
--password, -p PASSWORD
124+
Password to use for logging in
125+
126+
127+
128+
```
129+
130+
### `$ ./cli upload --force`
131+
132+
```shell
133+
# this file is located in 'src/upload_command.sh'
134+
# code for 'cli upload' goes here
135+
# you can edit it freely and regenerate (it will not be overwritten)
136+
args:
137+
- ${args[--force]} = 1
138+
139+
140+
```
141+
142+
### `$ ./cli download --debug`
143+
144+
```shell
145+
# this file is located in 'src/download_command.sh'
146+
# code for 'cli download' goes here
147+
# you can edit it freely and regenerate (it will not be overwritten)
148+
args:
149+
- ${args[--debug]} = 1
150+
151+
152+
```
153+
154+
155+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: cli
2+
help: Sample application
3+
version: 0.1.0
4+
5+
commands:
6+
- name: download
7+
help: Download a file
8+
9+
# Label these flags (&force, &debug) so we can reference it later
10+
flags:
11+
- &force
12+
long: --force
13+
short: -f
14+
help: Overwrite existing files
15+
16+
- &debug
17+
long: --debug
18+
short: -d
19+
help: Show debug information
20+
21+
- name: upload
22+
help: Upload a file
23+
24+
# Reuse previously defined flags, and then add new ones
25+
flags:
26+
- *force
27+
- *debug
28+
- long: --password
29+
short: -p
30+
arg: password
31+
help: Password to use for logging in

examples/reusable-flags/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 download -h
11+
./cli upload -h
12+
./cli upload --force
13+
./cli download --debug

0 commit comments

Comments
 (0)