Module: Choice
- Extended by:
- Choice
- Included in:
- Choice
- Defined in:
- lib/choice.rb,
lib/choice/option.rb,
lib/choice/parser.rb,
lib/choice/writer.rb,
lib/choice/version.rb,
lib/choice/lazyhash.rb
Overview
Usage of this module is lovingly detailed in the README file.
Defined Under Namespace
Modules: Parser, Version, Writer Classes: LazyHash, Option
Instance Method Summary collapse
-
#args ⇒ Object
Return the args.
-
#args=(args) ⇒ Object
Set the args, potentially to something other than ARGV.
-
#args_of(opt) ⇒ Object
Returns the arguments that follow an argument.
-
#choices ⇒ Object
Returns a hash representing options passed in via the command line.
-
#dont_exit_on_help=(val) ⇒ Object
You can choose to not kill the script after the help screen is printed.
-
#exit_on_help? ⇒ Boolean
Do we want to exit on help?.
-
#help ⇒ Object
Print the help screen by calling our Writer object.
-
#option(opt, options = {}, &block) ⇒ Object
Defines an option.
-
#options(hash = {}, &block) ⇒ Object
The main method, which defines the options.
-
#options_from_hash(options_hash) ⇒ Object
Set options from a hash, shorthand style.
-
#output_to(target = nil) ⇒ Object
If we want to write to somewhere other than STDOUT.
-
#parse ⇒ Object
Parse the provided args against the defined options.
-
#parsed? ⇒ Boolean
Did we already parse the arguments?.
-
#reset! ⇒ Object
Reset all the class variables.
-
#separator(str) ⇒ Object
Separators are text displayed by –help within the options block.
Instance Method Details
#args ⇒ Object
Return the args.
110 111 112 |
# File 'lib/choice.rb', line 110 def args #:nodoc: @@args end |
#args=(args) ⇒ Object
Set the args, potentially to something other than ARGV.
104 105 106 107 |
# File 'lib/choice.rb', line 104 def args=(args) #:nodoc: @@args = args.dup.map { |a| a + '' } parse if parsed? end |
#args_of(opt) ⇒ Object
Returns the arguments that follow an argument
115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/choice.rb', line 115 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 ⇒ Object
Returns a hash representing options passed in via the command line.
41 42 43 |
# File 'lib/choice.rb', line 41 def choices @@choices end |
#dont_exit_on_help=(val) ⇒ Object
You can choose to not kill the script after the help screen is printed.
131 132 133 |
# File 'lib/choice.rb', line 131 def dont_exit_on_help=(val) #:nodoc: @@exit = true end |
#exit_on_help? ⇒ Boolean
Do we want to exit on help?
136 137 138 |
# File 'lib/choice.rb', line 136 def exit_on_help? #:nodoc: @@exit rescue false end |
#help ⇒ Object
Print the help screen by calling our Writer object
97 98 99 100 101 |
# File 'lib/choice.rb', line 97 def help #:nodoc: Writer.help( { :banner => @@banner, :header => @@header, :options => @@options, :footer => @@footer }, output_to, exit_on_help? ) end |
#option(opt, options = {}, &block) ⇒ Object
Defines an option.
46 47 48 49 50 |
# File 'lib/choice.rb', line 46 def option(opt, = {}, &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(, &block)] end |
#options(hash = {}, &block) ⇒ Object
The main method, which defines the options
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/choice.rb', line 14 def (hash = {}, &block) # if we are passing in a hash to define our options, use that straight (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) ⇒ Object
Set options from a hash, shorthand style
30 31 32 33 34 35 36 37 38 |
# File 'lib/choice.rb', line 30 def () .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 |
#output_to(target = nil) ⇒ Object
If we want to write to somewhere other than STDOUT.
141 142 143 144 145 |
# File 'lib/choice.rb', line 141 def output_to(target = nil) #:nodoc: @@output_to ||= STDOUT return @@output_to if target.nil? @@output_to = target end |
#parse ⇒ Object
Parse the provided args against the defined options.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/choice.rb', line 72 def parse #:nodoc: # Do nothing if options are not defined. return unless @@options.size > 0 # Show help if it's anywhere in the argument list. if @@args.include?('--help') or @@args.include?('-h') help else begin # Delegate parsing to our parser class, passing it our defined # options and the passed arguments. @@choices = LazyHash.new(Parser.parse(@@options, @@args)) rescue Choice::Parser::ParseError # If we get an expected exception, show the help file. help end end end |
#parsed? ⇒ Boolean
Did we already parse the arguments?
92 93 94 |
# File 'lib/choice.rb', line 92 def parsed? #:nodoc: @@choices ||= false end |
#reset! ⇒ Object
Reset all the class variables.
148 149 150 151 152 153 154 |
# File 'lib/choice.rb', line 148 def reset! #:nodoc: @@args = false @@banner = false @@header = Array.new @@options = Array.new @@footer = Array.new end |
#separator(str) ⇒ Object
Separators are text displayed by –help within the options block.
53 54 55 56 57 |
# File 'lib/choice.rb', line 53 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 |