Module: OptionParser::Completion
- Included in:
- CompletingHash, OptionMap
- Defined in:
- lib/llm/shell/internal/optparse/lib/optparse.rb
Overview
Keyword completion module. This allows partial arguments to be specified and resolved against a list of acceptable values.
Class Method Summary collapse
- .candidate(key, icase = false, pat = nil, &block) ⇒ Object
- .completable?(key) ⇒ Boolean
-
.regexp(key, icase) ⇒ Object
:nodoc:.
Instance Method Summary collapse
- #candidate(key, icase = false, pat = nil, &_) ⇒ Object
- #complete(key, icase = false, pat = nil) ⇒ Object
- #convert(opt = nil, val = nil) ⇒ Object
Class Method Details
.candidate(key, icase = false, pat = nil, &block) ⇒ Object
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 |
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 448 def self.candidate(key, icase = false, pat = nil, &block) pat ||= Completion.regexp(key, icase) candidates = [] block.call do |k, *v| (if Regexp === k kn = "" k === key else kn = defined?(k.id2name) ? k.id2name : k pat === kn end) or next v << k if v.empty? candidates << [k, v, kn] end candidates end |
.completable?(key) ⇒ Boolean
465 466 467 |
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 465 def self.completable?(key) String.try_convert(key) or defined?(key.id2name) end |
.regexp(key, icase) ⇒ Object
:nodoc:
444 445 446 |
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 444 def self.regexp(key, icase) Regexp.new('\A' + Regexp.quote(key).gsub(/\w+\b/, '\&\w*'), icase) end |
Instance Method Details
#candidate(key, icase = false, pat = nil, &_) ⇒ Object
469 470 471 |
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 469 def candidate(key, icase = false, pat = nil, &_) Completion.candidate(key, icase, pat, &method(:each)) end |
#complete(key, icase = false, pat = nil) ⇒ Object
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 |
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 474 def complete(key, icase = false, pat = nil) candidates = candidate(key, icase, pat, &method(:each)).sort_by {|k, v, kn| kn.size} if candidates.size == 1 canon, sw, * = candidates[0] elsif candidates.size > 1 canon, sw, cn = candidates.shift candidates.each do |k, v, kn| next if sw == v if String === cn and String === kn if cn.rindex(kn, 0) canon, sw, cn = k, v, kn next elsif kn.rindex(cn, 0) next end end throw :ambiguous, key end end if canon block_given? or return key, *sw yield(key, *sw) end end |
#convert(opt = nil, val = nil) ⇒ Object
499 500 501 |
# File 'lib/llm/shell/internal/optparse/lib/optparse.rb', line 499 def convert(opt = nil, val = nil, *) val end |