Skip to content

Commit fa692a9

Browse files
committed
- Add support for repeatableflags
1 parent b42a399 commit fa692a9

File tree

13 files changed

+241
-0
lines changed

13 files changed

+241
-0
lines changed

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Each of these examples demonstrates one aspect or feature of bashly.
2424
- [extensible](extensible#readme) - letting your script's users extend the script
2525
- [extensible-delegate](extensible-delegate#readme) - extending your script by delegating commands to an external executable
2626
- [whitelist](whitelist#readme) - arguments and flags with a predefined allowed list of values
27+
- [repeatable](repeatable#readme) - allowing flags to be provided multiple times
2728
- [command-private](command-private#readme) - hiding commands from the command list
2829
- [stdin](stdin#readme) - reading input from stdin
2930
- [filters](filters#readme) - preventing commands from running unless custom conditions are met

examples/repeatable/.gitignore

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

examples/repeatable/README.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# Repeatable Example
2+
3+
Demonstrates the use of repeatable flags that allow users to run commands such
4+
as `download -d one -d "two three" -vvv`.
5+
6+
This example was generated with:
7+
8+
```bash
9+
$ bashly init --minimal
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: download
23+
help: Sample application to demonstrate the use of repeatable flags
24+
version: 0.1.0
25+
26+
flags:
27+
- long: --data
28+
short: -d
29+
arg: data
30+
help: Provide data values
31+
required: true
32+
33+
# Setting this to true on a flag with an argument means the user can type it
34+
# multiple times, like --data a --data b.
35+
# The argument will be received as a quoted and space-delimited string which
36+
# needs to be converted to an array with `eval "data=(${args[--data]})"`
37+
repeatable: true
38+
39+
- long: --verbose
40+
short: -v
41+
help: Set verbosity level
42+
43+
# Setting this to true on a regular flag means the user can type it multiple
44+
# times, in the form of -vvv or -v -v -v.
45+
# The argument's value will hold the number of times it was entered.
46+
repeatable: true
47+
48+
examples:
49+
- download -d one -d "two three" -vvv
50+
```
51+
52+
## `src/root_command.sh`
53+
54+
```bash
55+
# Convert the space delimited string to an array
56+
eval "data=(${args[--data]})"
57+
58+
echo "Data elements:"
59+
for i in "${data[@]}"; do
60+
echo "$i"
61+
done
62+
63+
# The --verbose arg will contain the number of times it was used by the user
64+
verbose=${args[--verbose]}
65+
echo ""
66+
echo "Verbosity level: $verbose"
67+
echo ""
68+
69+
inspect_args
70+
71+
```
72+
73+
74+
## Generated script output
75+
76+
### `$ ./download -h`
77+
78+
```shell
79+
download - Sample application to demonstrate use of repeatable flags
80+
81+
Usage:
82+
download [options]
83+
download --help | -h
84+
download --version
85+
86+
Options:
87+
--help, -h
88+
Show this help
89+
90+
--version
91+
Show version number
92+
93+
--data, -d DATA (required) (repeatable)
94+
Provide data values
95+
96+
--verbose, -v (repeatable)
97+
Set verbosity level
98+
99+
Examples:
100+
download -d one -d "two three" -vvv
101+
102+
103+
104+
```
105+
106+
### `$ ./download -d one -d "two three" -vvv`
107+
108+
```shell
109+
Data elements:
110+
one
111+
two three
112+
113+
Verbosity level: 3
114+
115+
args:
116+
- ${args[--data]} = "one" "two three"
117+
- ${args[--verbose]} = 3
118+
119+
120+
```
121+
122+
123+

examples/repeatable/src/bashly.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: download
2+
help: Sample application to demonstrate the use of repeatable flags
3+
version: 0.1.0
4+
5+
flags:
6+
- long: --data
7+
short: -d
8+
arg: data
9+
help: Provide data values
10+
required: true
11+
12+
# Setting this to true on a flag with an argument means the user can type it
13+
# multiple times, like --data a --data b.
14+
# The argument will be received as a quoted and space-delimited string which
15+
# needs to be converted to an array with `eval "data=(${args[--data]})"`
16+
repeatable: true
17+
18+
- long: --verbose
19+
short: -v
20+
help: Set verbosity level
21+
22+
# Setting this to true on a regular flag means the user can type it multiple
23+
# times, in the form of -vvv or -v -v -v.
24+
# The argument's value will hold the number of times it was entered.
25+
repeatable: true
26+
27+
examples:
28+
- download -d one -d "two three" -vvv
29+
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: 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+
eval "data=(${args[--data]})"
3+
4+
echo "Data elements:"
5+
for i in "${data[@]}"; do
6+
echo "$i"
7+
done
8+
9+
# The --verbose arg will contain the number of times it was used by the user
10+
verbose=${args[--verbose]}
11+
echo ""
12+
echo "Verbosity level: $verbose"
13+
echo ""
14+
15+
inspect_args

examples/repeatable/test.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
3+
set -x
4+
5+
bashly generate
6+
7+
### Try Me ###
8+
9+
./download -h
10+
./download -d one -d "two three" -vvv

lib/bashly/config_validator.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ def assert_arg(key, value)
7979
assert_optional_string "#{key}.default", value['default']
8080
assert_optional_string "#{key}.validate", value['validate']
8181
assert_boolean "#{key}.required", value['required']
82+
assert_boolean "#{key}.repeatable", value['repeatable']
8283

8384
assert_array "#{key}.allowed", value['allowed'], of: :string
8485

lib/bashly/script/base.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Base
2525
name
2626
parent_name
2727
private
28+
repeatable
2829
required
2930
short
3031
validate

lib/bashly/script/flag.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def usage_string(extended: false)
1919
result = [aliases.join(", ")]
2020
result << arg.upcase if arg
2121
result << strings[:required] if required and extended
22+
result << strings[:repeatable] if repeatable and extended
2223
result.join " "
2324
end
2425
end

0 commit comments

Comments
 (0)