如何创建thor :: group生成器作为my_command的args

Jul*_*ann 2 ruby gem generator thor

在我的gem中,我想要一个带有args的可执行命令,如下所示:

foo generate project
foo generate config
foo say_hi
Run Code Online (Sandbox Code Playgroud)

所以我做了

富/斌/富

#!/usr/bin/env ruby
require 'foo'
Foo::Foo.start
Run Code Online (Sandbox Code Playgroud)

和Foo文件在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
Run Code Online (Sandbox Code Playgroud)

foo/lib/thor/generators/project.rbfoo/lib/thor/generators/config.rb

我想在哪里指定继承自Thor :: Group的类,如katz示例...

module Foo
  module Generators
    class Project < Thor::Group
      include Thor::Actions
      ....
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

所以我的问题是:我如何设置,以便我可以从可执行文件调用这些生成器,如:

foo generate config
Run Code Online (Sandbox Code Playgroud)

我是否在正确的轨道上?理想情况下,单独打字foo应该say_hi为所有生成器提供帮助.

Jas*_*rth 7

我一开始也难以开始工作.这是我开始使用的模式:

$ 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
Run Code Online (Sandbox Code Playgroud)

$ ./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
Run Code Online (Sandbox Code Playgroud)

$ ./cli.rb问候

Hi
Goodbye
Run Code Online (Sandbox Code Playgroud)

$ ./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
Run Code Online (Sandbox Code Playgroud)

$ ./cli.rb crud创建

Creating a sub-thing
Run Code Online (Sandbox Code Playgroud)

$ ./cli.rb crud删除

Deleting a sub-thing
Run Code Online (Sandbox Code Playgroud)