summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/optparse.rb70
-rw-r--r--misc/rb_optparse.bash20
-rw-r--r--test/optparse/test_bash_completion.rb42
-rw-r--r--test/optparse/test_getopts.rb4
4 files changed, 127 insertions, 9 deletions
@@ -195,6 +195,11 @@
# options = OptparseExample.parse(ARGV)
# pp options
#
# === Further documentation
#
# The above examples should be enough to learn how to use this class. If you
@@ -218,12 +223,15 @@ class OptionParser
# and resolved against a list of acceptable values.
#
module Completion
- def complete(key, icase = false, pat = nil)
- pat ||= Regexp.new('\A' + Regexp.quote(key).gsub(/\w+\b/, '\&\w*'),
- icase)
canon, sw, cn = nil
candidates = []
- each do |k, *v|
(if Regexp === k
kn = nil
k === key
@@ -234,7 +242,16 @@ class OptionParser
v << k if v.empty?
candidates << [k, v, kn]
end
- candidates = candidates.sort_by {|k, v, kn| kn.size}
if candidates.size == 1
canon, sw, * = candidates[0]
elsif candidates.size > 1
@@ -717,9 +734,17 @@ class OptionParser
# --help
# Shows option summary.
#
Officious['help'] = proc do |parser|
- Switch::NoArgument.new do
- puts parser.help
exit
end
end
@@ -1461,6 +1486,35 @@ class OptionParser
end
private :complete
#
# Loads options from file names as +filename+. Does nothing when the file
# is not present. Returns whether successfully loaded.
@@ -1818,6 +1872,6 @@ ARGV.extend(OptionParser::Arguable)
if $0 == __FILE__
Version = OptionParser::Version
ARGV.options {|q|
- q.parse!.empty? or puts "what's #{ARGV.join(' ')}?"
} or abort(ARGV.options.to_s)
end
@@ -0,0 +1,20 @@
@@ -0,0 +1,42 @@
@@ -1,7 +1,9 @@
require 'test/unit'
require 'optparse'
-class TestOptionParserGetopts < Test::Unit::TestCase
def setup
@opt = OptionParser.new
end