Thor Executable - Ignore Task Name

瘦欲@ 提交于 2019-12-03 12:15:50

You should extend from Thor::Group and that call start method

class Test < Thor::Group
  desc "Act description"
  def act
    puts "do smth"
  end
end

Test.start

I found a rather 'strange' solution for this problem that is working quite well with me.

You add a default task to Thor. Than you add the method_missing so that you can trick Thor into passing the default method as an argument if there are parameters to your application.

Taking from your example, the solution would look like this:

class MyThorCommand < Thor
  default_task :my_default

  desc "my_default", "A simple default"
  def my_default(*args)
    puts args.inspect
  end 

  def method_missing(method, *args)
    args = ["my_default", method.to_s] + args
    MyThorCommand.start(args)
  end

end 

MyThorCommand.start(ARGV)

If this is in the file "my_thor.rb" an execution "ruby my_thor.rb foo bar" would show '["foo", "bar"]' as a result.

Hope it helps.

Though this does not exactly solve your problem, one alternative might be using Thor.map to invoke a command by only giving an option flag:

map '-F' => 'foo'

Now you can also pass parameters

mythorcommand -F bar # => invokes foo("bar")
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!