Skip to content

Instantly share code, notes, and snippets.

@ryansch
Last active December 15, 2015 09:39
Show Gist options
  • Select an option

  • Save ryansch/5239584 to your computer and use it in GitHub Desktop.

Select an option

Save ryansch/5239584 to your computer and use it in GitHub Desktop.
Thor namespace with custom binary Fake gem name: example
# lib/example/cli.rb
module Example
class CLI < Thor
include Thor::Actions
include ExitCode
namespace :default
source_root Example.root_path
class_option :verbose, :aliases => '-v', :type => :boolean
class_option :version, :aliases => '-V', :type => :boolean
desc "init", "Initialize the example"
def init
copy_file 'data/config.rb', 'config/example.rb'
end
desc "something <foo> [<other args>]", "Does something"
def something(foo, *args)
run "external_command FOO=#{rails_env} #{args.join(' ')}"
end
# Fixes thor's banners when used with :default namespace
def self.banner(command, namespace = nil, subcommand = false)
"#{basename} #{command.formatted_usage(self, false, subcommand)}"
end
class Bar < Thor
include ExitCode
namespace :bar
desc "other [<more args>]", "Does other"
def other(*args)
run "other_external_command #{args.join(' ')}"
end
end
end
end
# Usage:
# example init
# example something foo
# example bar:other
# bin/example
#!/usr/bin/env ruby
require 'example'
$thor_runner = true
Example::CLI.start(ARGV)
# lib/example.rb
module Example
ROOT = File.expand_path('../../', __FILE__)
require 'example/version'
autoload :ExitCode, 'example/exit_code.rb'
autoload :CLI, 'example/cli.rb'
def self.root_path(*a)
File.join ROOT, *a
end
end
# lib/example/exit_code.rb
module Example
module ExitCode
private
def self.exit_on_failure?
true
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment