summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-12-12 10:48:57 (GMT)
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-12-12 10:48:57 (GMT)
commit9e52416cd304c648e7bf49f1e77c81cd5adac506 (patch)
tree193bed0099ad0599045619bd863c9f85ddbfb975 /bin
parente82f4195d49b730a48031738694391dbb3e41091 (diff)
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
Diffstat (limited to 'bin')
-rwxr-xr-xbin/erb21
1 files changed, 18 insertions, 3 deletions
diff --git a/bin/erb b/bin/erb
index bd04e69..d6d7610 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