Skip to content

Subcommands

Rajasegar Chandran edited this page Sep 21, 2019 · 5 revisions

You can create subcommands using Thor's subcommand method

#!/usr/bin/env ruby

require 'thor'

class SubCommandBase < Thor
  def self.banner(command, namespace = nil, subcommand = false)
    "#{basename} #{subcommand_prefix} #{command.usage}"
  end

  def self.subcommand_prefix
    self.name.gsub(%r{.*::}, '').gsub(%r{^[A-Z]}) { |match| match[0].downcase }.gsub(%r{[A-Z]}) { |match| "-#{match[0].downcase}" }
  end
end

module App
  class Docs < SubCommandBase
    desc "create", "create docs"
    def create
      # pubish
    end

    desc "publish", "publish docs"
    def publish
      # pubish
    end
  end

  class CLI < Thor
    desc "docs", "create and publish docs"
    subcommand "docs", Docs
  end
end

App::CLI.start
Clone this wiki locally