allow nil and :boolean to be option configuration values

This commit is contained in:
Josh Holtrop 2014-06-25 15:22:39 -04:00
parent 0eb754814e
commit b79acd049f
2 changed files with 24 additions and 17 deletions

View File

@ -27,30 +27,34 @@ module Yawpa
# Example +options+: # Example +options+:
# #
# { # {
# version: {}, # version: nil,
# verbose: {short: 'v'}, # verbose: {short: 'v'},
# server: {nargs: (1..2)}, # server: {nargs: (1..2)},
# username: {nargs: 1}, # username: {nargs: 1},
# password: {nargs: 1}, # password: {nargs: 1},
# color: {boolean: true}, # color: :boolean,
# } # }
# #
# The keys of the +options+ Hash can be either strings or symbols. # The keys of the +options+ Hash can be either strings or symbols.
# #
# Options that have no special flags should have an empty hash as the value.
# #
# @param params [Array]
# List of program parameters to parse.
# @param options [Hash]
# Hash containing the long option names as keys, and values containing
# special flags for the options as values (examples above).
# Possible values:
# +nil+:: No special flags for this option (equivalent to +{}+)
# +:boolean+::
# The option is a toggleable boolean option (equivalent to
# +{boolean: true}+)
# 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 boolean
# option and allow a prefix of "no" to turn it off. # option and allow a prefix of "no" to turn it off.
#
# @param params [Array]
# List of program parameters to parse.
# @param options [Hash]
# Hash containing the long option names as keys, and hashes containing
# special flags for the options as values (example above).
# @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
@ -162,6 +166,8 @@ module Yawpa
def self._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 = {boolean: true} if v == :boolean
newkey = k.to_s newkey = k.to_s
newopts[newkey] = {key: k} newopts[newkey] = {key: k}
nargs = v[:nargs] || 0 nargs = v[:nargs] || 0

View File

@ -19,7 +19,7 @@ describe Yawpa do
it "returns boolean options which are set" do it "returns boolean options which are set" do
options = { options = {
one: {}, one: {},
two: {}, two: nil,
three: {}, three: {},
} }
params = ['--one', 'arg', '--two', 'arg2'] params = ['--one', 'arg', '--two', 'arg2']
@ -205,7 +205,7 @@ describe Yawpa do
it "ignores options after arguments in posix_order mode" do it "ignores options after arguments in posix_order mode" do
options = { options = {
one: {}, one: {},
two: {}, two: nil,
} }
params = ['--one', 'arg', '--two'] params = ['--one', 'arg', '--two']
opts, args = Yawpa.parse(params, options, posix_order: true) opts, args = Yawpa.parse(params, options, posix_order: true)
@ -216,7 +216,8 @@ describe Yawpa do
it "supports :boolean option flag" do it "supports :boolean option flag" do
options = { options = {
push: {boolean: true}, push: :boolean,
pull: {boolean: true},
} }
opts, args = Yawpa.parse(%w[hi], options) opts, args = Yawpa.parse(%w[hi], options)
@ -227,8 +228,8 @@ describe Yawpa do
expect(opts).to eq(push: true) expect(opts).to eq(push: true)
expect(args).to eq(%w[one two]) expect(args).to eq(%w[one two])
opts, args = Yawpa.parse(%w[arg --nopush], options) opts, args = Yawpa.parse(%w[arg --nopush --pull], options)
expect(opts).to eq(push: false) expect(opts).to eq(push: false, pull: true)
expect(args).to eq(%w[arg]) expect(args).to eq(%w[arg])
end end
end end