Class: CommandLine::Command

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommand

Returns a new instance of Command.



45
46
47
48
49
# File 'lib/ejt_command_line.rb', line 45

def initialize
  @switches = []
  @mutually_exclusive_sets = [] # list of lists of syms
  @mandatory = []
end

Instance Attribute Details

#switchesObject (readonly)

Returns the value of attribute switches.



43
44
45
# File 'lib/ejt_command_line.rb', line 43

def switches
  @switches
end

Instance Method Details

#add_mandatory_switch(sym) ⇒ Object



59
60
61
# File 'lib/ejt_command_line.rb', line 59

def add_mandatory_switch(sym)
  @mandatory << sym
end

#add_mutually_exclusive_set(syms) ⇒ Object



55
56
57
# File 'lib/ejt_command_line.rb', line 55

def add_mutually_exclusive_set(syms)
  @mutually_exclusive_sets << syms.to_set
end

#add_switches(syms) ⇒ Object



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

def add_switches(syms)
  @switches += syms
end

#check_mandatory(syms) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# 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) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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