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.



108
109
110
111
112
113
114
115
116
# File 'lib/ejt_command_line.rb', line 108

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



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/ejt_command_line.rb', line 146

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



118
119
120
# File 'lib/ejt_command_line.rb', line 118

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

#global(&block) ⇒ Object



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

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

#mandatory(sym) ⇒ Object



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

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

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



138
139
140
# File 'lib/ejt_command_line.rb', line 138

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

#one_of(*syms) ⇒ Object



163
164
165
166
167
# File 'lib/ejt_command_line.rb', line 163

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



176
177
178
179
# File 'lib/ejt_command_line.rb', line 176

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

#simple_switch(sym, *flags) ⇒ Object



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

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

#switches(*syms) ⇒ Object



158
159
160
161
# File 'lib/ejt_command_line.rb', line 158

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

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



134
135
136
# File 'lib/ejt_command_line.rb', line 134

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

#value_type(sym, &parser) ⇒ Object



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

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

  @value_types[sym] = parser
end