Class: CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/cli.rb,
lib/cli/dsl.rb

Defined Under Namespace

Modules: DSL Classes: Arguments, Options, ParserError, ParsingError, Switches, Values

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ CLI

Returns a new instance of CLI.



115
116
117
118
119
120
121
122
123
124
# File 'lib/cli.rb', line 115

def initialize(&block)
  @arguments = Arguments.new
  @switches = Switches.new
  @options = Options.new

  instance_eval(&block) if block_given?

  switch :help, :short => :h, :description => 'display this help message'
  switch :version, :description => 'display version string' if @version
end

Class Method Details

.nameObject



303
304
305
# File 'lib/cli.rb', line 303

def self.name
  File.basename $0
end

Instance Method Details

#argument(name, options = {}) ⇒ Object



138
139
140
141
142
143
144
# File 'lib/cli.rb', line 138

def argument(name, options = {})
  argument_dsl = DSL::Argument.new(name, options)

  raise ParserError::ArgumentNameSpecifiedTwice.new(argument_dsl.name) if @arguments.has?(argument_dsl)

  @arguments << argument_dsl
end

#arguments(name, options = {}) ⇒ Object



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

def arguments(name, options = {})
  arguments_dsl = DSL::Arguments.new(name, options)

  raise ParserError::ArgumentNameSpecifiedTwice.new(arguments_dsl.name) if @arguments.has?(arguments_dsl)

  @arguments << arguments_dsl

  raise ParserError::MultipleArgumentsSpecifierError.new(@arguments.multiary) if @arguments.multiary.length > 1
end

#description(desc) ⇒ Object



126
127
128
# File 'lib/cli.rb', line 126

def description(desc)
  @description = desc
end

#option(name, options = {}) ⇒ Object



162
163
164
165
166
# File 'lib/cli.rb', line 162

def option(name, options = {})
  option_dsl = DSL::Option.new(name, options)
  check_switch_collision!(option_dsl)
  @options << option_dsl
end

#options(name, options = {}) ⇒ Object



168
169
170
171
172
# File 'lib/cli.rb', line 168

def options(name, options = {})
  option_dsl = DSL::Options.new(name, options)
  check_switch_collision!(option_dsl)
  @options << option_dsl
end

#parse(_argv = ARGV, stdin = STDIN, stderr = STDERR) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/cli.rb', line 174

def parse(_argv = ARGV, stdin = STDIN, stderr = STDERR)
  values = Values.new
  argv = _argv.dup

  # check help and version
  argv.each do |arg|
    break if arg == '--'

    if arg == '-h' or arg == '--help' 
      values.help = usage
      return values
    end

    if @version and arg == '--version' 
      values.version = "#{CLI.name} version \"#{@version}\"\n"
      return values
    end
  end

  # initialize values
  @options.each do |o|
    values.value(o, nil)
  end

  @switches.each do |o|
    values.value(o, nil)
  end

  # initialize multi options
  @options.multiary.each do |o|
    values.value(o, [])
  end

  # process switches
  mandatory_options = @options.mandatory.dup

  while not argv.first == '--' and Switches.is_switch?(argv.first)
    arg = argv.shift

    if switch = @switches.find(arg)
      values.set(switch)
    elsif option = @options.find(arg)
      value = argv.shift or raise ParsingError::MissingOptionValueError.new(option)
      if option.multiary?
        values.append(option, option.cast(value))
      else
        values.value(option, option.cast(value))
      end
      mandatory_options.delete(option)
    else
      raise ParsingError::UnknownSwitchError.new(arg) unless switch
    end
  end

  # set defaults
  @options.unarry.each do |o|
    next unless o.has_default?
    values.value(o, o.default_cast) if values.get(o) == nil
  end

  @options.multiary.each do |o|
    next unless o.has_default?
    values.value(o, o.default_cast) if values.get(o) == []
  end

  # check mandatory options
  raise ParsingError::MandatoryOptionsNotSpecifiedError.new(mandatory_options) unless mandatory_options.empty?

  argv.shift if argv.first == '--'

  # process arguments
  arguments = @arguments.dup
  while argument = arguments.shift
    value = if arguments.mandatory.length < argv.length
      arg = argv.shift

      if argument.multiary?
        v = [arg]
        while argv.length > arguments.length
          v << argv.shift
        end
        v
      else
        arg
      end
    else
      if argument.has_default?
        argument.default
      elsif not argument.mandatory?
        argument.multiary? ? [] : nil
      else
        raise ParsingError::MandatoryArgumentNotSpecifiedError.new(argument) if argv.empty?
      end
    end

    values.value(argument, argument.cast(value))
  end

  # process stdin
  values.stdin = @stdin.cast(stdin) if @stdin

  values
