Skip to content

Commit 74f0ce3

Browse files
committed
- Add support for repeatable args
1 parent 011c648 commit 74f0ce3

File tree

5 files changed

+31
-4
lines changed

5 files changed

+31
-4
lines changed

lib/bashly/config_validator.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ def assert_flag(key, value)
9999
assert_optional_string "#{key}.default", value['default']
100100
assert_optional_string "#{key}.validate", value['validate']
101101

102+
assert_boolean "#{key}.repeatable", value['repeatable']
102103
assert_boolean "#{key}.required", value['required']
103104
assert_array "#{key}.allowed", value['allowed'], of: :string
104105
assert_array "#{key}.conflicts", value['conflicts'], of: :string

lib/bashly/script/argument.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ module Bashly
22
module Script
33
class Argument < Base
44
def usage_string
5-
required ? name.upcase : "[#{name.upcase}]"
5+
required ? label : "[#{label}]"
6+
end
7+
8+
def label
9+
repeatable ? "#{name.upcase}..." : name.upcase
610
end
711
end
812
end

lib/bashly/views/argument/usage.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# :argument.usage
2-
echo " <%= name.upcase %>"
2+
echo " <%= label %>"
33
printf "<%= help.wrap(76).indent(4).sanitize_for_print %>\n"
44
% if allowed
55
printf " <%= strings[:allowed] % { values: allowed.join(', ') } -%>\n"
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,38 @@
11
# :command.parse_requirements_case
2+
% repeatable_arg = false
23
% if args.any?
34
% condition = "if"
45
% args.each do |arg|
56
<%= condition %> [[ -z ${args[<%= arg.name %>]+x} ]]; then
67
<%= arg.render(:validations).indent 2 %>
8+
% if arg.repeatable
9+
% repeatable_arg = true
10+
args[<%= arg.name %>]="\"$1\""
11+
shift
12+
else
13+
args[<%= arg.name %>]="${args[<%= arg.name %>]} \"$1\""
14+
shift
15+
% else
716
args[<%= arg.name %>]=$1
817
shift
18+
% end
919
% condition = "elif"
1020
% end
21+
% if !repeatable_arg
1122
else
23+
% end
1224
% if catch_all.enabled?
1325
other_args+=("$1")
1426
shift
15-
% else
27+
% elsif !repeatable_arg
1628
printf "<%= strings[:invalid_argument] %>\n" "$key"
1729
exit 1
1830
% end
1931
fi
2032
% elsif catch_all.enabled?
2133
other_args+=("$1")
2234
shift
23-
% else
35+
% elsif !repeatable_arg
2436
printf "<%= strings[:invalid_argument] %>\n" "$key"
2537
exit 1
2638
% end

lib/bashly/views/command/whitelist_filter.erb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
# :command.whitelist_filter
22
% whitelisted_args.each do |arg|
3+
% if arg.repeatable
4+
eval "input_array=(${args[<%= arg.name %>]})"
5+
for i in "${input_array[@]}"; do
6+
if [[ ! $i =~ ^(<%= arg.allowed.join '|' %>)$ ]]; then
7+
printf "%s\n" "<%= strings[:disallowed_arg] % { name: arg.name, allowed: arg.allowed.join(', ') } %>"
8+
exit 1
9+
fi
10+
done
11+
% else
312
if [[ ! ${args[<%= arg.name %>]} =~ ^(<%= arg.allowed.join '|' %>)$ ]]; then
413
printf "%s\n" "<%= strings[:disallowed_argument] % { name: arg.name, allowed: arg.allowed.join(', ') } %>"
514
exit 1
615
fi
716
% end
17+
% end
818
% whitelisted_flags.each do |flag|
919
% if flag.repeatable
1020
eval "input_array=(${args[<%= flag.name %>]})"

0 commit comments

Comments
 (0)