Thor Executable - Ignore Task Name

后端 未结 3 1547
我在风中等你
我在风中等你 2021-02-13 19:42

The thor wiki page, Making an Exectable, shows you how to create a thor powered CLI command that looks something like this:

bash ./mythorcommand foo

<
3条回答
  •  灰色年华
    2021-02-13 20:12

    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.

提交回复
热议问题