class Catsay::CLI
handle CLI
stuff
Public Class Methods
# File lib/catsay.rb, line 44 def catfiles @catfiles ||= Dir.glob(File.join(File.expand_path(File.dirname(__FILE__)), '..', 'cats', '*.erb')) end
# File lib/catsay.rb, line 48 def cats catfiles.map! { |x| File.basename(x, '.erb') }.to_set end
-
parses command-line arguments
-
finds the cat and makes it meow (or gives an error)
# File lib/catsay.rb, line 16 def run! @options = parse_arguments if @options[:verbose] p @options end # list cats and exits 0 if there are cats, 1 if not. if @options[:list] puts 'these are the cats I know:' cats.each do |cat| puts "- #{cat}" end if cats.size == 0 exit 1 # oh noes! else exit 0 end end if @options[:cat] == :random @options[:cat] = cats.to_a.sample end output_handle.puts Cat.new(template: template).meow(message) end
Private Class Methods
checks if a template exists given a template id if not, raises DeadKitty
exception.
# File lib/catsay.rb, line 96 def heisenberg?(cat) if File.exist? template_path_for(cat) cat else fail DeadKitty end end
returns a file handle for the input if there is one or returns nil
# File lib/catsay.rb, line 56 def input_handle if !@options[:message].nil? nil elsif @options[:input] begin File.open(@options[:input]) rescue Errno::ENOENT $stderr.puts "no such file #{@options[:input]}" exit -1 end else $stdin end end
fetches the input by first looking for an input handle and then looking for options (text specified after arguments)
# File lib/catsay.rb, line 115 def message if input_handle.nil? @options[:message] else input_handle.read end end
check if output is a string if so, open it because that's probably a filename otherwise, just return $stdout
# File lib/catsay.rb, line 74 def output_handle if @options[:output].is_a? String File.open(@options[:output], 'w') else $stdout end end
parses the command-line arguments
# File lib/catsay.rb, line 124 def parse_arguments options = {} OptionParser.new do |opts| opts.banner = 'usage: catsay ...' opts.on('-c', '--cat [TEMPLATE]', 'Chooses the cat.') do |cat| options[:cat] = cat.to_sym || :default end opts.on('-o', '--out [OUTFILE]', 'Output file (default=/dev/stdout)') do |output| options[:output] = output end opts.on('-i', '--in [INFILE]', 'Input file (default=/dev/stdin)') do |input| options[:input] = input end opts.on('-l', '--list', 'List cats and exit') do |_list| options[:list] = true end opts.on('-e', '--verbose', 'Annoying kitty') do |verbose| options[:verbose] = verbose end end.parse! # check if there is a message, otherwise set message to nil message = ARGV.join(' ') options[:message] = message == '' ? nil : message # check if there is a cat template, otherwise set to :default options[:cat] = options[:cat] || :default options end
Given a symbol for a cat template, check if it exists. If it does, return the cat template, otherwise exits with an error
# File lib/catsay.rb, line 85 def template heisenberg?(@options[:cat]) rescue DeadKitty $stderr.puts "I haven't met a kitty named \"#{@options[:cat]}\" yet." exit -1 else File.read(template_path_for(@options[:cat])) end
returns the file path given a template id the template id should be the name of the file in the cats/ directory minus the cats/ and .erb extension
# File lib/catsay.rb, line 108 def template_path_for(template_id) File.join(File.expand_path(File.dirname(__FILE__)), '..', 'cats', "#{template_id}.erb") end