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

发布于 2025-01-07 09:02:15 字数 951 浏览 0 评论 0原文

在我的 gem 中,我希望有一个带有参数的可执行命令,如下所示:

foo generate project
foo generate config
foo say_hi

所以我制作了

foo/bin/foo

#!/usr/bin/env ruby
require 'foo'
Foo::Foo.start

并将 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

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

我想在其中指定从 Thor::Group 继承的类,如 katz 示例......

module Foo
  module Generators
    class Project < Thor::Group
      include Thor::Actions
      ....
    end
  end
end

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

foo generate config

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

攀登最高峰 2025-01-14 09:02:15

一开始我也很难让它发挥作用。这是我开始使用的模式:

$ 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.rbgreet

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删除

Deleting a sub-thing

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文