Skip to content

Commit 1601075

Browse files
committed
- Fix arg.allowed and flag.allowed to allow setting without 'required' or 'default'
1 parent f075665 commit 1601075

File tree

7 files changed

+77
-9
lines changed

7 files changed

+77
-9
lines changed

lib/bashly/config_validator.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,6 @@ def assert_arg(key, value)
101101
refute value['name'].match(/^-/), "#{key}.name must not start with '-'"
102102

103103
refute value['required'] && value['default'], "#{key} cannot have both nub`required` and nub`default`"
104-
105-
if value['allowed']
106-
assert (value['required'] || value['default']),
107-
"#{key}.allowed does not make sense without either nub`default` or nub`required`"
108-
end
109104
end
110105

111106
def assert_flag(key, value)
@@ -140,8 +135,6 @@ def assert_flag(key, value)
140135

141136
if value['allowed']
142137
assert value['arg'], "#{key}.allowed does not make sense without nub`arg`"
143-
assert (value['required'] || value['default']),
144-
"#{key}.allowed does not make sense without either nub`default` or nub`required`"
145138
end
146139

147140
if value['completions']

lib/bashly/views/command/whitelist_filter.gtx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ if whitelisted_args.any? or whitelisted_flags.any?
1212
> done
1313

1414
else
15-
> if [[ ! ${args['{{ arg.name }}']} =~ ^({{ arg.allowed.join '|' }})$ ]]; then
15+
> if [[ -n ${args['{{ arg.name }}']} ]] && [[ ! ${args['{{ arg.name }}']} =~ ^({{ arg.allowed.join '|' }})$ ]]; then
1616
> printf "%s\n" "{{ strings[:disallowed_argument] % { name: arg.name, allowed: arg.allowed.join(', ') } }}" >&2
1717
> exit 1
1818
> fi
@@ -31,7 +31,7 @@ if whitelisted_args.any? or whitelisted_flags.any?
3131
> done
3232

3333
else
34-
> if [[ ! ${args['{{ flag.name }}']} =~ ^({{ flag.allowed.join '|' }})$ ]]; then
34+
> if [[ ${args['{{ flag.name }}']} ]] && [[ ! ${args['{{ flag.name }}']} =~ ^({{ flag.allowed.join '|' }})$ ]]; then
3535
> printf "%s\n" "{{ strings[:disallowed_flag] % { name: flag.name, allowed: flag.allowed.join(', ') } }}" >&2
3636
> exit 1
3737
> fi
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
+ bundle exec bashly generate
2+
creating user files in src
3+
created src/root_command.sh
4+
created ./cli
5+
run ./cli --help to test your bash script
6+
+ ./cli
7+
# this file is located in 'src/root_command.sh'
8+
# you can edit it freely and regenerate (it will not be overwritten)
9+
args: none
10+
+ ./cli -h
11+
cli - Test that whitelist can be optional and without a default value
12+
13+
Usage:
14+
cli [ACTION] [OPTIONS]
15+
cli --help | -h
16+
cli --version | -v
17+
18+
Options:
19+
--notify HANDLER
20+
Send notification using this handler
21+
Allowed: slack, webhook
22+
23+
--help, -h
24+
Show this help
25+
26+
--version, -v
27+
Show version number
28+
29+
Arguments:
30+
ACTION
31+
Action to run on exit
32+
Allowed: push, commit
33+
34+
+ ./cli invalid
35+
action must be one of: push, commit
36+
+ ./cli --notify snail_mail
37+
--notify must be one of: slack, webhook
38+
+ ./cli push --notify slack
39+
# this file is located in 'src/root_command.sh'
40+
# you can edit it freely and regenerate (it will not be overwritten)
41+
args:
42+
- ${args[action]} = push
43+
- ${args[--notify]} = slack
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
src/*.sh
2+
cli
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This fixture tests that whitelist args and flags can be optional and without a
2+
default value.
3+
ref: https://github.com/DannyBen/bashly/issues/368
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: cli
2+
help: Test that whitelist can be optional and without a default value
3+
version: 0.1.0
4+
5+
args:
6+
- name: action
7+
help: Action to run on exit
8+
allowed: [push, commit]
9+
10+
flags:
11+
- long: --notify
12+
arg: handler
13+
help: Send notification using this handler
14+
allowed: [slack, webhook]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
3+
rm -f ./src/*.sh
4+
5+
set -x
6+
7+
bundle exec bashly generate
8+
9+
./cli
10+
./cli -h
11+
./cli invalid
12+
./cli --notify snail_mail
13+
./cli push --notify slack

0 commit comments

Comments
 (0)