rake task with arguments

Discussion in 'Ruby' started by Reacher, Feb 25, 2008.

  1. Reacher

    Reacher Guest

    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
    Reacher, Feb 25, 2008
    #1
    1. Advertising

  2. Reacher

    Reacher Guest

    On Feb 25, 2:39 pm, Reacher <> 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
    Reacher, Feb 25, 2008
    #2
    1. Advertising

  3. Reacher

    Jos Backus Guest

    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
    Jos Backus, Feb 25, 2008
    #3
  4. 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
    Joel VanderWerf, Feb 25, 2008
    #4
  5. Reacher

    Jos Backus Guest

    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
    Jos Backus, Feb 25, 2008
    #5
  6. Reacher

    Phlip Guest

    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
    Phlip, Feb 25, 2008
    #6
  7. 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 Biedenharn, Feb 25, 2008
    #7
  8. Reacher

    Reacher Guest

    On Feb 25, 3:44 pm, Rob Biedenharn <>
    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/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
    >


    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.
    Reacher, Feb 25, 2008
    #8
  9. 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/.
    Aldric Giacomoni, Dec 9, 2009
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joe Van Dyk
    Replies:
    2
    Views:
    92
    Damphyr
    Jan 31, 2006
  2. peppermonkey
    Replies:
    1
    Views:
    225
    Gregory Brown
    Feb 10, 2007
  3. Stéphane Wirtel
    Replies:
    3
    Views:
    355
    Stephane Wirtel
    Jun 15, 2007
  4. James Mead
    Replies:
    0
    Views:
    136
    James Mead
    Jan 15, 2008
  5. Glenn Ritz

    Rake task with variable arguments

    Glenn Ritz, Apr 1, 2010, in forum: Ruby
    Replies:
    0
    Views:
    124
    Glenn Ritz
    Apr 1, 2010
Loading...

Share This Page