module Choice
Usage of this module is lovingly detailed in the README file.
Public Instance Methods
[](choice)
click to toggle source
Shortcut access to Choice.choices
# File lib/choice.rb, line 51 def [](choice) choices[choice] end
args_of(opt)
click to toggle source
Returns the arguments that follow an argument
# File lib/choice.rb, line 125 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| if arg[0].chr != "-" args_of_opt << arg else break end end args_of_opt end
choices()
click to toggle source
Returns a hash representing options passed in via the command line.
# File lib/choice.rb, line 46 def choices @@choices end
option(opt, options = {}, &block)
click to toggle source
Defines an option.
# File lib/choice.rb, line 56 def option(opt, options = {}, &block) # Notice: options is maintained as an array of arrays, the first element # the option name and the second the option object. @@options << [opt.to_s, Option.new(options, &block)] end
options(hash = {}, &block)
click to toggle source
The main method, which defines the options
# File lib/choice.rb, line 14 def options(hash = {}, &block) # if we are passing in a hash to define our options, use that straight options_from_hash(hash) unless hash.empty? # Setup all instance variables reset! if hash.empty? @@args ||= ARGV # Eval the passed block to define the options. instance_eval(&block) if block_given? # Parse what we've got. parse unless parsed? end
options_from_hash(options_hash)
click to toggle source
Set options from a hash, shorthand style
# File lib/choice.rb, line 30 def options_from_hash(options_hash) options_hash.each do |name, definition| option = Option.new definition.each do |key, value| Array(value).each { |hit| option.send(key, hit) } end @@options << [name.to_s, option] end end
rest()
click to toggle source
Return an array representing the rest of the command line arguments
# File lib/choice.rb, line 41 def rest @@rest end
separator(str)
click to toggle source
Separators are text displayed by –help within the options block.
# File lib/choice.rb, line 63 def separator(str) # We store separators as simple strings in the options array to maintain # order. They are ignored by the parser. @@options << str end