diff --git a/lib/yawpa.rb b/lib/yawpa.rb index 8d2cb46..ea8b1a2 100644 --- a/lib/yawpa.rb +++ b/lib/yawpa.rb @@ -27,30 +27,34 @@ module Yawpa # Example +options+: # # { - # version: {}, + # version: nil, # verbose: {short: 'v'}, # server: {nargs: (1..2)}, # username: {nargs: 1}, # password: {nargs: 1}, - # color: {boolean: true}, + # color: :boolean, # } # # 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] # 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). + # 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: + # - +: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] # Optional flags dictating how {.parse} should do its job. # @option flags [Boolean] :posix_order @@ -162,6 +166,8 @@ module Yawpa def self._massage_options(options) {}.tap do |newopts| options.each_pair do |k, v| + v = {} if v.nil? + v = {boolean: true} if v == :boolean newkey = k.to_s newopts[newkey] = {key: k} nargs = v[:nargs] || 0 diff --git a/spec/yawpa_spec.rb b/spec/yawpa_spec.rb index 92f6669..4fc6afe 100644 --- a/spec/yawpa_spec.rb +++ b/spec/yawpa_spec.rb @@ -19,7 +19,7 @@ describe Yawpa do it "returns boolean options which are set" do options = { one: {}, - two: {}, + two: nil, three: {}, } params = ['--one', 'arg', '--two', 'arg2'] @@ -205,7 +205,7 @@ describe Yawpa do it "ignores options after arguments in posix_order mode" do options = { one: {}, - two: {}, + two: nil, } params = ['--one', 'arg', '--two'] opts, args = Yawpa.parse(params, options, posix_order: true) @@ -216,7 +216,8 @@ describe Yawpa do it "supports :boolean option flag" do options = { - push: {boolean: true}, + push: :boolean, + pull: {boolean: true}, } opts, args = Yawpa.parse(%w[hi], options) @@ -227,8 +228,8 @@ describe Yawpa do expect(opts).to eq(push: true) expect(args).to eq(%w[one two]) - opts, args = Yawpa.parse(%w[arg --nopush], options) - expect(opts).to eq(push: false) + opts, args = Yawpa.parse(%w[arg --nopush --pull], options) + expect(opts).to eq(push: false, pull: true) expect(args).to eq(%w[arg]) end end