Skip to content

Commit

Permalink
Merge pull request #22 from jekyll/patch-for-accepting-constants
Browse files Browse the repository at this point in the history
  • Loading branch information
parkr committed Feb 21, 2014
2 parents 6e38fc5 + 25e6534 commit 9e145ac
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
49 changes: 28 additions & 21 deletions lib/mercenary/option.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
module Mercenary
class Option
attr_reader :config_key, :description, :switches
attr_reader :config_key, :description, :short, :long, :return_type

# Public: Create a new Option
#
# config_key - the key in the config hash to which the value of this option will map
# info - an array containing first the switches, then a description of the option
# config_key - the key in the config hash to which the value of this option
# will map
# info - an array containing first the switches, then an optional
# return type (e.g. Array), then a description of the option
#
# Returns nothing
def initialize(config_key, info)
@config_key = config_key
@description = info.last unless info.last.start_with?("-")
set_switches(info.take(info.size - [@description].reject(&:nil?).size))
@config_key = config_key
while arg = info.shift
begin
@return_type = Object.const_get("#{arg}")
next
rescue NameError
end
if arg.start_with?("-")
if arg.start_with?("--")
@long = arg
else
@short = arg
end
next
end
@description = arg
end
end

# Public: Fetch the array containing the info OptionParser is interested in
#
# Returns the array which OptionParser#on wants
def for_option_parser
[switches.reject(&:empty?), description].reject{ |o| o.nil? || o.empty? }.flatten
[short, long, return_type, description].flatten.reject{ |o| o.to_s.empty? }
end

# Public: Build a string representation of this option including the
Expand Down Expand Up @@ -59,21 +75,12 @@ def eql?(other)
end.all?
end

private

# Private: Set the full switches array, ensuring the first element is the
# short switch and the second element is the long switch
# Public: Fetch an array of switches, including the short and long versions
#
# Returns the corrected switches array
def set_switches(switches)
if switches.size < 2
if switches.first.start_with?("--")
switches.unshift ""
else
switches << ""
end
end
@switches = switches
# Returns an array of two strings. An empty string represents no switch in
# that position.
def switches
[short, long].map(&:to_s)
end

end
Expand Down
2 changes: 1 addition & 1 deletion spec/command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
option = Mercenary::Option.new(name, opts)
command.option name, *opts
expect(command.options).to eql([option])
expect(command.map).to include({option.hash => name})
expect(command.map.values).to include(name)
end

it "knows its full name" do
Expand Down

0 comments on commit 9e145ac

Please sign in to comment.