Thor可执行文件-忽略任务名称 [英] Thor Executable - Ignore Task Name

查看:111
本文介绍了Thor可执行文件-忽略任务名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

thor Wiki页面制作可提取对象,向您展示了如何创建一个功能强大的CLI命令,如下所示:

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

bash ./mythorcommand foo

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

This requires you to pass in the thor task foo as the first argument.

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

I can also run a thor executable without any arguments using thor's default_method:

bash ./mythorcommand

bash ./mythorcommand

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

However, I'd like to pass in a variable string as the first argument:

bash ./mythorcommand "somevalue"

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

推荐答案

您应该从Thor :: Group扩展并调用启动方法

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

这篇关于Thor可执行文件-忽略任务名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