如何创建thor :: group生成器作为my_command的参数 [英] How do I create thor::group generators as args of my_command
问题描述
foo生成项目$ b在我的宝石中,我想要一个带有args的可执行命令,如下所示: $ b foo生成配置
foo say_hi
所以我做了
#!/ usr / bin / env ruby
require'foo'
Foo :: Foo.start
foo / lib / thor / foo.rb中的Foo文件
module Foo
类Foo< Thor
descgenerate [WHAT]
def generate(* args)
end
descsay_hi
def say_hi(* args)
....
结束
结束
结束
foo / lib / thor / generators / project.rb
和 foo / lib / thor / generators / config。 rb
在哪里我想指定从Thor :: Group继承的类,例如katz示例...
module Foo
模块生成器
class Project< Thor :: Group
包括Thor :: Actions
....
end
end
end
所以我的问题是:我如何设置,以便可以从可执行文件调用这些生成器,如:
foo生成配置
我在正确的轨道上甚至?理想情况下,自行输入 foo
应该为 say_hi
和所有生成器提供帮助。
我一开始也遇到了麻烦。以下是我开始使用的模式:
$ cat cli.rb
#!/ usr / bin / env ruby
require'rubygems'
require'thor'
require'thor / group'
模块CLI
class Greeter< Thor :: Group
def say_hi
sayHi
end
def say_goodbye
sayGoodbye
end
end
结束
模块CLI
class Crud< Thor
desc'create','创建一个子物件'
def创建
说创建子物件
结束
desc'删除','删除子物件'
def delete
说删除子物件
结束
结束
结束
模块CLI
class Root< Thor
注册CLI :: Greeter,'greet','greet','执行多步骤子任务'
注册CLI :: Crud,'crud','crud [COMMAND]','Delegates到一个子命令'
end
end
CLI :: Root.start
$ ./cli.rb
任务:
cli.rb crud [ COMMAND]#委托给一个子命令
cli.rb greet#执行多步骤子任务
cli.rb help [任务]#描述可用任务或一个特定任务
$ ./cli.rb greet
Hi
Goodbye
$ ./cli.rb crud
任务:
cli.rb crud create#创建一个子物件
cli.rb crud delete#删除一个子物件,事物
cli.rb crud help [命令]#描述子命令或一个特定的子命令
$ ./cli.rb crud create
创建一个sub-thi ng
$ ./cli.rb crud删除
删除子物件
In my gem I'd like to have a an executable command with args like so:
foo generate project
foo generate config
foo say_hi
So I made
foo/bin/foo
#!/usr/bin/env ruby
require 'foo'
Foo::Foo.start
And the Foo file in foo/lib/thor/foo.rb
module Foo
class Foo < Thor
desc "generate [WHAT]"
def generate(*args)
end
desc "say_hi"
def say_hi(*args)
....
end
end
end
And foo/lib/thor/generators/project.rb And foo/lib/thor/generators/config.rb
Where I'd like to specify classes inherited from Thor::Group like katz examples ...
module Foo
module Generators
class Project < Thor::Group
include Thor::Actions
....
end
end
end
So my question is: How do I set things up so that I can call those generators from the executable like:
foo generate config
Am I on the right track even? Ideally, typing foo
on its own should give help for say_hi
and for all the generators.
I had trouble getting this to work at first, too. Here's the pattern that I've started using:
$ cat cli.rb
#!/usr/bin/env ruby
require 'rubygems'
require 'thor'
require 'thor/group'
module CLI
class Greeter < Thor::Group
def say_hi
say "Hi"
end
def say_goodbye
say "Goodbye"
end
end
end
module CLI
class Crud < Thor
desc 'create', 'Creates a sub-thing'
def create
say "Creating a sub-thing"
end
desc 'delete', 'Deletes a sub-thing'
def delete
say "Deleting a sub-thing"
end
end
end
module CLI
class Root < Thor
register CLI::Greeter, 'greet', 'greet', 'Executes a multi-step subtask'
register CLI::Crud, 'crud', 'crud [COMMAND]', 'Delegates to a sub-command'
end
end
CLI::Root.start
$ ./cli.rb
Tasks:
cli.rb crud [COMMAND] # Delegates to a sub-command
cli.rb greet # Executes a multi-step subtask
cli.rb help [TASK] # Describe available tasks or one specific task
$ ./cli.rb greet
Hi
Goodbye
$ ./cli.rb crud
Tasks:
cli.rb crud create # Creates a sub-thing
cli.rb crud delete # Deletes a sub-thing
cli.rb crud help [COMMAND] # Describe subcommands or one specific subcommand
$ ./cli.rb crud create
Creating a sub-thing
$ ./cli.rb crud delete
Deleting a sub-thing
这篇关于如何创建thor :: group生成器作为my_command的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!