clean up class method declaration slightly using class << self syntax

This commit is contained in:
Josh Holtrop 2016-04-19 08:37:39 -04:00
parent 6cf70753f4
commit e852d2755c

View File

@ -13,16 +13,19 @@ require "yawpa/version"
# - Options can be defined with a range specifying the allowed number of
# parameters
module Yawpa
# Exception class raised when an unknown option is observed.
class ArgumentParsingException < Exception; end
class << self
# Parse input parameters looking for options according to rules given in
# flags.
# Syntax:
# opts, args = parse(params, options, flags = {})
#
# An ArgumentParsingException will be raised if an unknown option is observed
# or insufficient arguments are present for an option.
# An ArgumentParsingException will be raised if an unknown option is
# observed or insufficient arguments are present for an option.
#
# Example +options+:
#
@ -50,11 +53,12 @@ module Yawpa
# +{boolean: true}+)
# Hash::
# 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
# arguments to the option
# - +:boolean+: if true, specify that the option is a toggleable boolean
# option and allow a prefix of "no" to turn it off.
# - +:boolean+: if true, specify that the option is a toggleable
# boolean option and allow a prefix of "no" to turn it off.
# @param flags [Hash]
# Optional flags dictating how {.parse} should do its job.
# @option flags [Boolean] :posix_order
@ -71,7 +75,7 @@ module Yawpa
# parameters (if +:posix_order+ was passed in +flags+, this array might
# contain further options that were not processed after observing a
# non-option parameters).
def self.parse(params, options, flags = {})
def parse(params, options, flags = {})
options = _massage_options(options)
opts = {}
args = []
@ -114,7 +118,12 @@ module Yawpa
opts[param_key] = true
else
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
end
short_idx += 1
@ -138,8 +147,10 @@ module Yawpa
return [opts, args]
end
private
# 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
if initial and initial != ''
result << initial
@ -160,10 +171,9 @@ module Yawpa
end
num_indices_used
end
private_class_method :_gather
# Internal helper method to format the options in a consistent format
def self._massage_options(options)
def _massage_options(options)
{}.tap do |newopts|
options.each_pair do |k, v|
v = {} if v.nil?
@ -178,14 +188,15 @@ module Yawpa
end
end
end
private_class_method :_massage_options
# 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|
return v if v[:short] == short_name
end
nil
end
private_class_method :_find_opt_config_by_short_name
end
end