clean up class method declaration slightly using class << self syntax
This commit is contained in:
parent
6cf70753f4
commit
e852d2755c
37
lib/yawpa.rb
37
lib/yawpa.rb
@ -13,16 +13,19 @@ require "yawpa/version"
|
|||||||
# - Options can be defined with a range specifying the allowed number of
|
# - Options can be defined with a range specifying the allowed number of
|
||||||
# parameters
|
# parameters
|
||||||
module Yawpa
|
module Yawpa
|
||||||
|
|
||||||
# Exception class raised when an unknown option is observed.
|
# Exception class raised when an unknown option is observed.
|
||||||
class ArgumentParsingException < Exception; end
|
class ArgumentParsingException < Exception; end
|
||||||
|
|
||||||
|
class << self
|
||||||
|
|
||||||
# Parse input parameters looking for options according to rules given in
|
# Parse input parameters looking for options according to rules given in
|
||||||
# flags.
|
# flags.
|
||||||
# Syntax:
|
# Syntax:
|
||||||
# opts, args = parse(params, options, flags = {})
|
# opts, args = parse(params, options, flags = {})
|
||||||
#
|
#
|
||||||
# An ArgumentParsingException will be raised if an unknown option is observed
|
# An ArgumentParsingException will be raised if an unknown option is
|
||||||
# or insufficient arguments are present for an option.
|
# observed or insufficient arguments are present for an option.
|
||||||
#
|
#
|
||||||
# Example +options+:
|
# Example +options+:
|
||||||
#
|
#
|
||||||
@ -50,11 +53,12 @@ module Yawpa
|
|||||||
# +{boolean: true}+)
|
# +{boolean: true}+)
|
||||||
# Hash::
|
# Hash::
|
||||||
# Possible option flags:
|
# Possible option flags:
|
||||||
# - +:short+: specify a short option letter to associate with the long option
|
# - +:short+: specify a short option letter to associate with the long
|
||||||
|
# option
|
||||||
# - +:nargs+: specify an exact number or range of possible numbers of
|
# - +:nargs+: specify an exact number or range of possible numbers of
|
||||||
# arguments to the option
|
# arguments to the option
|
||||||
# - +:boolean+: if true, specify that the option is a toggleable boolean
|
# - +:boolean+: if true, specify that the option is a toggleable
|
||||||
# option and allow a prefix of "no" to turn it off.
|
# boolean option and allow a prefix of "no" to turn it off.
|
||||||
# @param flags [Hash]
|
# @param flags [Hash]
|
||||||
# Optional flags dictating how {.parse} should do its job.
|
# Optional flags dictating how {.parse} should do its job.
|
||||||
# @option flags [Boolean] :posix_order
|
# @option flags [Boolean] :posix_order
|
||||||
@ -71,7 +75,7 @@ module Yawpa
|
|||||||
# parameters (if +:posix_order+ was passed in +flags+, this array might
|
# parameters (if +:posix_order+ was passed in +flags+, this array might
|
||||||
# contain further options that were not processed after observing a
|
# contain further options that were not processed after observing a
|
||||||
# non-option parameters).
|
# non-option parameters).
|
||||||
def self.parse(params, options, flags = {})
|
def parse(params, options, flags = {})
|
||||||
options = _massage_options(options)
|
options = _massage_options(options)
|
||||||
opts = {}
|
opts = {}
|
||||||
args = []
|
args = []
|
||||||
@ -114,7 +118,12 @@ module Yawpa
|
|||||||
opts[param_key] = true
|
opts[param_key] = true
|
||||||
else
|
else
|
||||||
opts[param_key] = []
|
opts[param_key] = []
|
||||||
i += _gather(opt_config[:nargs], i + 1, params, short_flags[short_idx + 1, short_flags.length], param_key, opts[param_key])
|
i += _gather(opt_config[:nargs],
|
||||||
|
i + 1,
|
||||||
|
params,
|
||||||
|
short_flags[short_idx + 1, short_flags.length],
|
||||||
|
param_key,
|
||||||
|
opts[param_key])
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
short_idx += 1
|
short_idx += 1
|
||||||
@ -138,8 +147,10 @@ module Yawpa
|
|||||||
return [opts, args]
|
return [opts, args]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
# Internal helper method to gather arguments for an option
|
# Internal helper method to gather arguments for an option
|
||||||
def self._gather(nargs, start_idx, params, initial, param_key, result)
|
def _gather(nargs, start_idx, params, initial, param_key, result)
|
||||||
n_gathered = 0
|
n_gathered = 0
|
||||||
if initial and initial != ''
|
if initial and initial != ''
|
||||||
result << initial
|
result << initial
|
||||||
@ -160,10 +171,9 @@ module Yawpa
|
|||||||
end
|
end
|
||||||
num_indices_used
|
num_indices_used
|
||||||
end
|
end
|
||||||
private_class_method :_gather
|
|
||||||
|
|
||||||
# Internal helper method to format the options in a consistent format
|
# Internal helper method to format the options in a consistent format
|
||||||
def self._massage_options(options)
|
def _massage_options(options)
|
||||||
{}.tap do |newopts|
|
{}.tap do |newopts|
|
||||||
options.each_pair do |k, v|
|
options.each_pair do |k, v|
|
||||||
v = {} if v.nil?
|
v = {} if v.nil?
|
||||||
@ -178,14 +188,15 @@ module Yawpa
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
private_class_method :_massage_options
|
|
||||||
|
|
||||||
# Internal helper method to find an option configuration by short name
|
# Internal helper method to find an option configuration by short name
|
||||||
def self._find_opt_config_by_short_name(options, short_name)
|
def _find_opt_config_by_short_name(options, short_name)
|
||||||
options.each_pair do |k, v|
|
options.each_pair do |k, v|
|
||||||
return v if v[:short] == short_name
|
return v if v[:short] == short_name
|
||||||
end
|
end
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
private_class_method :_find_opt_config_by_short_name
|
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user