Thor 可执行文件 - 忽略任务名称
thor wiki 页面 制作可执行文件 向您展示了如何创建雷神驱动的 CLI 命令看起来像这样:
bash ./mythorcommand foo
这要求您传入 thor 任务 foo 作为第一个参数。
我还可以使用 thor 的 default_method 运行不带任何参数的 thor 可执行文件:
bash ./神话命令
但是,我想传递一个变量字符串作为第一个参数:
bash ./mythorcommand“某个值”
这不起作用,因为 thor 命令期望第一个参数是任务名称。有没有办法忽略任务名称并将第一个参数发送给默认方法?
如果此功能不存在,我认为添加一种将所有命令行参数传递到一个任务/方法中的方法将非常有用:
class MyThorCommand < Thor
only_method :default
def default(*args)
puts args.inpsect
end
end
MyThorCommand.start
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
This requires you to pass in the thor task foo as the first argument.
I can also run a thor executable without any arguments using thor's default_method:
bash
./mythorcommand
However, I'd like to pass in a variable string as the first argument:
bash
./mythorcommand "somevalue"
This doesn't work because thor commands expect the first argument to the be a task name. Is there a way to ignore the task name and send the first argument to a default method?
If this functionality doesn't exist, I think it would be very useful to add a method that would pass all commandline arguments into one task/method:
class MyThorCommand < Thor
only_method :default
def default(*args)
puts args.inpsect
end
end
MyThorCommand.start
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该从 Thor::Group 扩展并调用 start 方法
You should extend from Thor::Group and that call start method
我找到了一个相当“奇怪”的解决方案来解决这个问题,它对我来说效果很好。
您向 Thor 添加了一个默认任务。然后添加 method_missing ,这样如果您的应用程序有参数,您就可以欺骗 Thor 将默认方法作为参数传递。
从您的示例来看,解决方案如下所示:
如果它位于文件“my_thor.rb”中,则执行“ruby my_thor.rb foo bar”将显示“[“foo”,“bar”]”结果。
希望有帮助。
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:
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.
虽然这并不能完全解决您的问题,但一种替代方法可能是使用
Thor。 map
仅通过提供选项标志来调用命令:现在您还可以传递参数
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:Now you can also pass parameters