Class: CLASP::Arguments

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

Overview

The main class for processing command-line arguments

Defined Under Namespace

Classes: Flag, ImmutableArray, Option

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv = ARGV, aliases = nil, options = {}) ⇒ Arguments

Constructs an instance of the class, according to the given parameters

See the documentation for the ::CLASP module for examples

Signature

  • Parameters:

    • argv

      (Array) The arguments array. May not be nil. Defaults to ARGV.

    • aliases

      (Array) The aliases array. Defaults to nil. If none supplied, no aliasing will be performed.

    • options

      An options hash, containing any of the following options.

  • Options:

    • mutate_arg:

      (Boolean) Determines if the library should mutate argv. Defaults to true. This is essential when using CLASP in conjunction with $<.



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
# File 'lib/clasp/arguments.rb', line 219

def initialize(argv = ARGV, aliases = nil, options = {})

  # have to do this name-swap, as 'options' has CLASP-specific
  # meaning
  init_opts, options = options.dup, nil

  init_opts[:mutate_argv] = true unless init_opts.has_key? :mutate_argv


  @argv        = argv
  @argv_original_copy  =  ImmutableArray.new(argv.dup)

  @aliases = aliases

  aliases    = nil if aliases and aliases.empty?

  flags, options, values = Arguments.parse(argv, aliases)

  @flags   =  ImmutableArray.new(flags)
  @options = ImmutableArray.new(options)
  @values    = ImmutableArray.new(values)


  # do argv-mutation, if required
  if init_opts[:mutate_argv]

    while not argv.empty?

      argv.shift
    end

    @values.each do |v|

      argv << v
    end
  end
end

Instance Attribute Details

#aliasesObject (readonly)

an immutable array of aliases



394
395
396
# File 'lib/clasp/arguments.rb', line 394

def aliases
  @aliases
end

#argvObject (readonly)

the (possibly mutated) array of arguments instance passed to new



406
407
408
# File 'lib/clasp/arguments.rb', line 406

def argv
  @argv
end

#argv_original_copyObject (readonly)

unchanged copy of the original array of arguments passed to new



409
410
411
# File 'lib/clasp/arguments.rb', line 409

def argv_original_copy
  @argv_original_copy
end

#flagsObject (readonly)

an immutable array of flags



397
398
399
# File 'lib/clasp/arguments.rb', line 397

def flags
  @flags
end

#optionsObject (readonly)

an immutable array of options



400
401
402
# File 'lib/clasp/arguments.rb', line 400

def options
  @options
end

#valuesObject (readonly)

an immutable array of values



403
404
405
# File 'lib/clasp/arguments.rb', line 403

def values
  @values
end

Class Method Details

.parse(argv, aliases) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
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
# File 'lib/clasp/arguments.rb', line 258

def self.parse(argv, aliases)

  flags  =  []
  options  =  []
  values = []

  forced_value   =  false
  want_option_value  =  false

  argv.each_with_index do |arg, index|

    if not forced_value

      if '--' == arg

        # all subsequent arguments are values
        forced_value = true
        next
      end

      # do regex test to see if option/flag/value
      if arg =~ /^(-+)([^=]+)/

        hyphens     = $1
        given_label   =  $2
        given_name    = "#$1#$2"
        value     = ($' and not $'.empty?) ? $'[1 ... $'.size] : nil
        argument_alias  =  nil
        resolved_name = nil

        (aliases || []).each do |a|

          if a.name == given_name or a.aliases.include? given_name

            argument_alias  =  a
            resolved_name = a.name

            # need to check whether the alias is a default-option
            # and, if so, expand out its name and value, and replace
            # the name and (if none previously specified) the value
            if resolved_name =~ /^(-+)([^=]+)=/

              resolved_name  =  "#$1#$2"
              value      ||=  $'
            end
            break
          end
        end

        # Here we intercept and (potentially) cater to grouped flags
        if not argument_alias and not value and aliases and 1 == hyphens.size

          # Must match all
          flag_aliases = []
          given_label[0 ... given_label.size].each_char do |c|

            new_flag  =  "-#{c.chr}"

            flag_alias  =  nil

            # special case where the flag's actual name is short form and found here
            flag_alias  ||=  aliases.detect { |a| a.is_a?(CLASP::Flag) && a.name == new_flag }

            # if not found as a flag, look in each aliases' aliases
            flag_alias  ||=  aliases.detect { |a| a.aliases.include? new_flag }

            if not flag_alias

              flag_aliases = nil
              break
            else

              flag_aliases <<  flag_alias
            end
          end

          if flag_aliases

            # got them all, so now have to process them all
            # as normal. Note: is this susceptible to
            # infinite recursion

            # convert to argv and invoke
            flags_argv = flag_aliases.map { |a| a.name }

            grp_flags, grp_options, grp_value = Arguments.parse flags_argv, aliases

            grp_flags.map! { |f| Flag.new(arg, index, given_name, f.name, f.argument_alias, hyphens.size, given_label, argument_alias ? argument_alias.extras : nil) }
            grp_options.map! { |o| Option.new(arg, index, given_name, o.name, o.argument_alias, hyphens.size, given_label, o.value, argument_alias ? argument_alias.extras : nil) }

            flags.push(*grp_flags)
            options.push(*grp_options)
            values.push(*grp_value)

            next
          end
        end

        if argument_alias and argument_alias.is_a? CLASP::Option and not value

          want_option_value = true
          options << Option.new(arg, index, given_name, resolved_name, argument_alias, hyphens.size, given_label, nil, argument_alias ? argument_alias.extras : nil)
        elsif value

          want_option_value = false
          options << Option.new(arg, index, given_name, resolved_name, argument_alias, hyphens.size, given_label, value, argument_alias ? argument_alias.extras : nil)
        else

          want_option_value = false
          flags << Flag.new(arg, index, given_name, resolved_name, argument_alias, hyphens.size, given_label, argument_alias ? argument_alias.extras : nil)
        end

        next
      end
    end

    if want_option_value and not forced_value

      option = options[-1]
      option.instance_eval("@value='#{arg}'")
      want_option_value = false
    else

      values << arg
    end
  end

  return flags, options, values

end

Instance Method Details

#find_first_unknown(options = {}) ⇒ Object

finds the first unknown flag or option; nil if all used

Raises:

  • (ArgumentError)


413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/clasp/arguments.rb', line 413

def find_first_unknown options = {}

  option = {} if options.nil?

  raise ArgumentError, "options must be nil or Hash - #{option.class} given" unless options.is_a? ::Hash

  aliases  =  options[:aliases] || @aliases

  raise ArgumentError, "aliases may not be nil" if aliases.nil?

  flags.each do |f|

    return f unless aliases.any? { |al| al.is_a?(::CLASP::Flag) && al.name == f.name }
  end

  options.each do |o|

    return o unless aliases.any? { |al| al.is_a?(::CLASP::Option) && al.name == o.name }
  end

  nil
end