Class: OptionParser

Inherits:
Object
  • Object
show all
Defined in:
lib/pickled_optparse/pickled_optparse.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



7
8
9
# File 'lib/pickled_optparse/pickled_optparse.rb', line 7

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



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pickled_optparse/pickled_optparse.rb', line 19

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)


10
11
12
# File 'lib/pickled_optparse/pickled_optparse.rb', line 10

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)



16
# File 'lib/pickled_optparse/pickled_optparse.rb', line 16

alias :pickled_make_switch :make_switch