Skip to content

Commit f028230

Browse files
authored
Merge pull request #493 from DannyBen/refactor/validation
Refactor validations to run after assigning default values
2 parents 2a3845c + d112052 commit f028230

File tree

30 files changed

+193
-29
lines changed

30 files changed

+193
-29
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
source 'https://rubygems.org'
22

3-
gem 'byebug'
3+
gem 'debug'
44
gem 'lp'
55
gem 'rspec'
66
gem 'rspec_approvals'

examples/render-mandoc/docs/download.1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.\" Automatically generated by Pandoc 3.1.9
22
.\"
3-
.TH "download" "1" "December 2023" "Version 0.1.0" "Sample application"
3+
.TH "download" "1" "February 2024" "Version 0.1.0" "Sample application"
44
.SH NAME
55
\f[B]download\f[R] - Sample application
66
.SH SYNOPSIS

examples/render-mandoc/docs/download.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
% download(1) Version 0.1.0 | Sample application
22
% Lana Lang
3-
% December 2023
3+
% February 2024
44

55
NAME
66
==================================================

lib/bashly.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
if ENV['BYEBUG']
2-
require 'byebug'
1+
if ENV['DEBUGGER']
2+
require 'debug'
33
require 'lp'
44
end
55

lib/bashly/script/command.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,11 @@ def user_lib
322322
@user_lib ||= Dir["#{Settings.full_lib_dir}/**/*.#{Settings.partials_extension}"]
323323
end
324324

325+
# Returns a mixed array of Argument and Flag objects that have validations
326+
def validatables
327+
@validatables ||= args.select(&:validate) + flags.select(&:validate)
328+
end
329+
325330
# Returns an array of all the args with a whitelist
326331
def whitelisted_args
327332
args.select(&:allowed)
Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
if validate
2+
= view_marker
23

3-
= view_marker
4-
5-
> if [[ -n $(validate_{{ validate }} "$1") ]]; then
6-
> printf "{{ strings[:validation_error] }}\n" "{{ name.upcase }}" "$(validate_{{ validate }} "$1")" >&2
7-
> exit 1
8-
> fi
9-
>
4+
if repeatable
5+
> if [[ -v args['{{ name }}'] ]]; then
6+
> eval "values=(${args['{{ name }}']})"
7+
> for value in "${values[@]}"; do
8+
> if [[ -n $(validate_{{ validate }} "$value") ]]; then
9+
> printf "{{ strings[:validation_error] }}\n" "{{ name.upcase }}" "$(validate_{{ validate }} "$value")" >&2
10+
> exit 1
11+
> fi
12+
> done
13+
> fi
14+
else
15+
> if [[ -v args['{{ name }}'] && -n $(validate_{{ validate }} "${args['{{ name }}']:-}") ]]; then
16+
> printf "{{ strings[:validation_error] }}\n" "{{ name.upcase }}" "$(validate_{{ validate }} "${args['{{ name }}']:-}")" >&2
17+
> exit 1
18+
> fi
19+
>
20+
end
1021

1122
end

lib/bashly/views/command/parse_requirements.gtx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ end
1515
= render(:required_flags_filter).indent 2
1616
= render(:catch_all_filter).indent 2
1717
= render(:default_assignments).indent 2
18+
= render(:validations).indent 2
1819
= render(:whitelist_filter).indent 2
1920
= render(:user_filter).indent 2
2021

lib/bashly/views/command/parse_requirements_case_catch_all.gtx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ if args.any?
44
condition = "if"
55
args.each do |arg|
66
> {{ condition }} [[ -z ${args['{{ arg.name }}']+x} ]]; then
7-
= arg.render(:validations).indent 2
87
> args['{{ arg.name }}']=$1
98
> shift
109

lib/bashly/views/command/parse_requirements_case_repeatable.gtx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
condition = "if"
44
args.each do |arg|
5-
= arg.render(:validations)
65
> {{ condition }} [[ -z ${args['{{ arg.name }}']+x} ]]; then
76
if arg.repeatable
87
> args['{{ arg.name }}']="\"$1\""

lib/bashly/views/command/parse_requirements_case_simple.gtx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ if args.any?
44
condition = "if"
55
args.each do |arg|
66
> {{ condition }} [[ -z ${args['{{ arg.name }}']+x} ]]; then
7-
> {{ arg.render(:validations).indent 2 }}
87
> args['{{ arg.name }}']=$1
98
> shift
109

0 commit comments

Comments
 (0)