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? }



580
581
582
583
584
585
586
# File 'lib/command-set/arguments.rb', line 580

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



598
599
600
601
602
# File 'lib/command-set/arguments.rb', line 598

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



613
614
615
616
617
618
619
# File 'lib/command-set/arguments.rb', line 613

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



627
628
629
630
631
632
633
634
635
# File 'lib/command-set/arguments.rb', line 627

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



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

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

#match_terms(subject, terms, arguments) ⇒ Object



637
638
639
# File 'lib/command-set/arguments.rb', line 637

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

#name(name = nil) ⇒ Object



588
589
590
591
592
593
594
595
596
# File 'lib/command-set/arguments.rb', line 588

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

#omittable?Boolean

Returns:

  • (Boolean)


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

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

#parse(subject, term) ⇒ Object



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

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

#subject_requirementsObject



621
622
623
624
625
# File 'lib/command-set/arguments.rb', line 621

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

#validate(term, subject) ⇒ Object



604
605
606
607
608
609
610
611
# File 'lib/command-set/arguments.rb', line 604

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