end

#parse!(argv = ARGV, stdin = STDIN, stderr = STDERR, stdout = STDOUT) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/cli.rb', line 278

def parse!(argv = ARGV, stdin = STDIN, stderr = STDERR, stdout = STDOUT)
  begin
    pp = parse(argv, stdin, stderr)
    if pp.help
      stdout.write pp.help
      exit 0
    end
    if pp.version
      stdout.write pp.version
      exit 0
    end

    if block_given?
      begin
        yield pp
      rescue RuntimeError => e
        raise ParsingError::UsageError, e.message
      end
    end
    pp
  rescue ParsingError => pe
    usage!(pe, stderr)
  end
end

#stdin(name = :data, options = {}) ⇒ Object



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

def stdin(name = :data, options = {})
  @stdin = DSL::Input.new(name, options)
end

#switch(name, options = {}) ⇒ Object



156
157
158
159
160
# File 'lib/cli.rb', line 156

def switch(name, options = {})
  switch_dsl = DSL::Switch.new(name, options)
  check_switch_collision!(switch_dsl)
  @switches << switch_dsl
end

#usage(msg = nil) ⇒ Object



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/cli.rb', line 307

def usage(msg = nil)
  out = StringIO.new
  out.puts msg if msg
  out.print "Usage: #{CLI.name}"
  out.print ' [switches|options]' if not @switches.empty? and not @options.optional.empty?
  out.print ' [switches]' if not @switches.empty? and @options.optional.empty?
  out.print ' [options]' if @switches.empty? and not @options.optional.empty?
  @options.mandatory.each do |o|
    out.print " #{o.switch} <value>"
  end
  out.print ' [--]' if not @arguments.empty? and (not @switches.empty? or not @options.empty?)
  out.print ' ' unless @arguments.empty?
  out.print(@arguments.map do |a|
    v = ''
    v += '[' unless a.mandatory?
    v += a.multiary? ? a.to_s + '*': a.to_s
    v += ']' unless a.mandatory?
    v
  end.join(' '))
  out.print " < #{@stdin}" if @stdin

  out.puts
  out.puts @description if @description

  if @stdin and @stdin.description?
    out.puts "Input:"
    out.puts "   #{@stdin} - #{@stdin.description}"
  end

  unless @switches.empty?
    out.puts "Switches:"
    @switches.each do |s|
      out.print '   '
      out.print s.switch
      out.print " (#{s.switch_short})" if s.has_short?
      out.print " - #{s.description}" if s.description?
      out.puts
    end
  end

  unless @options.empty?
    out.puts "Options:"
    @options.each do |o|
      unless o.multiary?
        out.print "   #{o.switch}"
      else
        out.print "   #{o.switch}*"
      end
      out.print " (#{o.switch_short})" if o.has_short?
      out.print ' (mandatory)' if o.mandatory?
      if o.has_default_label?
        out.print " [#{o.default_label}]"
      elsif o.has_default?
        out.print " [%s]" % o.default
      end
      out.print " - #{o.description}" if o.description?
      out.puts
    end
  end

  unless @arguments.empty?
    out.puts "Arguments:"
    @arguments.each do |a|
      unless a.multiary?
        out.print "   #{a}"
      else
        out.print "   #{a}*"
      end
      out.print ' (optional)' if not a.mandatory? and not a.has_default?
      if a.has_default_label?
        out.print " [#{a.default_label}]"
      elsif a.has_default?
        out.print " [%s]" % (a.default.is_a?(Array) ? a.default.join(' ') : a.default)
      end
      out.print " - #{a.description}" if a.description?
      out.puts
    end
  end

  out.rewind
  out.read
end

#usage!(msg = nil, io = STDERR) ⇒ Object



390
391
392
393
394
# File 'lib/cli.rb', line 390

def usage!(msg = nil, io = STDERR)
  msg = "Error: #{msg}" if msg
  io.write usage(msg)
  exit 42
end

#version(version) ⇒ Object



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

def version(version)
  @version = version.to_s
end