Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > rake task with arguments

Reply
Thread Tools

rake task with arguments

 
 
Reacher
Guest
Posts: n/a
 
      02-25-2008
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
 
Reply With Quote
 
 
 
 
Reacher
Guest
Posts: n/a
 
      02-25-2008
On Feb 25, 2:39*pm, Reacher <(E-Mail Removed)> wrote:
> 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


I figured it out

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

BTW, csh is evil
 
Reply With Quote
 
 
 
 
Jos Backus
Guest
Posts: n/a
 
      02-25-2008
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.

--
Jos Backus
jos at catnook.com

 
Reply With Quote
 
Joel VanderWerf
Guest
Posts: n/a
 
      02-25-2008
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

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

 
Reply With Quote
 
Jos Backus
Guest
Posts: n/a
 
      02-25-2008
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...

--
Jos Backus
jos at catnook.com

 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      02-25-2008
Jos Backus wrote:

> Hey, using []'s in rake wasn't my idea...


Bash:

$ echo ()
>


The () parens are bashoidal delimiters. Other shells probably obey some standard
there, too. I don't know what they do, but I always escape them with \(\), and
my experiment invoked the dreaded subsidiary command prompt, >.

$ echo []

That doesnt' have this problem!

--
Phlip
 
Reply With Quote
 
Rob Biedenharn
Guest
Posts: n/a
 
      02-25-2008
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/CshProgrammi...edHarmful.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
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
Reacher
Guest
Posts: n/a
 
      02-25-2008
On Feb 25, 3:44*pm, Rob Biedenharn <(E-Mail Removed)>
wrote:
> 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/CshProgrammi...edHarmful.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
> (E-Mail Removed)


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.
 
Reply With Quote
 
Aldric Giacomoni
Guest
Posts: n/a
 
      12-09-2009
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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Rake task with variable arguments Glenn Ritz Ruby 0 04-01-2010 07:28 PM
rake published rdoc version and arity of Rake::Task#execute - wrongnumber of arguments (0 for 1) James Mead Ruby 0 01-15-2008 05:42 PM
[Rake] call a task of a namespace from an other task. Stéphane Wirtel Ruby 3 06-15-2007 06:52 AM
Rake and rake aborted! Rake aborted! undefined method `gem' for main:Object peppermonkey Ruby 1 02-10-2007 04:43 AM
Rake 0.7.0 breaks "rake engine_migrate"? Joe Van Dyk Ruby 2 01-31-2006 12:11 AM



Advertisments