Skip to content

Instantly share code, notes, and snippets.

@chischaschos
Forked from daniel-g/common.thor
Created December 6, 2010 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chischaschos/730683 to your computer and use it in GitHub Desktop.
Save chischaschos/730683 to your computer and use it in GitHub Desktop.
class Common < Thor
desc "passgen [LENGTH]", "generates passwords with a given length (defaults to 8)"
method_options :numeric => :boolean, :downcase => :boolean, :upcase => :boolean, :alphanumeric => :boolean
def passgen(len = 8)
chars = []
if (options.empty?)
chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
else
if options[:alphanumeric]
chars += ("a".."z").to_a + ("A".."Z").to_a
else
chars += ("a".."z").to_a if options[:downcase]
chars += ("A".."Z").to_a if options[:upcase]
end
chars += ("0".."9").to_a if options[:numeric]
end
newpass = ""
1.upto(len.to_i) { |i| newpass << chars[rand(chars.size-1)] }
puts newpass
end
end
class Amazing < Thor
desc "describe NAME", "say that someone is amazing"
method_options :forcefully => :boolean
def describe(name, opts)
ret = "#{name} is amazing"
puts opts["forcefully"] ? ret.upcase : ret
end
desc "hello", "say hello"
def hello
puts "Hello"
end
end
class App < Thor # [1]
map "-L" => :list # [2]
desc "install APP_NAME", "install one of the available apps" # [3]
method_options :force => :boolean, :alias => :string # [4]
def install(name)
user_alias = options[:alias]
if options.force?
puts "forcing"
end
puts "other code"
end
desc "list [SEARCH]", "list all of the available apps, limited by SEARCH"
def list(search="")
puts "listing"
end
end
class App < Thor # [1]
map "-L" => :list # [2]
desc "install APP_NAME", "install one of the available apps" # [3]
method_options :force => :boolean, :alias => :string # [4]
def install(name)
user_alias = options[:alias]
if options.force?
puts "forcing"
end
puts "other code"
end
desc "list [SEARCH]", "list all of the available apps, limited by SEARCH"
def list(search="")
puts "listing"
end
end
module JQuerier
class MyApp < Thor
namespace :jquerier
desc "chat NAME", "creates a new chat"
#method_options :kill => true
#method_option :attributes, :type => :hash, :default => {}, :required => true
method_options :where => :boolean
def chat(name)
puts "Craeting chat name: #{name}, #{options}"
end
end
end
class Counter < Thor
desc "one", "Prints 1, 2, 3"
def one
puts 1
invoke :two
invoke :three
end
desc "two", "Prints 2, 3"
def two
puts 2
invoke :three
end
desc "three", "Prints 3"
def three
puts 3
end
end
class CounterExtra < Thor::Group
# number will be available as attr_accessor
argument :number, :type => :numeric, :desc => "The number to start counting"
desc "Prints the 'number' given upto 'number+2'"
def one
puts number + 0
end
def two
puts number + 1
end
def three
puts number + 2
end
end
class CounterBase < Thor::Group
desc "Prints 1, 2, 3"
def one
puts 1
end
def two
puts 2
end
def three
puts 3
end
end
class Newgem < Thor::Group
include Thor::Actions
namespace :gemerator
# Define arguments and options
argument :name
class_option :test_framework, :default => :test_unit
def self.source_root
File.dirname(__FILE__)
end
def create_lib_file
template('templates/newgem.tt', "#{name}/lib/#{name}.rb")
end
def create_test_file
test = options[:test_framework] == "rspec" ? :spec : :test
create_file "#{name}/#{test}/#{name}_#{test}.rb"
end
def copy_licence
if yes?("Use MIT license?")
# Make a copy of the MITLICENSE file at the source root
copy_file "MITLICENSE", "#{name}/MITLICENSE"
else
say "Shame on you…", :red
end
end
end
class <%= name.capitalize %>
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment