[前][次][番号順一覧][スレッド一覧]

ruby-changes:23164

From: nobu <ko1@a...>
Date: Mon, 2 Apr 2012 13:26:09 +0900 (JST)
Subject: [ruby-changes:23164] nobu:r35214 (trunk): * lib/optparse/ac.rb: autoconf-like options.

nobu	2012-04-02 13:25:14 +0900 (Mon, 02 Apr 2012)

  New Revision: 35214

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=35214

  Log:
    * lib/optparse/ac.rb: autoconf-like options.

  Added files:
    trunk/lib/optparse/ac.rb
    trunk/test/optparse/test_autoconf.rb
  Modified files:
    trunk/ChangeLog

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 35213)
+++ ChangeLog	(revision 35214)
@@ -1,3 +1,7 @@
+Mon Apr  2 13:25:08 2012  Nobuyoshi Nakada  <nobu@r...>
+
+	* lib/optparse/ac.rb: autoconf-like options.
+
 Mon Apr  2 10:34:00 2012  eregon  <eregontp@g...>
 
 	* string.c (rb_str_start_with, rb_str_end_with): raise an error if
Index: lib/optparse/ac.rb
===================================================================
--- lib/optparse/ac.rb	(revision 0)
+++ lib/optparse/ac.rb	(revision 35214)
@@ -0,0 +1,50 @@
+require 'optparse'
+
+class OptionParser::AC < OptionParser
+  private
+
+  def _check_ac_args(name, block)
+    unless /\A\w[-\w]*\z/ =~ name
+      raise ArgumentError, name
+    end
+    unless block
+      raise ArgumentError, "no block given", ParseError.filter_backtrace(caller)
+    end
+  end
+
+  def _ac_arg_enable(prefix, name, help_string, block)
+    _check_ac_args(name, block)
+
+    sdesc = []
+    ldesc = ["--#{prefix}-#{name}"]
+    desc = [help_string]
+    q = name.downcase
+    enable = Switch::NoArgument.new(nil, proc {true}, sdesc, ldesc, nil, desc, block)
+    disable = Switch::NoArgument.new(nil, proc {false}, sdesc, ldesc, nil, desc, block)
+    top.append(enable, [], ["enable-" + q], disable, ['disable-' + q])
+    enable
+  end
+
+  public
+
+  def ac_arg_enable(name, help_string, &block)
+    _ac_arg_enable("enable", name, help_string, block)
+  end
+
+  def ac_arg_disable(name, help_string, &block)
+    _ac_arg_enable("disable", name, help_string, block)
+  end
+
+  def ac_arg_with(name, help_string, &block)
+    _check_ac_args(name, block)
+
+    sdesc = []
+    ldesc = ["--with-#{name}"]
+    desc = [help_string]
+    q = name.downcase
+    with = Switch::PlacedArgument.new(*search(:atype, String), sdesc, ldesc, nil, desc, block)
+    without = Switch::NoArgument.new(nil, proc {}, sdesc, ldesc, nil, desc, block)
+    top.append(with, [], ["with-" + q], without, ['without-' + q])
+    with
+  end
+end

Property changes on: lib/optparse/ac.rb
___________________________________________________________________
Added: svn:eol-style
   + LF

Index: test/optparse/test_autoconf.rb
===================================================================
--- test/optparse/test_autoconf.rb	(revision 0)
+++ test/optparse/test_autoconf.rb	(revision 35214)
@@ -0,0 +1,63 @@
+require 'test/unit'
+require 'optparse/ac'
+
+class TestOptionParser < Test::Unit::TestCase; end
+
+class TestOptionParser::AutoConf < Test::Unit::TestCase
+  def setup
+    @opt = OptionParser::AC.new
+    @foo = @bar = self.class
+    @opt.ac_arg_enable("foo", "foo option") {|x| @foo = x}
+    @opt.ac_arg_disable("bar", "bar option") {|x| @bar = x}
+    @opt.ac_arg_with("zot", "zot option") {|x| @zot = x}
+  end
+
+  class DummyOutput < String
+    alias write <<
+  end
+  def no_error(*args)
+    $stderr, stderr = DummyOutput.new, $stderr
+    assert_nothing_raised(*args) {return yield}
+  ensure
+    stderr, $stderr = $stderr, stderr
+    $!.backtrace.delete_if {|e| /\A#{Regexp.quote(__FILE__)}:#{__LINE__-2}/o =~ e} if $!
+    assert_empty(stderr)
+  end
+
+  def test_enable
+    @opt.parse!(%w"--enable-foo")
+    assert_equal(true, @foo)
+    @opt.parse!(%w"--enable-bar")
+    assert_equal(true, @bar)
+  end
+
+  def test_disable
+    @opt.parse!(%w"--disable-foo")
+    assert_equal(false, @foo)
+    @opt.parse!(%w"--disable-bar")
+    assert_equal(false, @bar)
+  end
+
+  def test_with
+    @opt.parse!(%w"--with-zot=foobar")
+    assert_equal("foobar", @zot)
+    @opt.parse!(%w"--without-zot")
+    assert_nil(@zot)
+  end
+
+  def test_without
+    @opt.parse!(%w"--without-zot")
+    assert_nil(@zot)
+    assert_raise(OptionParser::NeedlessArgument) {@opt.parse!(%w"--without-zot=foobar")}
+  end
+
+  def test_help
+    help = @opt.help
+    assert_match(/--enable-foo/, help)
+    assert_match(/--disable-bar/, help)
+    assert_match(/--with-zot/, help)
+    assert_not_match(/--disable-foo/, help)
+    assert_not_match(/--enable-bar/, help)
+    assert_not_match(/--without/, help)
+  end
+end

Property changes on: test/optparse/test_autoconf.rb
___________________________________________________________________
Added: svn:eol-style
   + LF


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]