Thor 中带连字符的子命令
Hyphenated subcommand in Thor
我正在用 Thor 编写 CLI gem。现在我有两个子命令,我想用连字符连接它们的名称。但我不知道该怎么做。
这是主要内容class
module CLI
class Base < Thor
desc "api-token COMMAND", "Configure the API token"
subcommand "api-token", ApiToken
这是子命令class
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".
如果我把它写成一个单词或使用下划线,命令就可以正常工作,但我真的更喜欢连字符。
如果有人对此感到疑惑,您可以使用 Thor.map 将字符串映射到 method/command。例如
desc "Command help", "Longer command description"
def package_all
puts "Packing..."
end
map "package-all" => "package_all"
Link 到文档:Thor#map-class_method
不需要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
我正在用 Thor 编写 CLI gem。现在我有两个子命令,我想用连字符连接它们的名称。但我不知道该怎么做。
这是主要内容class
module CLI
class Base < Thor
desc "api-token COMMAND", "Configure the API token"
subcommand "api-token", ApiToken
这是子命令class
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".
如果我把它写成一个单词或使用下划线,命令就可以正常工作,但我真的更喜欢连字符。
如果有人对此感到疑惑,您可以使用 Thor.map 将字符串映射到 method/command。例如
desc "Command help", "Longer command description"
def package_all
puts "Packing..."
end
map "package-all" => "package_all"
Link 到文档:Thor#map-class_method
不需要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