botp
botp

Reputation: 31

how to pass arguments and options to rake, like i do in thor

I have a working thor command. now I want it wrapped in rake and called rake thereafter, which rake then calls thor.

eg, my thor call:

$ thor my_sample_thor_class:hello friend --from messenger

from: messenger
Hello, friend!

how to i call something like this in rake? (like just replacing thor with rake)

this is my called thor.


require 'thor'

class MySampleThorClass < Thor

  desc "hello NAME", "say hello to NAME"
  method_option :name, alias: "-n", type: :string, desc: "greet NAME" 
  method_option :from, default: "whatever"
  method_option :message #, default: "just checking :)"

  def hello(name="there")
    puts "from: #{options[:from]}" if options[:from]
    puts "Hello, #{name}!"
    puts options[:message] if options[:message]
  end

end

this is my calling rake.

require 'thor'
load 'lib/tasks/my_sample_thor_class.thor'

namespace :sample_thor do
  desc "Say hello using Thor"
  task :say_hello_thor => :environment do |t, args|
    ARGV.shift
    MySampleThorClass.start ARGV
  end
end

checking...


rake -T | grep -i thor
rake sample_thor:say_hello_thor   # Say hello using Thor

rake sample_thor:say_hello_thor
Commands:
  rake hello NAME      # say hello to NAME
  rake help [COMMAND]  # Describe available commands or one specific command


now invoking just hello.

rake sample_thor:say_hello_thor hello 

from: whatever
Hello, there!

rake aborted!
Don't know how to build task 'hello' (See the list of available tasks with `rake --tasks`)

errs but close.

invoking with argument.

rake sample_thor:say_hello_thor hello friend

from: whatever
Hello, friend!

rake aborted!
Don't know how to build task 'hello' (See the list of available tasks with `rake --tasks`)

errs but closer.

invoking with option --from


rake sample_thor:say_hello_thor hello friend --from whoever
invalid option: --from

from there on, it's a merry go round...

Upvotes: 0

Views: 12

Answers (0)

Related Questions