use one exception class for all argument parsing errors
This commit is contained in:
parent
f55fb38e05
commit
0b2193b3d4
11
lib/yawpa.rb
11
lib/yawpa.rb
@ -14,10 +14,7 @@ require "yawpa/version"
|
|||||||
# - Options can be defined with a range specifying the allowed number of parameters
|
# - Options can be defined with a range specifying the allowed number of parameters
|
||||||
module Yawpa
|
module Yawpa
|
||||||
# Exception class raised when an unknown option is observed
|
# Exception class raised when an unknown option is observed
|
||||||
class UnknownOptionException < Exception; end
|
class ArgumentParsingException < Exception; end
|
||||||
|
|
||||||
# Exception class raised when invalid option arguments are observed
|
|
||||||
class InvalidArgumentsException < Exception; end
|
|
||||||
|
|
||||||
module_function
|
module_function
|
||||||
# :call-seq:
|
# :call-seq:
|
||||||
@ -68,7 +65,7 @@ module Yawpa
|
|||||||
if param =~ /^--([^=]+)(?:=(.+))?$/
|
if param =~ /^--([^=]+)(?:=(.+))?$/
|
||||||
param_name, val = $1, $2
|
param_name, val = $1, $2
|
||||||
if options[param_name].nil?
|
if options[param_name].nil?
|
||||||
raise UnknownOptionException.new("Unknown option '#{param_name}'")
|
raise ArgumentParsingException.new("Unknown option '#{param_name}'")
|
||||||
end
|
end
|
||||||
opt_config = options[param_name]
|
opt_config = options[param_name]
|
||||||
param_key = opt_config[:key]
|
param_key = opt_config[:key]
|
||||||
@ -84,7 +81,7 @@ module Yawpa
|
|||||||
while short_idx < short_flags.length
|
while short_idx < short_flags.length
|
||||||
opt_config = _find_opt_config_by_short_name(options, short_flags[short_idx])
|
opt_config = _find_opt_config_by_short_name(options, short_flags[short_idx])
|
||||||
if opt_config.nil?
|
if opt_config.nil?
|
||||||
raise UnknownOptionException.new("Unknown option '-#{short_flags[short_idx]}'")
|
raise ArgumentParsingException.new("Unknown option '-#{short_flags[short_idx]}'")
|
||||||
end
|
end
|
||||||
param_key = opt_config[:key]
|
param_key = opt_config[:key]
|
||||||
if opt_config[:nargs].last == 0
|
if opt_config[:nargs].last == 0
|
||||||
@ -133,7 +130,7 @@ module Yawpa
|
|||||||
n_gathered += 1
|
n_gathered += 1
|
||||||
end
|
end
|
||||||
if n_gathered < nargs.first
|
if n_gathered < nargs.first
|
||||||
raise InvalidArgumentsException.new("Not enough arguments supplied for option '#{param_key}'")
|
raise ArgumentParsingException.new("Not enough arguments supplied for option '#{param_key}'")
|
||||||
end
|
end
|
||||||
num_indices_used
|
num_indices_used
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user