Skip to content

Commit f45b993

Browse files
authored
Merge pull request #462 from DannyBen/add/default-array-support
Allow `flag.default` and `arg.default` to be an array if `repeatable` is true
2 parents 8a00ca8 + e53b82e commit f45b993

File tree

22 files changed

+264
-121
lines changed

22 files changed

+264
-121
lines changed

examples/repeatable-arg/README.md

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,22 @@ version: 0.1.0
2626
args:
2727
- name: file
2828
help: One or more files to process
29-
required: true
3029

3130
# Setting repeatable to true means that the user can provide multiple arguments
3231
# for it.
3332
# The argument will be received as a quoted and space-delimited string which
34-
# needs to be converted to an array with `eval "data=(${args[file]})"`
33+
# needs to be converted to an array with `eval "data=(${args[file]})"`.
3534
repeatable: true
3635

37-
# Setting unique to true will ignore non-unique repeating values
36+
# Setting unique to true will ignore non-unique repeating values.
3837
unique: true
3938

39+
# Setting default value(s) for a repeatable argument may be done in an array
40+
# form (or a string form if it is a single default value only).
41+
default:
42+
- file1
43+
- file2
44+
4045
examples:
4146
- upcase README.md LICENSE
4247
- upcase *.md
@@ -47,7 +52,7 @@ examples:
4752
````bash
4853
# Convert the space delimited string to an array
4954
files=''
50-
eval "files=(${args[file]})"
55+
eval "files=(${args[file]:-})"
5156

5257
echo
5358
echo "files:"
@@ -72,7 +77,7 @@ inspect_args
7277
upcase - Sample application to demonstrate the use of repeatable arguments
7378

7479
Usage:
75-
upcase FILE...
80+
upcase [FILE...]
7681
upcase --help | -h
7782
upcase --version | -v
7883

@@ -86,13 +91,32 @@ Options:
8691
Arguments:
8792
FILE...
8893
One or more files to process
94+
Default: file1, file2
8995

9096
Examples:
9197
upcase README.md LICENSE
9298
upcase *.md
9399

94100

95101

102+
````
103+
104+
### `$ ./upcase`
105+
106+
````shell
107+
108+
files:
109+
path: file1:
110+
content: content of file1
111+
upcase: CONTENT OF FILE1
112+
path: file2:
113+
content: content of file2
114+
upcase: CONTENT OF FILE2
115+
116+
args:
117+
- ${args[file]} = file1 file2
118+
119+
96120
````
97121

98122
### `$ ./upcase file1`

examples/repeatable-arg/src/bashly.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,22 @@ version: 0.1.0
55
args:
66
- name: file
77
help: One or more files to process
8-
required: true
98

109
# Setting repeatable to true means that the user can provide multiple arguments
1110
# for it.
1211
# 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]})"`
12+
# needs to be converted to an array with `eval "data=(${args[file]})"`.
1413
repeatable: true
1514

16-
# Setting unique to true will ignore non-unique repeating values
15+
# Setting unique to true will ignore non-unique repeating values.
1716
unique: true
1817

18+
# Setting default value(s) for a repeatable argument may be done in an array
19+
# form (or a string form if it is a single default value only).
20+
default:
21+
- file1
22+
- file2
23+
1924
examples:
2025
- upcase README.md LICENSE
2126
- upcase *.md

examples/repeatable-arg/src/root_command.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Convert the space delimited string to an array
22
files=''
3-
eval "files=(${args[file]})"
3+
eval "files=(${args[file]:-})"
44

55
echo
66
echo "files:"

examples/repeatable-arg/test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ bashly generate
77
### Try Me ###
88

99
./upcase -h
10+
./upcase
1011
./upcase file1
1112
./upcase file*
1213
./upcase file1 file2 file1

examples/repeatable-flag/README.md

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,11 @@ flags:
2828
short: -d
2929
arg: data
3030
help: Provide data values
31-
required: true
3231

3332
# Setting this to true on a flag with an argument means the user can type it
3433
# multiple times, like --data a --data b.
3534
# 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]})"`
35+
# needs to be converted to an array with `eval "data=(${args[--data]})"`.
3736
repeatable: true
3837

3938
- long: --path
@@ -42,9 +41,15 @@ flags:
4241
help: Specify one or more paths
4342
repeatable: true
4443

45-
# Setting this to true will ignore repeating arguments that are not unique
44+
# Setting this to true will ignore repeating arguments that are not unique.
4645
unique: true
4746

47+
# Setting default value(s) for a repeatable flag argument may be done in an
48+
# array form (or a string form if it is a single default value only).
49+
default:
50+
- file one
51+
- file-two
52+
4853
- long: --verbose
4954
short: -v
5055
help: Set verbosity level
@@ -63,7 +68,7 @@ examples:
6368

6469
````bash
6570
# Convert the space delimited string to an array
66-
eval "data=(${args[--data]})"
71+
eval "data=(${args[--data]:-})"
6772

