aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-12-12 10:48:57 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-12-12 10:48:57 +0000
commit9e52416cd304c648e7bf49f1e77c81cd5adac506 (patch)
tree193bed0099ad0599045619bd863c9f85ddbfb975
parente82f4195d49b730a48031738694391dbb3e41091 (diff)
downloadruby-9e52416cd304c648e7bf49f1e77c81cd5adac506.tar.gz
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] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48786 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rwxr-xr-xbin/erb21
-rw-r--r--lib/erb.rb7
-rw-r--r--test/erb/test_erb_command.rb10
4 files changed, 38 insertions, 5 deletions
diff --git a/ChangeLog b/ChangeLog
index 548ef3a92c..1d4ea8343b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Fri Dec 12 19:48:55 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * 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@ruby-lang.org>
* lib/erb.rb (ERB#lineno): accessor for line number to eval.
diff --git a/bin/erb b/bin/erb
index bd04e69673..d6d7610aff 100755
--- a/bin/erb
+++ b/bin/erb
@@ -25,6 +25,8 @@ class ERB
@maybe_arg = nil
end
"-#{$1}"
+ when /\A(\w+)=/
+ arg
else
self.unshift arg
nil
@@ -55,6 +57,7 @@ class ERB
def run(factory=ERB)
trim_mode = 0
disable_percent = false
+ variables = {}
begin
while switch = ARGV.switch
case switch
@@ -92,14 +95,17 @@ class ERB
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
-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
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
diff --git a/lib/erb.rb b/lib/erb.rb
index 5c41c37faa..25654d62c5 100644
--- a/lib/erb.rb
+++ b/lib/erb.rb
@@ -797,7 +797,7 @@ class ERB
@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
# 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
# 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
diff --git a/test/erb/test_erb_command.rb b/test/erb/test_erb_command.rb
new file mode 100644
index 0000000000..a21feb004d
--- /dev/null
+++ b/test/erb/test_erb_command.rb
@@ -0,0 +1,10 @@
+# -*- 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