Class: CommandLine::Parser

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

Constant Summary collapse

GLOBAL_SYM =
:global__

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Parser

Returns a new instance of Parser.



120
121
122
123
124
125
126
127
128
# File 'lib/ejt_command_line.rb', line 120

def initialize(&block)
  @switches = {}
  @global_switches = []
  @value_types = {}
  @commands = Hash.new {|hash, key| Command.new}
  @current_command = @commands[GLOBAL_SYM]

  configure(&block) if block
end

Instance Method Details

#command(sym, &block) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/ejt_command_line.rb', line 154

def command(sym, &block)
  old = @current_command
  @current_command = @commands[sym] = Command.new

  if block
    release = lambda {@current_command = old}
    bracket_(release) do
      self.instance_eval(&block)
    end
  end
end

#configure(&block) ⇒ Object



130
131
132
# File 'lib/ejt_command_line.rb', line 130

def configure(&block)
  self.instance_eval(&block)
end

#global(&block) ⇒ Object



150
151
152
# File 'lib/ejt_command_line.rb', line 150

def global(&block)
  command(GLOBAL_SYM, &block)
end

#mandatory(sym) ⇒ Object



177
178
179
180
181
182
# File 'lib/ejt_command_line.rb', line 177

def mandatory(sym)
  syms = [sym]
  check_switches_are_defined(syms)
  @current_command.add_switches(syms)
  @current_command.add_mandatory_switch(sym)
end

#one_of(*syms) ⇒ Object



171
172
173
174
175
# File 'lib/ejt_command_line.rb', line 171

def one_of(*syms)
  check_switches_are_defined(syms)
  @current_command.add_switches(syms)
  @current_command.add_mutually_exclusive_set(syms)
end

#parse(handler, *args) ⇒ Object



184
185
186
187
# File 'lib/ejt_command_line.rb', line 184

def parse(handler, *args)
  command, opts, plain_args = parse_(args)
  handler.send(command, opts, plain_args)
end

#simple_switch(sym, *flags) ⇒ Object



142
143
144
# File 'lib/ejt_command_line.rb', line 142

def simple_switch(sym, *flags)
  @switches[sym] = Switch.new(flags)
end

#switches(*syms) ⇒ Object



166
167
168
169
# File 'lib/ejt_command_line.rb', line 166

def switches(*syms)
  check_switches_are_defined(syms)
  @current_command.add_switches(syms)
end

#value_switch(sym, value_sym, *flags) ⇒ Object



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

def value_switch(sym, value_sym, *flags)
  @switches[sym] = Switch.new(flags, get_value_parser(value_sym))
end

#value_type(sym, &parser) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/ejt_command_line.rb', line 134

def value_type(sym, &parser)
  if @value_types.member?(sym)
    raise ConfigureError, "duplicate value type '#{sym}'"
  end

  @value_types[sym] = parser
end