Thor可执行文件 - 忽略任务名称

dhu*_*han 10 ruby thor

thor wiki页面,Making a Exectable,向您展示如何创建一个类似于以下内容的Thor驱动的CLI命令:

bash ./mythorcommand foo

这要求你传入thor任务foo作为第一个参数.

我还可以使用thor的default_method运行一个没有任何参数的thor可执行文件:

bash ./mythorcommand

但是,我想传入一个变量字符串作为第一个参数:

bash ./mythorcommand "somevalue"

这不起作用,因为thor命令期望第一个参数是任务名称.有没有办法忽略任务名称并将第一个参数发送到默认方法?

如果此功能不存在,我认为添加一个将所有命令行参数传递到一个任务/方法的方法非常有用:

class MyThorCommand < Thor
  only_method :default

  def default(*args)
    puts args.inpsect
  end 
end 

MyThorCommand.start
Run Code Online (Sandbox Code Playgroud)

tfi*_*ach 1

尽管这并不能完全解决您的问题,但一种替代方法可能是Thor.map通过仅提供选项标志来调用命令:

map '-F' => 'foo'
Run Code Online (Sandbox Code Playgroud)

现在你还可以传递参数

mythorcommand -F bar # => invokes foo("bar")
Run Code Online (Sandbox Code Playgroud)