Skip to content

Commit

Permalink
Change parsers to use errors aggregator
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Apr 11, 2020
1 parent 1eac60a commit 8a3f1ae
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 91 deletions.
40 changes: 7 additions & 33 deletions lib/tty/option/parser/arguments.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require_relative "../error_aggregator"
require_relative "../pipeline"

module TTY
Expand All @@ -16,8 +17,7 @@ class Arguments
# @api public
def initialize(arguments, **config)
@arguments = arguments
@raise_if_missing = config.fetch(:raise_if_missing) { true }
@errors = {}
@error_aggregator = ErrorAggregator.new(**config)
@parsed = {}
@remaining = []
@required = []
Expand Down Expand Up @@ -61,7 +61,7 @@ def parse(argv)

check_required

[@parsed, @remaining, @errors]
[@parsed, @remaining, @error_aggregator.errors]
end

private
Expand Down Expand Up @@ -93,7 +93,7 @@ def process_exact_arity(arg)
if 0 < values.size && values.size < arg.arity &&
Array(@defaults[arg.name]).size < arg.arity
error = InvalidArity.new(arg, values.size)
record_error(error, error.message, arg)
@error_aggregator.(error, error.message, arg)
end

values
Expand Down Expand Up @@ -124,7 +124,7 @@ def process_infinite_arity(arg)

if values.size < arity && Array(@defaults[arg.name]).size < arity
error = InvalidArity.new(arg, values.size)
record_error(error, error.message, arg)
@error_aggregator.(error, error.message, arg)
end

values
Expand Down Expand Up @@ -166,32 +166,6 @@ def keyword?(value)
!value.match(/^(.+)=(.+)/).nil?
end

# Record or raise an error
#
# @api private
def record_error(error, message, param = nil)
is_class = error.is_a?(Class)

if @raise_if_missing
if is_class
raise error, message
else
raise error
end
end

type_name = is_class ? error.name : error.class.name
type_key = type_name.to_s.split("::").last
.gsub(/([a-z]+)([A-Z])/, "\\1_\\2")
.downcase.to_sym

if param
(@errors[param.name] ||= {}).merge!(type_key => message)
else
@errors[:invalid] = message
end
end

# Assign argument to the parsed
#
# @param [Argument] arg
Expand Down Expand Up @@ -232,8 +206,8 @@ def check_required
else
param.name
end
record_error(MissingParameter,
"need to provide '#{name}' #{param.to_sym}", param)
@error_aggregator.(MissingParameter,
"need to provide '#{name}' #{param.to_sym}", param)
end
end
end # Arguments
Expand Down
31 changes: 6 additions & 25 deletions lib/tty/option/parser/environments.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require_relative "../error_aggregator"
require_relative "../pipeline"

module TTY
Expand All @@ -16,9 +17,8 @@ class Environments
# @api public
def initialize(environments, **config)
@environments = environments
@raise_if_missing = config.fetch(:raise_if_missing) { true }
@error_aggregator = ErrorAggregator.new(**config)
@parsed = {}
@errors = {}
@remaining = []
@names = {}
@arities = Hash.new(0)
Expand Down Expand Up @@ -75,7 +75,7 @@ def parse(argv, env)
check_arity
check_required

[@parsed, @remaining, @errors]
[@parsed, @remaining, @error_aggregator.errors]
end

private
Expand Down Expand Up @@ -121,25 +121,6 @@ def assign_envvar(env_arg, value)
end
end

# Record or raise an error
#
# @api private
def record_error(type, message, param = nil)
if @raise_if_missing
raise type, message
end

type_key = type.to_s.split("::").last
.gsub(/([a-z]+)([A-Z])/, "\\1_\\2")
.downcase.to_sym

if param
(@errors[param.name] ||= {}).merge!(type_key => message)
else
@errors[:invalid] = message
end
end

# Check if parameter matches arity
#
# @raise [InvalidArity]
Expand All @@ -151,7 +132,7 @@ def check_arity

if 0 < param.arity.abs && arity < param.arity.abs
error = InvalidArity.new(param, arity)
record_error(error, error.message, param)
@error_aggregator.(error, error.message, param)
end
end
end
Expand All @@ -170,8 +151,8 @@ def check_required
else
param.name
end
record_error(MissingParameter,
"need to provide '#{name}' #{param.to_sym}", param)
@error_aggregator.(MissingParameter,
"need to provide '#{name}' #{param.to_sym}", param)
end
end
end # Environments
Expand Down
37 changes: 6 additions & 31 deletions lib/tty/option/parser/keywords.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require_relative "../error_aggregator"
require_relative "../pipeline"

module TTY
Expand All @@ -16,8 +17,7 @@ class Keywords
# @api public
def initialize(keywords, **config)
@keywords = keywords
@raise_if_missing = config.fetch(:raise_if_missing) { true }
@errors = {}
@error_aggregator = ErrorAggregator.new(**config)
@parsed = {}
@remaining = []
@names = {}
Expand Down Expand Up @@ -63,7 +63,7 @@ def parse(argv)
check_arity
check_required

[@parsed, @remaining, @errors]
[@parsed, @remaining, @error_aggregator.errors]
end

private
Expand Down Expand Up @@ -119,31 +119,6 @@ def assign_keyword(kwarg, value)
end
end

# Record or raise an error
#
# @api private
def record_error(error, message, param = nil)
is_class = error.is_a?(Class)

if @raise_if_missing
if is_class
raise error, message
else
raise error
end
end

type_name = is_class ? error.name : error.class.name
type_key = type_name.to_s.split("::").last
.gsub(/([a-z]+)([A-Z])/, "\\1_\\2")
.downcase.to_sym
if param
(@errors[param.name] ||= {}).merge!(type_key => message)
else
@errors[:invalid] = message
end
end

# Check if parameter matches arity
#
# @raise [InvalidArity]
Expand All @@ -155,7 +130,7 @@ def check_arity

if 0 < param.arity.abs && arity < param.arity.abs
error = InvalidArity.new(param, arity)
record_error(error, error.message, param)
@error_aggregator.(error, error.message, param)
end
end
end
Expand All @@ -174,8 +149,8 @@ def check_required
else
param.name
end
record_error(MissingParameter,
"need to provide '#{name}' #{param.to_sym}", param)
@error_aggregator.(MissingParameter,
"need to provide '#{name}' #{param.to_sym}", param)
end
end
end # Keywords
Expand Down
4 changes: 2 additions & 2 deletions lib/tty/option/parser/options.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require_relative "../pipeline"
require_relative "../error_aggregator"
require_relative "../pipeline"

module TTY
module Option
Expand All @@ -23,14 +23,14 @@ def initialize(options, **config)
@options = options
@raise_if_missing = config.fetch(:raise_if_missing) { true }
@check_invalid_options = config.fetch(:check_invalid_options) { true }
@error_aggregator = ErrorAggregator.new(**config)
@parsed = {}
@remaining = []
@shorts = {}
@longs = {}
@arities = Hash.new(0)
@required = []
@multiplies = {}
@error_aggregator = ErrorAggregator.new(**config)

setup_opts
end
Expand Down

0 comments on commit 8a3f1ae

Please sign in to comment.