Created
January 9, 2015 12:21
-
-
Save mattheworiordan/f3e84964bb699525a435 to your computer and use it in GitHub Desktop.
Thor with subcommands that work correctly with help
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./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