Thor中的连字符子命令

时间:2016-10-15 21:52:01

标签: ruby thor

我正在用Thor写一个CLI宝石。现在我有两个名字的子命令,我想要连字符。但我无法弄清楚如何做到这一点。

这是主要的课程

module CLI
  class Base < Thor

    desc "api-token COMMAND", "Configure the API token"
    subcommand "api-token", ApiToken

这是子命令类

module CLI
  class ApiToken < Thor
    include Shared

    namespace "api-token"

子命令显示在主help输出中,如果我输入

$ bundle exec bin/cli help api-token

它显示了子命令操作的正确输出。所以至少有些东西在连接。

但是当我尝试使用命令时,这就是我所看到的

$ bundle exec bin/cli api-token set
> Could not find command "api-token".

如果我用一个单词或使用下划线,该命令工作正常,但我真的更喜欢连字符。

2 个答案:

答案 0 :(得分:2)

不需要map,只需

class Test < Thor
  desc 'howto-dash', "dash in command name"
  def howto_dash
    puts "dashing through the snow"
  end
end

输出:

> thor list
test
----
thor test:howto-dash  # dash in command name

答案 1 :(得分:0)

如果有人想知道这一点,您可以使用Thor.map将字符串映射到方法/命令。实施例

knit_child

链接到文档:Thor#map-class_method