Ruby Thor based executable with namespaces
Asked Answered
T

1

6

Is it possible to create a Thor based Ruby executable that accepts namespaces? To allow, for example, the following from the commandline: ./thorfile greet:formal

Given I have the following thorfile:

#!/usr/bin/env ruby

require 'rubygems'
require 'thor'

class TalkTasks < Thor
  namespace       "talk"

  desc      "greet", "says hello"
  def greet
    puts "Hello!"
  end

  class Formal < Thor
    namespace "talk:formal"

    desc    "greet", "says a formal hello"
    def greet
      puts "Good evening!"
    end
  end

end

TalkTasks.start

This thorfile provides the following tasks (thor -T):

thor talk:formal:greet  # says a formal hello
thor talk:greet         # says hello

I can also use thorfile directly as an executable:

./thorfile greet

Which displays:

Hello!

How can I get ./thorfile formal:greet (or something similar) to execute the greet method in the Formal class, in order to display:

Good evening!

Theoretician answered 3/7, 2011 at 8:39 Comment(1)
Take a look at this answer: #5664019Delano
O
0

Change the namespace for Formal class

class Formal < Thor
    namespace "formal"
    ...
end

You have nested the classes so the namespaces nest. If you separate them than you can do talk:formal hehe no time to test it. Should work.

Orthodontics answered 6/9, 2012 at 12:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.