Skip to content

Commit bafa806

Browse files
committed
- Add optional arg/flag validation functions
1 parent e0b708e commit bafa806

File tree

10 files changed

+36
-0
lines changed

10 files changed

+36
-0
lines changed

lib/bashly/models/argument.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ class Argument < Base
44
def usage_string
55
required ? name.upcase : "[#{name.upcase}]"
66
end
7+
8+
def validations
9+
return [] unless options['validate']
10+
if options['validate'].is_a? String
11+
[options['validate']]
12+
else
13+
options['validate']
14+
end
15+
end
716
end
817
end
918
end

lib/bashly/models/base.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Base
2525
parent_name
2626
required
2727
short
28+
validate
2829
version
2930
]
3031

lib/bashly/models/flag.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ def usage_string(extended: false)
2121
result << strings[:required] if required and extended
2222
result.join " "
2323
end
24+
25+
def validations
26+
return [] unless options['validate']
27+
if options['validate'].is_a? String
28+
[options['validate']]
29+
else
30+
options['validate']
31+
end
32+
end
2433
end
2534
end
2635
end

lib/bashly/templates/strings.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ missing_dependency: "missing dependency: %{dependency}"
3232
disallowed_flag: "%{name} must be one of: %{allowed}"
3333
disallowed_argument: "%{name} must be one of: %{allowed}"
3434
unsupported_bash_version: "bash version 4 or higher is required"
35+
validation_error: "validation error: %s"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
% validations.each do |validation_function|
2+
if [[ -n $(validate_<%= validation_function %> "$1") ]]; then
3+
printf "<%= strings[:validation_error] %>\n" "<%= name.upcase %> $(validate_<%= validation_function %> "$1")"
4+
exit 1
5+
fi
6+
% end

lib/bashly/views/command/parse_requirements_case.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
% condition = "if"
44
% args.each do |arg|
55
<%= condition %> [[ ! ${args[<%= arg.name %>]} ]]; then
6+
<%= arg.render(:validations).indent 2 %>
67
args[<%= arg.name %>]=$1
78
shift
89
% condition = "elif"

lib/bashly/views/command/required_args_filter.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# :command.required_args_filter
22
% required_args.each do |arg|
33
if [[ $1 && $1 != -* ]]; then
4+
<%= arg.render(:validations).indent 2 %>
45
args[<%= arg.name %>]=$1
56
shift
67
else

lib/bashly/views/flag/case.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<%= aliases.join " | " %> )
33
% if arg
44
if [[ $2 ]]; then
5+
<%= render(:validations).indent 4 %>
56
args[<%= name %>]="$2"
67
shift
78
shift
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
% validations.each do |validation_function|
2+
if [[ -n $(validate_<%= validation_function %> "$2") ]]; then
3+
printf "<%= strings[:validation_error] %>\n" "<%= usage_string %> $(validate_<%= validation_function %> "$2")"
4+
exit 1
5+
fi
6+
% end

spec/fixtures/workspaces/flag-args-with-dash/argflag

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ parse_requirements() {
143143
# :flag.case
144144
--options | -o )
145145
if [[ $2 ]]; then
146+
146147
args[--options]="$2"
147148
shift
148149
shift

0 commit comments

Comments
 (0)