Module: OptionParser::Completion

Included in:
CompletingHash, OptionMap
Defined in:
lib/MrMurano/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

Instance Method Summary collapse

Class Method Details

.candidate(key, icase = false, pat = nil, typ = nil, &block) ⇒ Object

lb

added: typ param



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/MrMurano/optparse.rb', line 421

def self.candidate(key, icase = false, pat = nil, typ = nil, &block)
  pat ||= Completion.regexp(key, icase)
  candidates = []
  block.call do |k, *v|
    (if Regexp === k
       kn = "".freeze
       k === key
     else
       kn = defined?(k.id2name) ? k.id2name : k
       pat === kn
     end) or next

    # [lb]: Do not compare single character to first character of long option.
    # E.g., if you define one option, ('-T', '--excellent', 'Awesome option'),
    # and another option just ('-e', 'Another option'), the command `mycmd -e`
    # will match against --excellent because long options are considered first.
    # (2017-08-16: I saw this with github.com/commander-rb, where I used
    # global_option to define a long option without a short abbreviation;
    # and I used cmd.option to define an option with both a short and a long,
    # where the short's letter is the first letter of the global_option long.)
    # To see this in action, set a breakpoint here and run something like
    #   murano status -e
    key.length > 1 || typ != :long || next

    v << k if v.empty?
    candidates << [k, v, kn]
  end
  candidates
end

.regexp(key, icase) ⇒ Object



416
417
418
# File 'lib/MrMurano/optparse.rb', line 416

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, typ = nil) ⇒ Object

lb

added: typ param



452
453
454
455
# File 'lib/MrMurano/optparse.rb', line 452

def candidate(key, icase = false, pat = nil, typ = nil)
  # [lb] added: typ param
  Completion.candidate(key, icase, pat, typ, &method(:each))
end

#complete(key, icase = false, pat = nil, typ = nil) ⇒ Object

lb

added: typ param



459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'lib/MrMurano/optparse.rb', line 459

def complete(key, icase = false, pat = nil, typ = nil)
  # [lb] added: typ param
  candidates = candidate(key, icase, pat, typ, &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



485
486
487
# File 'lib/MrMurano/optparse.rb', line 485

def convert(opt = nil, val = nil, *)
  val
end