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

ruby-changes:36705

From: nobu <ko1@a...>
Date: Fri, 12 Dec 2014 19:49:07 +0900 (JST)
Subject: [ruby-changes:36705] nobu:r48786 (trunk): erb: set variables from the command line

nobu	2014-12-12 19:48:57 +0900 (Fri, 12 Dec 2014)

  New Revision: 48786

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

  Log:
    erb: set variables from the command line
    
    * bin/erb (ARGV.switch, ERB::Main#run): allow variables to be set
      from the command line.  [ruby-core:65772] [Feature #10395]

  Added files:
    trunk/test/erb/test_erb_command.rb
  Modified files:
    trunk/ChangeLog
    trunk/bin/erb
    trunk/lib/erb.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 48785)
+++ ChangeLog	(revision 48786)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri Dec 12 19:48:55 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* bin/erb (ARGV.switch, ERB::Main#run): allow variables to be set
+	  from the command line.  [ruby-core:65772] [Feature #10395]
+
 Fri Dec 12 19:31:44 2014  Nobuyoshi Nakada  <nobu@r...>
 
 	* lib/erb.rb (ERB#lineno): accessor for line number to eval.
Index: lib/erb.rb
===================================================================
--- lib/erb.rb	(revision 48785)
+++ lib/erb.rb	(revision 48786)
@@ -797,7 +797,7 @@ class ERB https://github.com/ruby/ruby/blob/trunk/lib/erb.rb#L797
     @safe_level = safe_level
     compiler = make_compiler(trim_mode)
     set_eoutvar(compiler, eoutvar)
-    @src, @enc = *compiler.compile(str)
+    @src, @encoding = *compiler.compile(str)
     @filename = nil
     @lineno = 0
   end
@@ -812,6 +812,9 @@ class ERB https://github.com/ruby/ruby/blob/trunk/lib/erb.rb#L812
   # The Ruby code generated by ERB
   attr_reader :src
 
+  # The encoding to eval
+  attr_reader :encoding
+
   # The optional _filename_ argument passed to Kernel#eval when the ERB code
   # is run
   attr_accessor :filename
@@ -879,7 +882,7 @@ class ERB https://github.com/ruby/ruby/blob/trunk/lib/erb.rb#L882
   #   print MyClass.new.render('foo', 123)
   def def_method(mod, methodname, fname='(ERB)')
     src = self.src
-    magic_comment = "#coding:#{@enc}\n"
+    magic_comment = "#coding:#{@encoding}\n"
     mod.module_eval do
       eval(magic_comment + "def #{methodname}\n" + src + "\nend\n", binding, fname, -2)
     end
Index: bin/erb
===================================================================
--- bin/erb	(revision 48785)
+++ bin/erb	(revision 48786)
@@ -25,6 +25,8 @@ class ERB https://github.com/ruby/ruby/blob/trunk/bin/erb#L25
           @maybe_arg = nil
         end
         "-#{$1}"
+      when /\A(\w+)=/
+        arg
       else
         self.unshift arg
         nil
@@ -55,6 +57,7 @@ class ERB https://github.com/ruby/ruby/blob/trunk/bin/erb#L57
     def run(factory=ERB)
       trim_mode = 0
       disable_percent = false
+      variables = {}
       begin
         while switch = ARGV.switch
           case switch
@@ -92,14 +95,17 @@ class ERB https://github.com/ruby/ruby/blob/trunk/bin/erb#L95
             disable_percent = true
           when '--help'
             raise "print this help"
-          else
+          when /\A-/
             raise "unknown switch #{switch.dump}"
+          else
+            var, val = *switch.split('=', 2)
+            (variables ||= {})[var] = val
           end
         end
       rescue                               # usage
         STDERR.puts $!.to_s
         STDERR.puts File.basename($0) +
-          " [switches] [inputfile]"
+          " [switches] [var=value...] [inputfile]"
         STDERR.puts <<EOU
   -x               print ruby script
   -n               print ruby script with line number
@@ -111,6 +117,7 @@ class ERB https://github.com/ruby/ruby/blob/trunk/bin/erb#L117
   -U               set default encoding to UTF-8.
   -T trim_mode     specify trim_mode (0..2, -)
   -P               ignore lines which start with "%"
+  var=value        set variable
 EOU
         exit 1
       end
@@ -131,7 +138,15 @@ EOU https://github.com/ruby/ruby/blob/trunk/bin/erb#L138
           puts erb.src
         end
       else
-        erb.run(TOPLEVEL_BINDING.taint)
+        bind = TOPLEVEL_BINDING.taint
+        if variables
+          enc = erb.encoding
+          variables.each do |var, val|
+            val = val.encode(enc) if val
+            bind.local_variable_set(var, val)
+          end
+        end
+        erb.run(bind)
       end
     end
     module_function :run
Index: test/erb/test_erb_command.rb
===================================================================
--- test/erb/test_erb_command.rb	(revision 0)
+++ test/erb/test_erb_command.rb	(revision 48786)
@@ -0,0 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/test/erb/test_erb_command.rb#L1
+# -*- coding: us-ascii -*-
+require 'test/unit'
+
+class TestErbCommand < Test::Unit::TestCase
+  def test_var
+    assert_in_out_err([File.expand_path("../../../bin/erb", __FILE__),
+                       "var=hoge"],
+                      "<%=var%>", ["hoge"])
+  end
+end

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


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

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