lib/grepg/parser.rb in grepg-0.0.3 vs lib/grepg/parser.rb in grepg-0.0.4

- old
+ new

@@ -1,40 +1,73 @@ require 'trollop' require 'rest-client' +require 'yaml' +require_relative 'version' # This class parses commandline arguments module GrepPage class Parser def initialize(args) + default_config = self.class.get_default_config parser = Trollop::Parser.new do + opt :user, + "username", + :type => :string, + :default => default_config['user'], + :short => "-u" + opt :topic, + "topic", + :type => :string, + :required => true, + :short => "-t" opt :search, "text to search", :type => :string, :required => false, :short => "-s" + opt :colorize, + "colorize output", + :type => :boolean, + :default => default_config['colorize'], + :short => "-c" + version "grepg version #{GrepPage::VERSION}" banner <<-EOS Usage: - grepg user_name topic_name [-s search_term] + grepg -u user_name -t topic_name [-s search_term] Examples: - grepg kdavis css - greppg kdavis css -s color + grepg -u evidanary -t css + greppg -u evidanary -t css -s color + +Defaults: + To set defaults, create a file in ~/.grepg.yml with + user: test + colorize: true EOS end @opts = Trollop::with_standard_exception_handling parser do - raise Trollop::HelpNeeded if args.size < 2 # show help screen parser.parse args end - leftovers = parser.leftovers - @user = leftovers.shift - @topic = leftovers.shift + @user = @opts[:user] + @topic = @opts[:topic] @search_term = @opts[:search] + @colorize = @opts[:colorize] end + def self.get_default_config + file = self.default_file_name + file ? YAML.load(IO.read(file)) : {} + end + + def self.default_file_name + file = ENV['HOME'] + '/.grepg.yml' + File.exist?(file) ? file : nil + end + def get_all_topics(user) GrepPage::API.sheets(user) end def filter_topics(topics, topic_name = '') @@ -52,31 +85,35 @@ (cheat[:description].downcase[search_term.downcase] || cheat[:command].downcase[search_term.downcase]) != nil end end - def run! - headers = ["User: #{@user}", "Topic: #{@topic}"] - headers << "Search-Term: #{@search_term}" if @search_term + def process_args(user, topic, search_term, colorize) + headers = ["User: #{user}", "Topic: #{topic}"] + headers << "Search-Term: #{search_term}" if search_term puts headers.join(", ") begin - topics = get_all_topics(@user) + topics = get_all_topics(user) rescue RestClient::ResourceNotFound puts "That username does not exist" - exit 1 + raise "Unable to find user" end - topic = filter_topics(topics, @topic) + topic = filter_topics(topics, topic) if topic.nil? || topic.empty? puts "Can't find that topic. Choose one of the following" puts topics.map{|t| t[:name]} - exit 1 + raise "Unable to find topic" end - cheats = get_cheats(@user, topic[:id]) - cheats = filter_cheats(cheats, @search_term) if @search_term + cheats = get_cheats(user, topic[:id]) + cheats = filter_cheats(cheats, search_term) if search_term - GrepPage::Formatter.cheat_rows(cheats, @search_term) + GrepPage::Formatter.cheat_rows(cheats, search_term, colorize) + end + + def run! + process_args(@user, @topic, @search_term, @colorize) end end end