Skip to content

Instantly share code, notes, and snippets.

@Falkor
Last active November 5, 2016 13:11
Show Gist options
  • Save Falkor/8312e886d30d9fc3b5909f0d8916f02e to your computer and use it in GitHub Desktop.
Save Falkor/8312e886d30d9fc3b5909f0d8916f02e to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
#########################################
# myapp.rb
# Time-stamp: <Sat 2016-11-05 10:42 svarrette>
#
# @description Sample Thor-based CLI to illustrate help display issue
##############################################################################
require 'thor'
require 'thor/actions'
require 'thor/group'
require "thor/error"
module MyApp
VERSION = '0.1.2'
module CLI
#########################
class Base < ::Thor
# Default settings for all my Thor-based classes
include Thor::Actions
check_unknown_options!
def self.exit_on_failure?
true
end
class_option :verbose, :aliases => "-v",
:type => :boolean, :desc => "Enable verbose output mode"
end # class Base
#########################
class SubCommand < Base
namespace :sub
# Override banner method to correct missing subcommand.
# @see https://github.com/erikhuda/thor/issues/261
def self.banner(command, namespace = nil, subcommand = false)
return super if subcommand
"#{basename} #{self.namespace} #{command.formatted_usage(self, $thor_runner, subcommand)}"
end
#......................................
desc "cmd", "Subcommand example in a separate namespace"
method_option :output, :aliases => '-o', :desc => 'Output option'
def cmd()
say '#cdm', :green
end # subcmd
end # class SubCommand
###################
class Main < Base
map %w[-h --help] => :help # FAIL
#...........................................
desc "init PATH", "initialize MyApp in PATH"
method_option :force, :default => false, :type => :boolean, :desc => 'Force creation'
def init(path = '.')
path = File.expand_path(path)
say "#init(#{path})", :green
end # task init
#......................................
map %w[-v --version] => :version
desc "version", "Display version"
def version
say 'MyApp version' + MyApp::VERSION, :yellow
end # version
##### sub <subcommand> ##############
desc "sub SUBCOMMAND", "A subcommand"
subcommand "sub", MyApp::CLI::SubCommand
end # class Main
end
end
##### Let's start the runner
begin
MyApp::CLI::Main.start(ARGV, :debug => true)
rescue Thor::InvocationError => e
Thor::Actions.say e.message, :red
exit 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment