summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-11-19 11:26:54 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-11-19 11:26:54 +0000
commit644f0445e86034dde399d6db8261c82cf34b8e07 ()
treee5f07c9ca2f876d615549cbd3ea46f6ea370e421
parent3c1f696a507fb8f7deb87b7111d44458efb8c051 (diff)
* lib/optparse.rb: shell completion support for bash.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29832 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-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