Skip to content

Commit e0ea989

Browse files
committed
add validations for new completions
1 parent be9b05c commit e0ea989

File tree

4 files changed

+24
-0
lines changed

4 files changed

+24
-0
lines changed

lib/bashly/config_validator.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ def assert_flag(key, value)
6161
assert_hash key, value, Script::Flag.option_keys
6262
assert value['short'] || value['long'], "#{key} must have at least one of long or short name"
6363

64+
refute value['allowed'] && value['completions'], "#{key} cannot have both allowed and completions"
65+
6466
assert_optional_string "#{key}.long", value['long']
6567
assert_optional_string "#{key}.short", value['short']
6668
assert_optional_string "#{key}.help", value['help']
@@ -72,6 +74,7 @@ def assert_flag(key, value)
7274
assert_boolean "#{key}.required", value['required']
7375
assert_array "#{key}.allowed", value['allowed'], of: :string
7476
assert_array "#{key}.conflicts", value['conflicts'], of: :string
77+
assert_array "#{key}.completions", value['completions'], of: :string
7578

7679
assert value['long'].match(/^--[a-zA-Z0-9_\-]+$/), "#{key}.long must be in the form of '--name'" if value['long']
7780
assert value['short'].match(/^-[a-zA-Z0-9]$/), "#{key}.short must be in the form of '-n'" if value['short']
@@ -86,6 +89,10 @@ def assert_flag(key, value)
8689
if value['allowed']
8790
assert value['arg'], "#{key}.allowed does not make sense without arg"
8891
end
92+
93+
if value['completions']
94+
assert value['arg'], "#{key}.completions does not make sense without arg"
95+
end
8996
end
9097

9198
def assert_env_var(key, value)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#<Bashly::ConfigurationError: root.flags[0] cannot have both allowed and completions>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#<Bashly::ConfigurationError: root.flags[0].completions does not make sense without arg>

spec/fixtures/script/validations.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,21 @@
146146
- long: --role
147147
allowed: [admin, noob]
148148

149+
:flag_completions_without_arg:
150+
name: invalid
151+
help: flag must have an arg when using completions
152+
flags:
153+
- long: --target
154+
completions: [<dir>]
155+
156+
:flag_allowed_and_completions:
157+
name: invalid
158+
help: flag with both allowed and completions
159+
flags:
160+
- long: --file
161+
allowed: [one, two]
162+
completions: [<file>]
163+
149164
:root_alias:
150165
name: invalid
151166
help: root cannot have alias

0 commit comments

Comments
 (0)