From 9e52416cd304c648e7bf49f1e77c81cd5adac506 Mon Sep 17 00:00:00 2001 From: nobu Date: Fri, 12 Dec 2014 10:48:57 +0000 Subject: [PATCH] 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 --- ChangeLog | 5 +++++ bin/erb | 21 ++++++++++++++++++--- lib/erb.rb | 7 +++++-- test/erb/test_erb_command.rb | 10 ++++++++++ 4 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 test/erb/test_erb_command.rb 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 + + * 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 * 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 <", ["hoge"]) + end +end