Class: OptionParser

Inherits:
Object
  • Object
show all
Defined in:
lib/qu/utils.rb

Overview

Add the ability to specify switches as required to OptionParser

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#missing_switchesObject (readonly)

An array of messages describing any missing required switches



146
147
148
# File 'lib/qu/utils.rb', line 146

def missing_switches
  @missing_switches
end

Instance Method Details

#make_switch(opts, block = nil) ⇒ Object

Wrapper for OptionParser::make_switch to allow for required switches



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/qu/utils.rb', line 158

def make_switch(opts, block = nil)

  # Test if a switch is required
  required = opts.delete(:required)

  return_values = pickled_make_switch(opts, block)

  # Make sure required switches are given
  if required 
    short = return_values[1][0].nil? ? nil : "-#{return_values[1][0]}"
    long = return_values[2][0].nil? ? nil : "--#{return_values[2][0]}"
    
    if !(default_argv.include?(short) || default_argv.include?(long))
      @missing_switches ||= [] # Should be placed in initialize if incorporated into Ruby proper
    
      # Ugly and hard to read, should figure out a prettier way of doing this
      @missing_switches << "Missing switch: #{short if !short.nil?}#{" or " if !short.nil? && !long.nil?}#{long if !long.nil?}"
    end
  end
  
  return return_values
end

#missing_switches?Boolean

Convenience method to test if we’re missing any required switches

Returns:

  • (Boolean)


149
150
151
# File 'lib/qu/utils.rb', line 149

def missing_switches?
  !@missing_switches.nil?
end

#pickled_make_switchObject

Alias the OptionParser::make_switch function (instead of directly modifying it like I did in 0.1.0)



155
# File 'lib/qu/utils.rb', line 155

alias :pickled_make_switch :make_switch