Class: Command::AlternatingArgument

Inherits:
ArgumentDecorator show all
Includes:
DSL::Argument
Defined in:
lib/command-set/arguments.rb

Overview

Allows several arguments to share a position. Pass a block to the “decorator” method with the argument declarations inside. The first argument that can parse the input will be assigned - others will get nil.

Instance Attribute Summary

Attributes inherited from Argument

#value

Instance Method Summary collapse

Methods included from DSL::Argument

#alternating_argument, #argument, argument_typemap, #create, #create_decorator, document, #named_optionals, register_argument, register_decorator, #special_argument

Methods inherited from ArgumentDecorator

decor, #decor, #decorate, decoration, register

Methods inherited from Argument

#check_present, register, #required?

Constructor Details

#initialize(embed_in, &block) ⇒ AlternatingArgument

initialize(embed_in){ yield if block_given? }



503
504
505
506
507
508
509
# File 'lib/command-set/arguments.rb', line 503

def initialize(embed_in, &block)
  super(embed_in)
  @names = []
  @sub_arguments = []
  self.instance_eval &block
  @wrapping_decorator.embed_argument(self)
end

Instance Method Details

#complete(prefix, subject) ⇒ Object



521
522
523
524
525
# File 'lib/command-set/arguments.rb', line 521

def complete(prefix, subject)
  return sub_arguments.inject([]) do |list, sub_arg|
    list.concat sub_arg.complete(prefix, subject)
  end
end

#consume(subject, arguments) ⇒ Object



536
537
538
539
540
541
542
# File 'lib/command-set/arguments.rb', line 536

def consume(subject, arguments)
  term = arguments.first
  catcher = first_catch(term, subject)
  result = catcher.consume(subject, arguments)
  @value = catcher.value
  return result.merge({@name => result[catcher.name]})
end

#consume_hash(subject, hash) ⇒ Object

If a hash is used for arguments that includes more than one of alternating argument’s sub-arguments, the behavior is undefined



545
546
547
548
549
550
551
552
553
554
555
556
# File 'lib/command-set/arguments.rb', line 545

def consume_hash(subject, hash)
  begin
    return super(subject, hash)
  rescue OutOfArgumentsException; end
  @sub_arguments.each do |arg|
    unless hash[arg.name].nil?
      result = arg.consume_hash(subject, hash)
      return result.merge({@name => result[arg.name]})
    end
  end
  raise OutOfArgumentsException, "Missing argument: #@name!"
end

#embed_argument(arg) ⇒ Object



573
574
575
576
# File 'lib/command-set/arguments.rb', line 573

def embed_argument(arg)
  @names << arg.name
  @sub_arguments << optional.decorate(arg)
end

#match_terms(subject, terms, arguments) ⇒ Object



558
559
560
# File 'lib/command-set/arguments.rb', line 558

def match_terms(subject, terms, arguments)
  first_catch(terms.first, subject).match_terms(subject, terms, arguments)
end

#name(name = nil) ⇒ Object



511
512
513
514
515
516
517
518
519
# File 'lib/command-set/arguments.rb', line 511

def name(name = nil)
  if name.nil?
    return @names
  else
    name = name.to_s
    @names << name
    @name = name
  end
end

#omittable?Boolean

Returns:

  • (Boolean)


562
563
564
565
566
# File 'lib/command-set/arguments.rb', line 562

def omittable?
  return sub_arguments.inject(false) do |can_omit, sub_arg|
    can_omit || sub_arg.omittable?
  end
end

#parse(subject, term) ⇒ Object



568
569
570
571
# File 'lib/command-set/arguments.rb', line 568

def parse(subject, term)
  catcher = first_catch(term, subject)
  return catcher.parse(subject, term)
end

#validate(term, subject) ⇒ Object



527
528
529
530
531
532
533
534
# File 'lib/command-set/arguments.rb', line 527

def validate(term, subject)
  begin
    first_catch(term, subject)
    return true
  rescue CommandError
    return false
  end
end