Compare commits
No commits in common. "master" and "v1.1.0" have entirely different histories.
50
Gemfile.lock
50
Gemfile.lock
@ -1,35 +1,37 @@
|
|||||||
PATH
|
PATH
|
||||||
remote: .
|
remote: .
|
||||||
specs:
|
specs:
|
||||||
yawpa (1.2.0)
|
yawpa (1.1.0)
|
||||||
|
|
||||||
GEM
|
GEM
|
||||||
remote: https://rubygems.org/
|
remote: https://rubygems.org/
|
||||||
specs:
|
specs:
|
||||||
diff-lcs (1.3)
|
diff-lcs (1.2.5)
|
||||||
docile (1.1.5)
|
docile (1.1.5)
|
||||||
json (2.1.0)
|
json (1.8.1)
|
||||||
rake (12.0.0)
|
multi_json (1.10.1)
|
||||||
rdoc (5.1.0)
|
rake (10.3.2)
|
||||||
rspec (3.6.0)
|
rdoc (4.1.1)
|
||||||
rspec-core (~> 3.6.0)
|
json (~> 1.4)
|
||||||
rspec-expectations (~> 3.6.0)
|
redcarpet (3.1.2)
|
||||||
rspec-mocks (~> 3.6.0)
|
rspec (3.0.0)
|
||||||
rspec-core (3.6.0)
|
rspec-core (~> 3.0.0)
|
||||||
rspec-support (~> 3.6.0)
|
rspec-expectations (~> 3.0.0)
|
||||||
rspec-expectations (3.6.0)
|
rspec-mocks (~> 3.0.0)
|
||||||
|
rspec-core (3.0.2)
|
||||||
|
rspec-support (~> 3.0.0)
|
||||||
|
rspec-expectations (3.0.2)
|
||||||
diff-lcs (>= 1.2.0, < 2.0)
|
diff-lcs (>= 1.2.0, < 2.0)
|
||||||
rspec-support (~> 3.6.0)
|
rspec-support (~> 3.0.0)
|
||||||
rspec-mocks (3.6.0)
|
rspec-mocks (3.0.2)
|
||||||
diff-lcs (>= 1.2.0, < 2.0)
|
rspec-support (~> 3.0.0)
|
||||||
rspec-support (~> 3.6.0)
|
rspec-support (3.0.2)
|
||||||
rspec-support (3.6.0)
|
simplecov (0.8.2)
|
||||||
simplecov (0.15.0)
|
|
||||||
docile (~> 1.1.0)
|
docile (~> 1.1.0)
|
||||||
json (>= 1.8, < 3)
|
multi_json
|
||||||
simplecov-html (~> 0.10.0)
|
simplecov-html (~> 0.8.0)
|
||||||
simplecov-html (0.10.2)
|
simplecov-html (0.8.0)
|
||||||
yard (0.9.9)
|
yard (0.8.7.4)
|
||||||
|
|
||||||
PLATFORMS
|
PLATFORMS
|
||||||
ruby
|
ruby
|
||||||
@ -37,10 +39,8 @@ PLATFORMS
|
|||||||
DEPENDENCIES
|
DEPENDENCIES
|
||||||
rake
|
rake
|
||||||
rdoc
|
rdoc
|
||||||
|
redcarpet
|
||||||
rspec
|
rspec
|
||||||
simplecov
|
simplecov
|
||||||
yard
|
yard
|
||||||
yawpa!
|
yawpa!
|
||||||
|
|
||||||
BUNDLED WITH
|
|
||||||
1.10.6
|
|
||||||
|
@ -109,10 +109,6 @@ options that were not processed after observing a non-option parameters).
|
|||||||
|
|
||||||
## Release Notes
|
## Release Notes
|
||||||
|
|
||||||
### v1.2.0
|
|
||||||
|
|
||||||
- Always return non-frozen strings
|
|
||||||
|
|
||||||
### v1.1.0
|
### v1.1.0
|
||||||
|
|
||||||
- Add `:boolean` option flag.
|
- Add `:boolean` option flag.
|
||||||
|
45
lib/yawpa.rb
45
lib/yawpa.rb
@ -13,19 +13,16 @@ 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
|
# An ArgumentParsingException will be raised if an unknown option is observed
|
||||||
# observed or insufficient arguments are present for an option.
|
# or insufficient arguments are present for an option.
|
||||||
#
|
#
|
||||||
# Example +options+:
|
# Example +options+:
|
||||||
#
|
#
|
||||||
@ -53,12 +50,11 @@ 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
|
# - +:short+: specify a short option letter to associate with the long option
|
||||||
# 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+: if true, specify that the option is a toggleable boolean
|
||||||
# boolean option and allow a prefix of "no" to turn it off.
|
# 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
|
||||||
@ -75,7 +71,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 parse(params, options, flags = {})
|
def self.parse(params, options, flags = {})
|
||||||
options = _massage_options(options)
|
options = _massage_options(options)
|
||||||
opts = {}
|
opts = {}
|
||||||
args = []
|
args = []
|
||||||
@ -118,21 +114,16 @@ module Yawpa
|
|||||||
opts[param_key] = true
|
opts[param_key] = true
|
||||||
else
|
else
|
||||||
opts[param_key] = []
|
opts[param_key] = []
|
||||||
i += _gather(opt_config[:nargs],
|
i += _gather(opt_config[:nargs], i + 1, params, short_flags[short_idx + 1, short_flags.length], param_key, opts[param_key])
|
||||||
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
|
||||||
end
|
end
|
||||||
elsif flags[:posix_order]
|
elsif flags[:posix_order]
|
||||||
args = params[i, params.length].map(&:dup)
|
args = params[i, params.length]
|
||||||
break
|
break
|
||||||
else
|
else
|
||||||
args << params[i].dup
|
args << params[i]
|
||||||
end
|
end
|
||||||
i += 1
|
i += 1
|
||||||
end
|
end
|
||||||
@ -147,10 +138,8 @@ 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 _gather(nargs, start_idx, params, initial, param_key, result)
|
def self._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
|
||||||
@ -161,7 +150,7 @@ module Yawpa
|
|||||||
while n_gathered < nargs.last and
|
while n_gathered < nargs.last and
|
||||||
index < params.length and
|
index < params.length and
|
||||||
params[index][0] != '-' do
|
params[index][0] != '-' do
|
||||||
result << params[index].dup
|
result << params[index]
|
||||||
index += 1
|
index += 1
|
||||||
num_indices_used += 1
|
num_indices_used += 1
|
||||||
n_gathered += 1
|
n_gathered += 1
|
||||||
@ -171,9 +160,10 @@ 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 _massage_options(options)
|
def self._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?
|
||||||
@ -181,22 +171,21 @@ module Yawpa
|
|||||||
newkey = k.to_s
|
newkey = k.to_s
|
||||||
newopts[newkey] = {key: k}
|
newopts[newkey] = {key: k}
|
||||||
nargs = v[:nargs] || 0
|
nargs = v[:nargs] || 0
|
||||||
nargs = (nargs..nargs) if nargs.is_a?(Integer)
|
nargs = (nargs..nargs) if nargs.is_a?(Fixnum)
|
||||||
newopts[newkey][:nargs] = nargs
|
newopts[newkey][:nargs] = nargs
|
||||||
newopts[newkey][:short] = v[:short] || ''
|
newopts[newkey][:short] = v[:short] || ''
|
||||||
newopts[newkey][:boolean] = v[:boolean]
|
newopts[newkey][:boolean] = v[:boolean]
|
||||||
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 _find_opt_config_by_short_name(options, short_name)
|
def self._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
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
module Yawpa
|
module Yawpa
|
||||||
# gem version
|
# gem version
|
||||||
VERSION = "1.3.0"
|
VERSION = "1.1.0"
|
||||||
end
|
end
|
||||||
|
@ -11,7 +11,7 @@ describe Yawpa do
|
|||||||
it "raises an exception when an invalid option is passed" do
|
it "raises an exception when an invalid option is passed" do
|
||||||
options = { }
|
options = { }
|
||||||
params = ['one', '--option', 'two']
|
params = ['one', '--option', 'two']
|
||||||
expect { Yawpa.parse(params, options) }.to raise_error(Yawpa::ArgumentParsingException, /Unknown option/)
|
expect { Yawpa.parse(params, options) }.to raise_error
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns boolean options which are set" do
|
it "returns boolean options which are set" do
|
||||||
@ -53,7 +53,7 @@ describe Yawpa do
|
|||||||
opt: {nargs: 2},
|
opt: {nargs: 2},
|
||||||
}
|
}
|
||||||
params = ['--opt', 'val']
|
params = ['--opt', 'val']
|
||||||
expect { Yawpa.parse(params, options) }.to raise_error(Yawpa::ArgumentParsingException, /Not enough arguments supplied/)
|
expect { Yawpa.parse(params, options) }.to raise_error
|
||||||
end
|
end
|
||||||
|
|
||||||
it "uses --opt=val syntax for an option's value" do
|
it "uses --opt=val syntax for an option's value" do
|
||||||
@ -179,7 +179,7 @@ describe Yawpa do
|
|||||||
a: {short: 'a'},
|
a: {short: 'a'},
|
||||||
}
|
}
|
||||||
params = ['-ab']
|
params = ['-ab']
|
||||||
expect { Yawpa.parse(params, options) }.to raise_error(Yawpa::ArgumentParsingException, /Unknown option/)
|
expect { Yawpa.parse(params, options) }.to raise_error
|
||||||
end
|
end
|
||||||
|
|
||||||
it "raises an error when not enough arguments are given to short option" do
|
it "raises an error when not enough arguments are given to short option" do
|
||||||
@ -187,7 +187,7 @@ describe Yawpa do
|
|||||||
a: {nargs: 1, short: 'a'},
|
a: {nargs: 1, short: 'a'},
|
||||||
}
|
}
|
||||||
params = ['-a']
|
params = ['-a']
|
||||||
expect { Yawpa.parse(params, options) }.to raise_error(Yawpa::ArgumentParsingException, /Not enough arguments supplied/)
|
expect { Yawpa.parse(params, options) }.to raise_error
|
||||||
end
|
end
|
||||||
|
|
||||||
it "overwrites option value when short option used after long" do
|
it "overwrites option value when short option used after long" do
|
||||||
@ -230,32 +230,5 @@ describe Yawpa do
|
|||||||
expect(opts).to eq(push: false, pull: true)
|
expect(opts).to eq(push: false, pull: true)
|
||||||
expect(args).to eq(%w[arg])
|
expect(args).to eq(%w[arg])
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns non-frozen strings" do
|
|
||||||
options = {
|
|
||||||
o1: {nargs: 1, short: "1"},
|
|
||||||
o2: {nargs: 1, short: "2"},
|
|
||||||
o3: {nargs: 1, short: "3"},
|
|
||||||
o4: {nargs: 1, short: "4"},
|
|
||||||
}
|
|
||||||
|
|
||||||
arguments = %w[--o1=one --o2 two -3 three -4four arg].map(&:freeze)
|
|
||||||
|
|
||||||
opts, args = Yawpa.parse(arguments, options)
|
|
||||||
expect(opts[:o1].frozen?).to be_falsey
|
|
||||||
expect{opts[:o1].sub!(/./, '-')}.to_not raise_error
|
|
||||||
expect(opts[:o2].frozen?).to be_falsey
|
|
||||||
expect{opts[:o2].sub!(/./, '-')}.to_not raise_error
|
|
||||||
expect(opts[:o3].frozen?).to be_falsey
|
|
||||||
expect{opts[:o3].sub!(/./, '-')}.to_not raise_error
|
|
||||||
expect(opts[:o4].frozen?).to be_falsey
|
|
||||||
expect{opts[:o4].sub!(/./, '-')}.to_not raise_error
|
|
||||||
expect(args[0].frozen?).to be_falsey
|
|
||||||
expect{args[0].sub!(/./, '-')}.to_not raise_error
|
|
||||||
|
|
||||||
opts, args = Yawpa.parse(arguments, options, posix_order: true)
|
|
||||||
expect(args[0].frozen?).to be_falsey
|
|
||||||
expect{args[0].sub!(/./, '-')}.to_not raise_error
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -20,4 +20,5 @@ Gem::Specification.new do |gem|
|
|||||||
gem.add_development_dependency "rake"
|
gem.add_development_dependency "rake"
|
||||||
gem.add_development_dependency "rdoc"
|
gem.add_development_dependency "rdoc"
|
||||||
gem.add_development_dependency "yard"
|
gem.add_development_dependency "yard"
|
||||||
|
gem.add_development_dependency "redcarpet"
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user