Skip to content

Commit

Permalink
Merge pull request #456 from DannyBen/add/unique-repeatable-arg
Browse files Browse the repository at this point in the history
Add support for `unique` in repeatable args
  • Loading branch information
DannyBen authored Dec 7, 2023
2 parents 220150d + 7559b03 commit ac3df69
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 6 deletions.
21 changes: 21 additions & 0 deletions examples/repeatable-arg/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ args:
# needs to be converted to an array with `eval "data=(${args[file]})"`
repeatable: true

# Setting unique to true will ignore non-unique repeating values
unique: true

examples:
- upcase README.md LICENSE
- upcase *.md
Expand Down Expand Up @@ -125,5 +128,23 @@ args:

````

### `$ ./upcase file1 file2 file1`

````shell

files:
path: file1:
content: content of file1
upcase: CONTENT OF FILE1
path: file2:
content: content of file2
upcase: CONTENT OF FILE2

args:
- ${args[file]} = "file1" "file2"


````



3 changes: 3 additions & 0 deletions examples/repeatable-arg/src/bashly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ args:
# needs to be converted to an array with `eval "data=(${args[file]})"`
repeatable: true

# Setting unique to true will ignore non-unique repeating values
unique: true

examples:
- upcase README.md LICENSE
- upcase *.md
1 change: 1 addition & 0 deletions examples/repeatable-arg/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ bashly generate
./upcase -h
./upcase file1
./upcase file*
./upcase file1 file2 file1
8 changes: 6 additions & 2 deletions lib/bashly/config_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,17 @@ def assert_arg(key, value)
assert_optional_string "#{key}.validate", value['validate']
assert_boolean "#{key}.required", value['required']
assert_boolean "#{key}.repeatable", value['repeatable']
assert_boolean "#{key}.unique", value['unique']

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

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

refute value['required'] && value['default'], "#{key} cannot have both nub`required` and nub`default`"

if value['unique']
assert value['repeatable'], "#{key}.unique does not make sense without nub`repeatable`"
end
end

def assert_flag(key, value)
Expand Down Expand Up @@ -143,8 +148,7 @@ def assert_flag(key, value)
end

if value['unique']
condition = value['arg'] && value['repeatable']
assert condition, "#{key}.unique does not make sense without nub`arg` and nub`repeatable`"
assert value['arg'] && value['repeatable'], "#{key}.unique does not make sense without nub`arg` and nub`repeatable`"
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/bashly/script/argument.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Argument < Base
class << self
def option_keys
@option_keys ||= %i[
allowed default help name repeatable required validate
allowed default help name repeatable required unique validate
]
end
end
Expand Down
14 changes: 11 additions & 3 deletions lib/bashly/views/command/parse_requirements_case_repeatable.gtx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@ args.each do |arg|
if arg.repeatable
> args['{{ arg.name }}']="\"$1\""
> shift
> else
> args['{{ arg.name }}']="${args[{{ arg.name }}]} \"$1\""
> shift
if arg.unique
> elif [[ ! "${args['{{ arg.name }}']}" =~ \"$1\" ]]; then
> args['{{ arg.name }}']="${args[{{ arg.name }}]} \"$1\""
> shift
> else
> shift
else
> else
> args['{{ arg.name }}']="${args[{{ arg.name }}]} \"$1\""
> shift
end

else
> args['{{ arg.name }}']=$1
Expand Down
12 changes: 12 additions & 0 deletions spec/approvals/examples/repeatable-arg
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,15 @@ files:

args:
- ${args[file]} = "file1" "file2"
+ ./upcase file1 file2 file1

files:
path: file1:
content: content of file1
upcase: CONTENT OF FILE1
path: file2:
content: content of file2
upcase: CONTENT OF FILE2

args:
- ${args[file]} = "file1" "file2"
1 change: 1 addition & 0 deletions spec/approvals/validations/arg_unique_without_repeatable
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#<Bashly::ConfigurationError: root.args[0].unique does not make sense without nub`repeatable`>
8 changes: 8 additions & 0 deletions spec/fixtures/script/validations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
- name: target
help: Target filename

:arg_unique_without_repeatable:
name: invalid
help: arg must be repeatable when using unique
args:
- name: target
help: Target filename
unique: true

:command_catch_all_type:
name: invalid
help: catch_all must be boolean, string, or hash
Expand Down

0 comments on commit ac3df69

Please sign in to comment.