Forum: Ruby rake task with arguments

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C0f75c054e0191ad24dd655b2e1b4532?d=identicon&s=25 Reacher (Guest)
on 2008-02-25 21:42
(Received via mailing list)
I found an example (http://www.betweentherails.com/rake/) of passing
arguments to a rake task in the new (0.8.n) version of rake. From this
example, I created the following test:

namespace :foo do
  desc 'lol'
  task :bar, :num do |t, args|
    puts "num = #{args.num}"
  end
end

I took a look at the task list:

$ rake --tasks
(in /path/to/my/dir)
rake foo:bar[num]  # lol

All looks well ... until I try to run it:

$ rake foo:bar[123]
rake: No match

Hmm ..  let's try without the argument:

$ rake foo:bar
(in /path/to/my/dir)
num =

o.O
C0f75c054e0191ad24dd655b2e1b4532?d=identicon&s=25 Reacher (Guest)
on 2008-02-25 21:45
(Received via mailing list)
On Feb 25, 2:39 pm, Reacher <brandon.g.jo...@gmail.com> wrote:
>
>
> Hmm ..  let's try without the argument:
>
> $ rake foo:bar
> (in /path/to/my/dir)
> num =
>
> o.O

I figured it out

$ rake foo:bar\[123\]
(in /path/to/my/dir)
num = 123

BTW, csh is evil
0107ef1bc42d0626a706ca6af9a43060?d=identicon&s=25 Jos Backus (Guest)
on 2008-02-25 21:50
(Received via mailing list)
On Tue, Feb 26, 2008 at 05:39:57AM +0900, Reacher wrote:
> All looks well ... until I try to run it:
>
> $ rake foo:bar[123]
> rake: No match

Try this instead:

   $ rake 'foo:bar[123]'

The shell is interpreting the []'s as globbing metacharacters. You have
to
quote them so the shell passes them to ruby as-is.
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2008-02-25 22:08
(Received via mailing list)
Jos Backus wrote:
> On Tue, Feb 26, 2008 at 05:39:57AM +0900, Reacher wrote:
>> All looks well ... until I try to run it:
>>
>> $ rake foo:bar[123]
>> rake: No match
>
> Try this instead:
>
>    $ rake 'foo:bar[123]'

Yuck.

FWIW, you can also embed arguments in the task name, which makes the
command line cleaner. This is yucky in its own special way.

$ cat rakefile
foo_task_pat = /^foo(\w+)$/

make_foo_dep_name =
   proc do |taskname|
     "foo/#{taskname[foo_task_pat, 1]}"
   end

rule foo_task_pat => make_foo_dep_name do |t|
   puts "handling rule for #{t.name.inspect}"
end

directory "foo"

file "foo/bar" => "foo" do
   system "touch foo/bar"
end
$ rm -rf foo
$ rake foobar
(in /home/vjoel/ruby/misc/rake/args)
handling rule for "foobar"
$ ls foo
bar
0107ef1bc42d0626a706ca6af9a43060?d=identicon&s=25 Jos Backus (Guest)
on 2008-02-25 22:24
(Received via mailing list)
On Tue, Feb 26, 2008 at 06:07:38AM +0900, Joel VanderWerf wrote:
> Jos Backus wrote:
>>    $ rake 'foo:bar[123]'
>
> Yuck.

Hey, using []'s in rake wasn't my idea...
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2008-02-25 22:45
(Received via mailing list)
On Feb 25, 2008, at 3:44 PM, Reacher wrote:

> I figured it out
>
> $ rake foo:bar\[123\]
> (in /path/to/my/dir)
> num = 123
>
> BTW, csh is evil


Of course csh is evil!  That's nothing new.
http://ooblick.com/text/CshProgrammingConsideredHarmful.html

This works just fine with bash:

rab://tmp $ cat Rakefile
namespace :foo do
   desc 'lol'
   task :bar, :num do |t, args|
     puts "num = #{args.num}"
   end
end
rab://tmp $ rake foo:bar[123]
(in /private/tmp)
num = 123

-Rob

Rob Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
C0f75c054e0191ad24dd655b2e1b4532?d=identicon&s=25 Reacher (Guest)
on 2008-02-25 23:05
(Received via mailing list)
On Feb 25, 3:44 pm, Rob Biedenharn <R...@AgileConsultingLLC.com>
wrote:
> Of course csh is evil!  That's nothing 
new.http://ooblick.com/text/CshProgrammingConsideredHarmful.html
> rab://tmp $ rake foo:bar[123]
> (in /private/tmp)
> num = 123
>
> -Rob
>
> Rob Biedenharn          http://agileconsultingllc.com
> R...@AgileConsultingLLC.com

When I got my first real job programming, I had no experience with
*NIX .. at all.  The shell we worked in was tcsh.  Currently, everyone
in our office uses ksh, but I've been slow to conform, since I'm used
to tcsh and it's few but handy niceties.  I think the results of this
thread may be the poke needed to move to bash.
76d5176f2419e5984d7680d1951c0b5e?d=identicon&s=25 Aldric Giacomoni (trevoke)
on 2009-12-09 17:08
Reacher wrote:
> Currently, everyone
> in our office uses ksh, but I've been slow to conform, since I'm used
> to tcsh and it's few but handy niceties.  I think the results of this
> thread may be the poke needed to move to bash.

Skip bash and go to straight to zsh. You won't regret it.
This topic is locked and can not be replied to.