Class: Command::AlternatingArgument
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
#alternating_argument, #argument, argument_typemap, #create, #create_decorator, document, #named_optionals, register_argument, register_decorator, #special_argument
decor, #decor, #decorate, decoration, register
Methods inherited from Argument
#check_present, register, #required?
Constructor Details
initialize(embed_in){ yield if block_given? }
554
555
556
557
558
559
560
|
# File 'lib/command-set/arguments.rb', line 554
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
572
573
574
575
576
|
# File 'lib/command-set/arguments.rb', line 572
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
587
588
589
590
591
592
593
|
# File 'lib/command-set/arguments.rb', line 587
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
596
597
598
599
600
601
602
603
604
605
606
607
|
# File 'lib/command-set/arguments.rb', line 596
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
624
625
626
627
|
# File 'lib/command-set/arguments.rb', line 624
def embed_argument(arg)
@names << arg.name
@sub_arguments << optional.decorate(arg)
end
|
#match_terms(subject, terms, arguments) ⇒ Object
609
610
611
|
# File 'lib/command-set/arguments.rb', line 609
def match_terms(subject, terms, arguments)
first_catch(terms.first, subject).match_terms(subject, terms, arguments)
end
|
#name(name = nil) ⇒ Object
562
563
564
565
566
567
568
569
570
|
# File 'lib/command-set/arguments.rb', line 562
def name(name = nil)
if name.nil?
return @names
else
name = name.to_s
@names << name
@name = name
end
end
|
#omittable? ⇒ Boolean
613
614
615
616
617
|
# File 'lib/command-set/arguments.rb', line 613
def omittable?
return sub_arguments.inject(false) do |can_omit, sub_arg|
can_omit || sub_arg.omittable?
end
end
|
#parse(subject, term) ⇒ Object
619
620
621
622
|
# File 'lib/command-set/arguments.rb', line 619
def parse(subject, term)
catcher = first_catch(term, subject)
return catcher.parse(subject, term)
end
|
#validate(term, subject) ⇒ Object
578
579
580
581
582
583
584
585
|
# File 'lib/command-set/arguments.rb', line 578
def validate(term, subject)
begin
first_catch(term, subject)
return true
rescue CommandError
return false
end
end
|