Skip to content

Commit ac3df69

Browse files
authored
Merge pull request #456 from DannyBen/add/unique-repeatable-arg
Add support for `unique` in repeatable args
2 parents 220150d + 7559b03 commit ac3df69

File tree

9 files changed

+64
-6
lines changed

9 files changed

+64
-6
lines changed

examples/repeatable-arg/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ args:
3434
# needs to be converted to an array with `eval "data=(${args[file]})"`
3535
repeatable: true
3636

37+
# Setting unique to true will ignore non-unique repeating values
38+
unique: true
39+
3740
examples:
3841
- upcase README.md LICENSE
3942
- upcase *.md
@@ -125,5 +128,23 @@ args:
125128

126129
````
127130

131+
### `$ ./upcase file1 file2 file1`
132+
133+
````shell
134+
135+
files:
136+
path: file1:
137+
content: content of file1
138+
upcase: CONTENT OF FILE1
139+
path: file2:
140+
content: content of file2
141+
upcase: CONTENT OF FILE2
142+
143+
args:
144+
- ${args[file]} = "file1" "file2"
145+
146+
147+
````
148+
128149

129150

examples/repeatable-arg/src/bashly.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ args:
1313
# needs to be converted to an array with `eval "data=(${args[file]})"`
1414
repeatable: true
1515

16+
# Setting unique to true will ignore non-unique repeating values
17+
unique: true
18+
1619
examples:
1720
- upcase README.md LICENSE
1821
- upcase *.md

examples/repeatable-arg/test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ bashly generate
99
./upcase -h
1010
./upcase file1
1111
./upcase file*
12+
./upcase file1 file2 file1

lib/bashly/config_validator.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,17 @@ def assert_arg(key, value)
9595
assert_optional_string "#{key}.validate", value['validate']
9696
assert_boolean "#{key}.required", value['required']
9797
assert_boolean "#{key}.repeatable", value['repeatable']
98+
assert_boolean "#{key}.unique", value['unique']
9899

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

101102
refute value['name'].match(/^-/), "#{key}.name must not start with '-'"
102103

103104
refute value['required'] && value['default'], "#{key} cannot have both nub`required` and nub`default`"
105+
106+
if value['unique']
107+
assert value['repeatable'], "#{key}.unique does not make sense without nub`repeatable`"
108+
end
104109
end
105110

106111
def assert_flag(key, value)
@@ -143,8 +148,7 @@ def assert_flag(key, value)
143148
end
144149

145150
if value['unique']
146-
condition = value['arg'] && value['repeatable']
147-
assert condition, "#{key}.unique does not make sense without nub`arg` and nub`repeatable`"
151+
assert value['arg'] && value['repeatable'], "#{key}.unique does not make sense without nub`arg` and nub`repeatable`"
148152
end
149153
end
150154

lib/bashly/script/argument.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class Argument < Base
44
class << self
55
def option_keys
66
@option_keys ||= %i[
7-
allowed default help name repeatable required validate
7+
allowed default help name repeatable required unique validate
88
]
99
end
1010
end

lib/bashly/views/command/parse_requirements_case_repeatable.gtx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,17 @@ args.each do |arg|
77
if arg.repeatable
88
> args['{{ arg.name }}']="\"$1\""
99
> shift
10-
> else
11-
> args['{{ arg.name }}']="${args[{{ arg.name }}]} \"$1\""
12-
> shift
10+
if arg.unique
11+
> elif [[ ! "${args['{{ arg.name }}']}" =~ \"$1\" ]]; then
12+
> args['{{ arg.name }}']="${args[{{ arg.name }}]} \"$1\""
13+
> shift
14+
> else
15+
> shift
16+
else
17+
> else
18+
> args['{{ arg.name }}']="${args[{{ arg.name }}]} \"$1\""
19+
> shift
20+
end
1321

1422
else
1523
> args['{{ arg.name }}']=$1

spec/approvals/examples/repeatable-arg

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,15 @@ files:
4747

4848
args:
4949
- ${args[file]} = "file1" "file2"
50+
+ ./upcase file1 file2 file1
51+
52+
files:
53+
path: file1:
54+
content: content of file1
55+
upcase: CONTENT OF FILE1
56+
path: file2:
57+
content: content of file2
58+
upcase: CONTENT OF FILE2
59+
60+
args:
61+
- ${args[file]} = "file1" "file2"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#<Bashly::ConfigurationError: root.args[0].unique does not make sense without nub`repeatable`>

spec/fixtures/script/validations.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@
4242
- name: target
4343
help: Target filename
4444

45+
:arg_unique_without_repeatable:
46+
name: invalid
47+
help: arg must be repeatable when using unique
48+
args:
49+
- name: target
50+
help: Target filename
51+
unique: true
52+
4553
:command_catch_all_type:
4654
name: invalid
4755
help: catch_all must be boolean, string, or hash

0 commit comments

Comments
 (0)