Created
July 3, 2015 14:21
-
-
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
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 | |
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