Class: Command::AlternatingArgument

Inherits:
Argument
  • Object
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 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, #subject

Methods inherited from Argument

#basis, #check_present, #names, register, #required?

Constructor Details

#initialize(embed_in, &block) ⇒ AlternatingArgument

initialize(embed_in){ yield if block_given? }



583
584
585
586
587
588
589
# File 'lib/command-set/arguments.rb', line 583

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

Instance Method Details

#complete(terms, prefix, subject) ⇒ Object



601
602
603
604
605
# File 'lib/command-set/arguments.rb', line 601

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

#consume(subject, arguments) ⇒ Object



616
617
618
619
620
621
622
# File 'lib/command-set/arguments.rb', line 616

def consume(subject, arguments)
  term = arguments.first
  catcher = first_catch(term, subject)
  result = catcher.consume(subject, arguments)
  return result if @name.nil?
  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



631
632
633
634
635
636
637
638
639
# File 'lib/command-set/arguments.rb', line 631

def consume_hash(subject, hash)
  result = @sub_arguments.inject({}) do |result, arg|
    result.merge arg.consume_hash(subject, hash)
  end
  unless @name.nil?
    result[@name] = parse(subject, hash[@name])
  end
  return result
end

#embed_argument(arg) ⇒ Object



656
657
658
659
# File 'lib/command-set/arguments.rb', line 656

def embed_argument(arg)
  @names << arg.name
  @sub_arguments << Optional.new(arg)
end

#match_terms(subject, terms, arguments) ⇒ Object



641
642
643
# File 'lib/command-set/arguments.rb', line 641

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

#name(name = nil) ⇒ Object



591
592
593
594
595
596
597
598
599
# File 'lib/command-set/arguments.rb', line 591

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

#omittable?Boolean

Returns:

  • (Boolean)


645
646
647
648
649
# File 'lib/command-set/arguments.rb', line 645

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

#parse(subject, term) ⇒ Object



651
652
653
654
# File 'lib/command-set/arguments.rb', line 651

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

#subject_requirementsObject



624
625
626
627
628
# File 'lib/command-set/arguments.rb', line 624

def subject_requirements
  @sub_arguments.inject([]) do |list, arg|
    list + arg.subject_requirements
  end
end

#validate(term, subject) ⇒ Object



607
608
609
610
611
612
613
614
# File 'lib/command-set/arguments.rb', line 607

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