Skip to content

Commit e45580c

Browse files
committed
- Refactor validations to run after assigning default values
1 parent 2a3845c commit e45580c

File tree

17 files changed

+61
-16
lines changed

17 files changed

+61
-16
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'

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: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
if validate
22

3-
= view_marker
3+
= view_marker
44

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-
>
5+
> if [[ -n $(validate_{{ validate }} "${args['{{ name }}']}") ]]; then
6+
> printf "{{ strings[:validation_error] }}\n" "{{ name.upcase }}" "$(validate_{{ validate }} "${args['{{ name }}']}")" >&2
7+
> exit 1
8+
> fi
9+
>
1010

1111
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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
if validatables.any?
2+
= view_marker
3+
4+
validatables.each do |arg_or_flag|
5+
= arg_or_flag.render(:validations)
6+
end
7+
end

lib/bashly/views/flag/case_arg.gtx

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

33
> if [[ -n ${2+x} ]]; then
4-
= render(:validations).indent 2
54

65
if repeatable
76
> if [[ -z ${args['{{ name }}']+x} ]]; then

0 commit comments

Comments
 (0)