allow nil and :boolean to be option configuration values
This commit is contained in:
parent
0eb754814e
commit
b79acd049f
30
lib/yawpa.rb
30
lib/yawpa.rb
@ -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.
|
|
||||||
#
|
|
||||||
# Possible option flags:
|
|
||||||
# - +: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.
|
|
||||||
#
|
#
|
||||||
# @param params [Array]
|
# @param params [Array]
|
||||||
# List of program parameters to parse.
|
# List of program parameters to parse.
|
||||||
# @param options [Hash]
|
# @param options [Hash]
|
||||||
# Hash containing the long option names as keys, and hashes containing
|
# Hash containing the long option names as keys, and values containing
|
||||||
# special flags for the options as values (example above).
|
# 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:
|
||||||
|
# - +: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.
|
||||||
# @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
|
||||||
|
@ -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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user