Class: Command::Named

Inherits:
ArgumentDecoration show all
Defined in:
lib/command-set/arguments.rb

Overview

Indicated that the name of the argument has to appear on the command line before it will be recognized. Useful for optional or alternating arguments

Instance Attribute Summary

Attributes inherited from Argument

#name

Instance Method Summary collapse

Methods inherited from ArgumentDecoration

#decorated, #pretty_print_instance_variables, register

Methods inherited from Argument

#basis, #check_present, #consume_hash, #names, #omittable?, #parse, register, #required?, #subject_requirements

Constructor Details

#initialize(*args) ⇒ Named

Returns a new instance of Named.



434
435
436
437
# File 'lib/command-set/arguments.rb', line 434

def initialize(*args)
  super
  @name_pos = nil
end

Instance Method Details

#complete(terms, prefix, subject) ⇒ Object



458
459
460
461
462
463
464
465
466
467
468
# File 'lib/command-set/arguments.rb', line 458

def complete(terms, prefix, subject)
  if not @name_pos.nil? and terms[-@name_pos] == name.to_s
    return decorated.complete(terms, prefix, subject)
  else
    if %r{^#{prefix.to_s}.*} =~ name.to_s
      return [name.to_s]
    else
      return []
    end
  end
end

#consume(subject, arguments) ⇒ Object



439
440
441
442
443
444
445
446
# File 'lib/command-set/arguments.rb', line 439

def consume(subject, arguments)
  if arguments.first == name
    arguments.shift
    return decorated.consume(subject,arguments)
  else
    raise ArgumentInvalidException, "Name \"#{name}\" required."
  end
end

#match_terms(subject, terms, arguments) ⇒ Object



448
449
450
451
452
453
454
455
456
# File 'lib/command-set/arguments.rb', line 448

def match_terms(subject, terms, arguments)
  if terms.first == name.to_s
    @name_pos = terms.length
    terms.shift
    decorated.match_terms(subject, terms, arguments)
  else
    arguments.shift
  end
end

#validate(term, subject) ⇒ Object



470
471
472
# File 'lib/command-set/arguments.rb', line 470

def validate(term, subject)
  return term == name || super
end