28

Here is my rake task

namespace :users do
  task :change_role, [:role] => :environment do |t, args|
    puts args.role
  end
end

I am calling it like this:

rake users:change_role["role"] but I am getting this error no matches found: users:change_role["role"]

brian d foy
  • 129,424
  • 31
  • 207
  • 592
dennismonsewicz
  • 25,132
  • 33
  • 116
  • 189

4 Answers4

61

You need to escape the square brackets when using them in some shells like zsh:

rake users:change_role\["role"\]
user229044
  • 232,980
  • 40
  • 330
  • 338
infused
  • 24,000
  • 13
  • 68
  • 78
  • No it is not. That is the error from your shell telling you there are no matches for that command found. With zsh and no escaping the output is `zsh: no matches found: users:change_role[ok]`. Using zsh with escaping the output is `ok` – infused Jul 03 '14 at 16:39
  • You're correct, its rake saying it can't find the command. In any case, you need to escape the brackets with zsh. – infused Jul 03 '14 at 16:42
  • Ah, actually I think you're right. "No matches found:" is definitely *not* being output by rake; rake would have said "don't know how to build task...". – user229044 Jul 03 '14 at 16:44
  • That escaping isn't correct... I tried this `rake users:change_roles[\"hello\"]` and I am still getting the same error – dennismonsewicz Jul 07 '14 at 12:58
  • Did you mean `rake users:change_role[\"hello\"]`? – infused Jul 07 '14 at 16:07
  • 3
    Great call! the full error is indeed: `zsh: no matches found:` – DaveWoodall.com Jun 14 '20 at 22:38
30

Put the rake task in single quotes.

rake 'users:change_role["role"]'

more on https://thoughtbot.com/blog/how-to-use-arguments-in-a-rake-task

geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
Koko Lingga
  • 301
  • 3
  • 2
9

You can add unsetopt nomatch to your .zshrc file, as Chad Pytel describes in here

Nadav
  • 304
  • 3
  • 11
1

@infused way works, but if you want change to be permanent, so you can simply call rake users:change_roles["hello"], add following to your .zshrc:

alias rake='noglob rake'
sobstel
  • 1,432
  • 16
  • 11