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

Instance Method Details

#[](choice) ⇒ Object

Shortcut access to Choice.choices



51
52
53
# File 'lib/choice.rb', line 51

def [](choice)
  choices[choice]
end

#argsObject

Return the args.



121
122
123
# File 'lib/choice.rb', line 121

def args #:nodoc:
  @@args
end

#args=(args) ⇒ Object

Set the args, potentially to something other than ARGV.



115
116
117
118
# File 'lib/choice.rb', line 115

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



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/choice.rb', line 126

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

#choicesObject

Returns a hash representing options passed in via the command line.



46
47
48
# File 'lib/choice.rb', line 46

def choices
  @@choices
end

#dont_exit_on_help=(val) ⇒ Object

You can choose to not kill the script after the help screen is printed.



142
143
144
# File 'lib/choice.rb', line 142

def dont_exit_on_help=(val) #:nodoc:
  @@exit = true
end

#exit_on_help?Boolean

Do we want to exit on help?

Returns:

  • (Boolean)


147
148
149
# File 'lib/choice.rb', line 147

def exit_on_help? #:nodoc:
  @@exit rescue false
end

#helpObject

Print the help screen by calling our Writer object



108
109
110
111
112
# File 'lib/choice.rb', line 108

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.



56
57
58
59
60
# 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) ⇒ 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 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) ⇒ Object

Set options from a hash, shorthand style



30
31
32
33
34
35
36
37
38
# 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

#output_to(target = nil) ⇒ Object

If we want to write to somewhere other than STDOUT.



152
153
154
155
156
# File 'lib/choice.rb', line 152

def output_to(target = nil) #:nodoc:
  @@output_to ||= STDOUT
  return @@output_to if target.nil?
  @@output_to = target
end

#parseObject

Parse the provided args against the defined options.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/choice.rb', line 82

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')
    help
  else
    begin
      # 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)
    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?

Returns:

  • (Boolean)


103
104
105
# File 'lib/choice.rb', line 103

def parsed? #:nodoc:
  @@choices ||= false
end

#reset!Object

Reset all the class variables.



159
160
161
162
163
164
165
# File 'lib/choice.rb', line 159

def reset! #:nodoc:
  @@args    = false
  @@banner  = false
  @@header  = Array.new
  @@options = Array.new
  @@footer  = Array.new
end

#restObject

Return an array representing the rest of the command line arguments



41
42
43
# File 'lib/choice.rb', line 41

def rest
  @@rest
end

#separator(str) ⇒ Object

Separators are text displayed by –help within the options block.



63
64
65
66
67
# 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