Skip to content

Commit b4fabac

Browse files
authored
Merge pull request #173 from DannyBen/add/more-validations
Add validations for flag and arg names
2 parents ee76065 + 7bb02bc commit b4fabac

File tree

6 files changed

+35
-0
lines changed

6 files changed

+35
-0
lines changed

lib/bashly/config_validator.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ def assert_arg(key, value)
8181
assert_boolean "#{key}.required", value['required']
8282

8383
assert_array "#{key}.allowed", value['allowed'], of: :string
84+
85+
refute value['name'].match(/^-/), "#{key}.name must not start with '-'"
8486
end
8587

8688
def assert_flag(key, value)
@@ -96,6 +98,10 @@ def assert_flag(key, value)
9698

9799
assert_boolean "#{key}.required", value['required']
98100
assert_array "#{key}.allowed", value['allowed'], of: :string
101+
102+
assert value['long'].match(/^--[a-zA-Z0-9_\-]+$/), "#{key}.long must be in the form of '--name'" if value['long']
103+
assert value['short'].match(/^-[a-zA-Z0-9]$/), "#{key}.short must be in the form of '-n'" if value['short']
104+
refute value['arg'].match(/^-/), "#{key}.arg must not start with '-'" if value['arg']
99105
end
100106

101107
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.args[0].name must not start with '-'>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#<Bashly::ConfigurationError: root.flags[0].arg must not start with '-'>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#<Bashly::ConfigurationError: root.flags[0].long must be in the form of '--name'>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#<Bashly::ConfigurationError: root.flags[0].short must be in the form of '-n'>

spec/fixtures/script/validations.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,28 @@
3636
- name: ok
3737
- help: invalid since there is no name
3838

39+
:invalid_long_flag:
40+
name: invalid
41+
help: invalid since the long flag is not in the form of --force
42+
flags:
43+
- long: force
44+
45+
:invalid_short_flag:
46+
name: invalid
47+
help: invalid since the short flag is not in the form of -f
48+
flags:
49+
- short: f
50+
51+
:invalid_flag_arg:
52+
name: invalid
53+
help: invalid since the flag.arg should not start with --
54+
flags:
55+
- long: --user
56+
arg: --name
57+
58+
:invalid_arg:
59+
name: invalid
60+
help: invalid since the arg.name should not start with --
61+
args:
62+
- name: --user
63+

0 commit comments

Comments
 (0)