Method: CLASP::Arguments#initialize

Defined in:
lib/clasp/arguments.rb

#initialize(argv = ARGV, specifications = 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

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

    • options An options hash, containing any of the following options

  • Options:

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



402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# File 'lib/clasp/arguments.rb', line 402

def initialize(argv = ARGV, specifications = 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

    @program_name       = init_opts[:program_name] || Arguments.derive_program_name_

    @argv               = argv
    argv                = argv.dup
    @argv_original_copy = argv.dup.freeze

    @specifications     = specifications
    @aliases            = @specifications

    specifications      = nil if specifications and specifications.empty?

    flags, options, values, double_slash_index = Arguments.parse_(argv, specifications)

    [ flags, options, values ].each do |ar|

        class << ar

            undef :inspect
            undef :to_s

            def to_s

                s =   ''

                s +=  '['
                s +=  self.map { |v| %Q<"#{v}"> }.join(', ')
                s +=  ']'

                s
            end

            def inspect

                s =   ''

                s +=  "#<#{self.class}:0x#{(object_id << 1).to_s(16)} ["
                s +=  self.map { |v| v.inspect }.join(', ')
                s +=  "]>"

                s
            end
        end
    end

    @flags    = flags.freeze
    @options  = options.freeze
    @values   = values.freeze

    @double_slash_index = double_slash_index

    # 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