Class: OptionParser

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



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

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



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/aggkit/option_parser.rb', line 22

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]}"

    unless default_argv.include?(short) || default_argv.include?(long)
      @missing_switches ||= [] # Should be placed in initialize if incorporated into Ruby proper

      # Not much prettier, but a bit more readable than the previous iteration
      message = 'Missing switch: '
      message << short unless short.nil?
      message << ' or ' unless short.nil? || long.nil?
      message << long unless long.nil?
      @missing_switches << message
    end
  end

  return_values
end

#missing_switches?Boolean

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

Returns:

  • (Boolean)


13
14
15
# File 'lib/aggkit/option_parser.rb', line 13

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)



19
# File 'lib/aggkit/option_parser.rb', line 19

alias pickled_make_switch make_switch