Skip to content

Commit

Permalink
Merge pull request #15 from defunkt/remove-1.8.6-stuff
Browse files Browse the repository at this point in the history
Remove version-specific hacks.
  • Loading branch information
gaustin committed Dec 31, 2014
2 parents d543415 + f1c624d commit 807541c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 32 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
0.2.0:
- Removed some version specific hacks. Including 1.8.6 support.
0.1.7:
- Fix bug related to NilClass#to_h
- Simplified packaging process: Now possible to package with an up-to-date system.
Expand Down
48 changes: 18 additions & 30 deletions lib/choice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@
require 'choice/writer'
require 'choice/lazyhash'

if RUBY_VERSION < "1.9"
class Hash
alias_method(:key, :index) unless method_defined?(:key)
end
end

#
# Usage of this module is lovingly detailed in the README file.
#
module Choice
module Choice
extend self

# The main method, which defines the options
Expand All @@ -24,7 +18,7 @@ def options(hash = {}, &block)
# Setup all instance variables
reset! if hash.empty?
@@args ||= ARGV

# Eval the passed block to define the options.
instance_eval(&block) if block_given?

Expand All @@ -36,13 +30,13 @@ def options(hash = {}, &block)
def options_from_hash(options_hash)
options_hash.each do |name, definition|
option = Option.new
definition.each do |key, value|
definition.each do |key, value|
Array(value).each { |hit| option.send(key, hit) }
end
@@options << [name.to_s, option]
end
end

# Return an array representing the rest of the command line arguments
def rest
@@rest
Expand Down Expand Up @@ -75,20 +69,14 @@ def separator(str)
# Define the banner, header, footer methods. All are just getters/setters
# of class variables.
%w[banner header footer].each do |method|
define_method(method) do |string|
define_method(method) do |string=nil|
variable = "@@#{method}"
return class_variable_get(variable) if string.nil?
val = class_variable_get(variable) || ''
class_variable_set(variable, val << string)
end

if RUBY_VERSION > "1.9"
original_method = "original_#{method}"
alias_method original_method, method
eval "def #{method}(string=nil); #{original_method}(string); end"
end
end

# Parse the provided args against the defined options.
def parse #:nodoc:
# Do nothing if options are not defined.
Expand All @@ -99,7 +87,7 @@ def parse #:nodoc:
help
else
begin
# Delegate parsing to our parser class, passing it our defined
# Delegate parsing to our parser class, passing it our defined
# options and the passed arguments.
@@choices, @@rest = Parser.parse(@@options, @@args)
@@choices = LazyHash.new(@@choices)
Expand All @@ -109,19 +97,19 @@ def parse #:nodoc:
end
end
end

# Did we already parse the arguments?
def parsed? #:nodoc:
@@choices ||= false
end

# Print the help screen by calling our Writer object
def help #:nodoc:
Writer.help( { :banner => @@banner, :header => @@header,
:options => @@options, :footer => @@footer },
Writer.help( { :banner => @@banner, :header => @@header,
:options => @@options, :footer => @@footer },
output_to, exit_on_help? )
end

# Set the args, potentially to something other than ARGV.
def args=(args) #:nodoc:
@@args = args.dup.map { |a| a + '' }
Expand All @@ -132,11 +120,11 @@ def args=(args) #:nodoc:
def args #:nodoc:
@@args
end

# Returns the arguments that follow an argument
def args_of(opt)
args_of_opt = []

# Return an array of the arguments between opt and the next option,
# which all start with "-"
@@args.slice(@@args.index(opt)+1, @@args.length).select do |arg|
Expand All @@ -148,24 +136,24 @@ def args_of(opt)
end
args_of_opt
end

# You can choose to not kill the script after the help screen is printed.
def dont_exit_on_help=(val) #:nodoc:
@@exit = true
end

# Do we want to exit on help?
def exit_on_help? #:nodoc:
@@exit rescue false
end

# If we want to write to somewhere other than STDOUT.
def output_to(target = nil) #:nodoc:
@@output_to ||= STDOUT
return @@output_to if target.nil?
@@output_to = target
end

# Reset all the class variables.
def reset! #:nodoc:
@@args = false
Expand Down
4 changes: 2 additions & 2 deletions lib/choice/version.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Choice
module Version #:nodoc:
MAJOR = 0
MINOR = 1
TINY = 7
MINOR = 2
TINY = 0
STRING = [MAJOR, MINOR, TINY] * '.'
end
end

0 comments on commit 807541c

Please sign in to comment.