6873
echo "Data elements:"
6974
for i in "${data[@]}"; do
@@ -94,11 +99,12 @@ Usage:
9499
download --version
95100

96101
Options:
97-
--data, -d DATA (required) (repeatable)
102+
--data, -d DATA (repeatable)
98103
Provide data values
99104

100105
--path, -p LOCATION (repeatable)
101106
Specify one or more paths
107+
Default: file one, file-two
102108

103109
--verbose, -v (repeatable)
104110
Set verbosity level
@@ -115,6 +121,19 @@ Examples:
115121

116122

117123

124+
````
125+
126+
### `$ ./download`
127+
128+
````shell
129+
Data elements:
130+
131+
Verbosity level: 1
132+
133+
args:
134+
- ${args[--path]} = file\ one file-two
135+
136+
118137
````
119138

120139
### `$ ./download -d one -d "two three" -vvv`
@@ -128,6 +147,7 @@ Verbosity level: 3
128147

129148
args:
130149
- ${args[--data]} = "one" "two three"
150+
- ${args[--path]} = file\ one file-two
131151
- ${args[--verbose]} = 3
132152

133153

examples/repeatable-flag/src/bashly.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ flags:
77
short: -d
88
arg: data
99
help: Provide data values
10-
required: true
1110

1211
# Setting this to true on a flag with an argument means the user can type it
1312
# multiple times, like --data a --data b.
1413
# 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]})"`
14+
# needs to be converted to an array with `eval "data=(${args[--data]})"`.
1615
repeatable: true
1716

1817
- long: --path
@@ -21,9 +20,15 @@ flags:
2120
help: Specify one or more paths
2221
repeatable: true
2322

24-
# Setting this to true will ignore repeating arguments that are not unique
23+
# Setting this to true will ignore repeating arguments that are not unique.
2524
unique: true
2625

26+
# Setting default value(s) for a repeatable flag argument may be done in an
27+
# array form (or a string form if it is a single default value only).
28+
default:
29+
- file one
30+
- file-two
31+
2732
- long: --verbose
2833
short: -v
2934
help: Set verbosity level

examples/repeatable-flag/src/root_command.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Convert the space delimited string to an array
2-
eval "data=(${args[--data]})"
2+
eval "data=(${args[--data]:-})"
33

44
echo "Data elements:"
55
for i in "${data[@]}"; do

examples/repeatable-flag/test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ bashly generate
77
### Try Me ###
88

99
./download -h
10+
./download
1011
./download -d one -d "two three" -vvv
1112
./download -d one --path /bin --path /usr/lib --path /bin

lib/bashly/concerns/validation_helpers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def assert_array(key, value, of: nil)
3131
return unless of
3232

3333
value.each_with_index do |val, i|
34-
send "assert_#{of}".to_sym, "#{key}[#{i}]", val
34+
send :"assert_#{of}", "#{key}[#{i}]", val
3535
end
3636
end
3737

lib/bashly/config_validator.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def assert_arg(key, value)
9191
assert_hash key, value, keys: Script::Argument.option_keys
9292
assert_string "#{key}.name", value['name']
9393
assert_optional_string "#{key}.help", value['help']
94-
assert_optional_string "#{key}.default", value['default']
94+
assert_string_or_array "#{key}.default", value['default']
9595
assert_optional_string "#{key}.validate", value['validate']
9696
assert_boolean "#{key}.required", value['required']
9797
assert_boolean "#{key}.repeatable", value['repeatable']
@@ -106,6 +106,10 @@ def assert_arg(key, value)
106106
if value['unique']
107107
assert value['repeatable'], "#{key}.unique does not make sense without nub`repeatable`"
108108
end
109+
110+
if value['default'].is_a? Array
111+
assert value['repeatable'], "#{key}.default array does not make sense without nub`repeatable`"
112+
end
109113
end
110114

111115
def assert_flag(key, value)
@@ -118,7 +122,7 @@ def assert_flag(key, value)
118122
assert_optional_string "#{key}.short", value['short']
119123
assert_optional_string "#{key}.help", value['help']
120124
assert_optional_string "#{key}.arg", value['arg']
121-
assert_optional_string "#{key}.default", value['default']
125+
assert_string_or_array "#{key}.default", value['default']
122126
assert_optional_string "#{key}.validate", value['validate']
123127

124128
assert_boolean "#{key}.private", value['private']
@@ -148,7 +152,12 @@ def assert_flag(key, value)
148152
end
149153

150154
if value['unique']
151-
assert value['arg'] && value['repeatable'], "#{key}.unique does not make sense without nub`arg` and nub`repeatable`"
155+
assert value['arg'] && value['repeatable'],
156+
"#{key}.unique does not make sense without nub`arg` and nub`repeatable`"
157+
end
158+
159+
if value['default'].is_a? Array
160+
assert value['repeatable'], "#{key}.default array does not make sense without nub`repeatable`"
152161
end
153162
end
154163

0 commit comments

Comments
 (0)