rubyrakecommand-line-interfacerakefile

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?


Solution

  • 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.