Skip to content

Instantly share code, notes, and snippets.

@rogerleite
Created July 3, 2015 14:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rogerleite/994bac68ef71e6dd2078 to your computer and use it in GitHub Desktop.
Save rogerleite/994bac68ef71e6dd2078 to your computer and use it in GitHub Desktop.
Template for ruby script with Gemfile inline and thor to map actions and arguments
#!/usr/bin/env ruby
begin
require "bundler/inline"
rescue LoadError => e
puts "You should install bundler with >= 1.10.3 version."
puts "* Current Ruby: #{`ruby -v`}"
puts "* Current Bundler: #{`gem list bundler`}"
puts "* Original Exception: \"#{e.message}\""
exit 1
end
gemfile(false) do
source 'https://rubygems.org'
gem 'thor', '~> 0.19' # http://whatisthor.com
end
class MyCLI < Thor
class_option :verbose, :type => :boolean, :aliases => :v
desc "hello NAME", "say hello to NAME"
option :from, :required => true
option :yell, :type => :boolean
def hello(name)
puts "> saying hello" if options[:verbose]
output = []
output << "from: #{options[:from]}" if options[:from]
output << "Hello #{name}"
output = output.join("\n")
puts options[:yell] ? output.upcase : output
puts "> done saying hello" if options[:verbose]
end
desc "goodbye", "say goodbye to the world"
def goodbye
puts "> saying goodbye" if options[:verbose]
puts "Goodbye World"
puts "> done saying goodbye" if options[:verbose]
end
end
MyCLI.start(ARGV)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment