Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Wednesday, January 27, 2016

How can I use a comma in a string argument to a rake task?

How can I use a comma in a string argument to a rake task?


I have the following Rakefile:

task :test_commas, :arg1 do |t, args|    puts args[:arg1]  end  

And want to call it with a single string argument containing commas. Here's what I get:

%rake 'test_commas[foo, bar]'  foo    %rake 'test_commas["foo, bar"]'  "foo    %rake "test_commas['foo, bar']"  'foo    %rake "test_commas['foo,bar']"  'foo    %rake "test_commas[foo\,bar]"  foo\  

I'm currently using the workaround proposed in this pull request to rake, but is there a way to accomplish this without patching rake?

Answer by Benjamin Udink ten Cate for How can I use a comma in a string argument to a rake task?


Have you tried escaping the , with a \?

Answer by eugen for How can I use a comma in a string argument to a rake task?


I'm not sure it's possible. Looking at lib/rake/application.rb, the method for parsing the task string is:

def parse_task_string(string)    if string =~ /^([^\[]+)(\[(.*)\])$/      name = $1      args = $3.split(/\s*,\s*/)    else      name = string      args = []    end     [name, args]  end   

It appears that the arguments string is split by commas, so you cannot have an argument that contains a comma, at least not in the current rake-0.9.2.

Answer by knut for How can I use a comma in a string argument to a rake task?


Eugen already answered, why it doesn't work.

But perhaps the following workaround may help you:

task :test_commas, :arg1, :arg2 do |t, args|    arg = args.to_hash.values.join(',')    puts "Argument is #{arg.inspect}"  end  

It takes two arguments, but joins them to get the 'real' one.

If you have more then one comma, you need more arguments.


I did some deeper research and found one (or two) solution. I don't think it's a perfect solution, but it seems it works.

require 'rake'  module Rake    class Application      #usage:       #   rake test_commas[1\,2\,3]      def parse_task_string_masked_commas(string)        if string =~ /^([^\[]+)(\[(.*)\])$/          name = $1          args = $3.split(/\s*(?

The version parse_task_string_masked_commasallows calls with masked commas:

rake test_commas[1\,2\,3]  

The version parse_task_string_combined allows:

rake test_commas[\"1,2,3\"]  

At least under windows, the " (or ') must be masked. If not, they are already deleted until the string reached Rake::Aplication (probably shell substitution)

Answer by Tombart for How can I use a comma in a string argument to a rake task?


Changing rake is quite a dirty fix. Try using OptionParser instead. The syntax is following

$ rake mytask -- -s 'some,comma,sepparated,string'  

the -- is necessary to skip the rake way of parsing the arguments

and here's the ruby code:

task :mytask do    options = {}      optparse = OptionParser.new do |opts|      opts.on('-s', '--string ARG', 'desc of my argument') do |str|        options[:str] = str      end        opts.on('-h', '--help', 'Display this screen') do             puts opts                                                                  exit                                                                            end     end      begin       optparse.parse!      mandatory = [:str]      missing = mandatory.select{ |param| options[param].nil? }      if not missing.empty?        puts "Missing options: #{missing.join(', ')}"        puts optparse        exit      end      rescue OptionParser::InvalidOption, OptionParser::MissingArgument      puts $!.to_s      puts optparse      exit      end      puts "Performing task with options: #{options.inspect}"    # @TODO add task's code  end  

Answer by jkelley for How can I use a comma in a string argument to a rake task?


Another simple workaround is to use a different delimiter in your arguments.

It's pretty simple to swap to a pipe | character (or another) instead of your comma separated args. Rake parses your arguments correctly in this case and allows you to split the first for an array.

desc 'Test pipe separated arguments'  task :test_pipes, :arg1, :arg2  do |t, args|    puts ":arg1 is: #{ args[:arg1] }"    puts ":arg2 still works: #{ args[:arg2] }"    puts "Split :arg1 is: #{ args[:arg1].split('|') }"  end  

Call it with:

rake test_pipes["foo|bar",baz]  

Answer by Gabe Kopley for How can I use a comma in a string argument to a rake task?


Rake task variables are not very convenient generally, instead use environment variables:

task :my_task do    ENV["MY_ARGUMENT"].split(",").each_with_index do |arg, i|      puts "Argument #{i}: #{arg}"    end  end  

Then you can invoke with

MY_ARGUMENT=foo,bar rake my_task  

Or if you prefer

rake my_task MY_ARGUMENT=foo,bar  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.