mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
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
This commit is contained in:
parent
e82f4195d4
commit
9e52416cd3
4 changed files with 38 additions and 5 deletions
|
@ -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.
|
||||
|
|
21
bin/erb
21
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
|
||||
|
|
|
@ -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
|
||||
|
|
10
test/erb/test_erb_command.rb
Normal file
10
test/erb/test_erb_command.rb
Normal file
|
@ -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
|
Loading…
Reference in a new issue