class CommandLine::Command

Attributes

switches[R]

Public Class Methods

new() click to toggle source
# File lib/ejt_command_line.rb, line 45
def initialize
  @switches = []
  @mutually_exclusive_sets = [] # list of lists of syms
  @mandatory = []
end

Public Instance Methods

add_mandatory_switch(sym) click to toggle source
# File lib/ejt_command_line.rb, line 59
def add_mandatory_switch(sym)
  @mandatory << sym
end
add_mutually_exclusive_set(syms) click to toggle source
# File lib/ejt_command_line.rb, line 55
def add_mutually_exclusive_set(syms)
  @mutually_exclusive_sets << syms.to_set
end
add_switches(syms) click to toggle source
# File lib/ejt_command_line.rb, line 51
def add_switches(syms)
  @switches += syms
end
check_mandatory(syms) click to toggle source
# File lib/ejt_command_line.rb, line 85
def check_mandatory(syms)
  missing = []

  @mandatory.each do |m|
    unless syms.member?(m)
      missing << m
    end
  end

  if missing.size > 0
    msg = "missing mandatory switches:\n"
    missing.each do |m|
      msg += "  #{m}\n"
    end

    raise ParseError, msg
  end
end
check_mutual_exclusion(syms) click to toggle source
# File lib/ejt_command_line.rb, line 63
def check_mutual_exclusion(syms)
  nr_sets = @mutually_exclusive_sets.size
  set_counts = Array.new(nr_sets, [])

  syms.each do |s|
    # is it in an exclusive set?
    0.upto(nr_sets - 1) do |n|
      if @mutually_exclusive_sets[n].member?(s)
        set_counts[n] << s
      end
    end
  end

  0.upto(nr_sets - 1) do |n|
    if set_counts[n].size > 1
      msg = "mutually exclusive options used:\n"
      set_counts[n].each {|sym| msg += "    #{sym}\n"}
      raise ParseError, msg
    end
  end
end