Skip to content

Instantly share code, notes, and snippets.

@mattheworiordan
Created January 9, 2015 12:21
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mattheworiordan/f3e84964bb699525a435 to your computer and use it in GitHub Desktop.
Save mattheworiordan/f3e84964bb699525a435 to your computer and use it in GitHub Desktop.
Thor with subcommands that work correctly with help
#!/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
$ ./test.rb
Commands:
test.rb docs # create and publish docs
test.rb help [COMMAND] # Describe available commands or one specific command
$ ./test.rb docs
Commands:
test.rb docs create # create docs
test.rb docs help [COMMAND] # Describe subcommands or one specific subcommand
test.rb docs publish # publish docs
$ ./test.rb help docs
Commands:
test.rb docs create # create docs
test.rb docs help [COMMAND] # Describe subcommands or one specific subcommand
test.rb docs publish # publish docs
$ ./test.rb docs help create
Usage:
test.rb docs create
create docs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment