How do I create thor::group generators as args of my_command

北战南征 提交于 2019-12-03 21:49:36

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!