lib/pushover/optparser.rb in pushover-0.2.0 vs lib/pushover/optparser.rb in pushover-0.3.1

- old
+ new

@@ -1,17 +1,21 @@ require 'optparse' module Pushover + # override the built-in [OptionParser], adding some nifty features. + # Options[] is a hash value designed to collect the contents added to @options. + class OptionParser < ::OptionParser def initialize super @options = {} on("-V", "--version", "Print version") { |version| @options[:version] = true} on("-u", "--user USER", "Which user, can be a saved name or token.") { |o| @options[:user] = o} on("-a", "--app APPKEY", "Which app to notify, can be a saved name or apikey.") { |o| @options[:appkey] = o} on("-T", "--title [TITLE]", "Set the title of the notification (optional).") { |o| @options[:title] = o} + on("-c", "--config_file [FILE]", "Set the target config file.") {|o| @options[:config_file] = o} on("--save-app NAME", "Saves the application to the config file under NAME.") { |o| @options[:save_app] = [@options[:appkey], o]} on("--save-user NAME", "Saves the user to the config file under NAME.") { |o| @options[:save_user] = [@options[:user], o]} end # This will build an on/off option with a default value set to false. @@ -20,12 +24,14 @@ on "-#{word.chars.first}", "--[no]#{word}", description do |o| Options[word.to_sym] == o end end + # Build out the banner and calls the built in parse! + # Loads any saved options automatically. def parse! - @banner = Pushover::VERSION + @banner = "Send notifications over to pushover.net.\n\n" super if @options[:version] puts Pushover::VERSION exit 0 @@ -36,22 +42,28 @@ Config.each do |k,v| @options[k] = v if !@options[k] && ["applications", "users"].include?(k) end end + # Entry point to the options hash + # @return will return the value of key if provided, else the entire [Hash] def [](k = nil) return @options[k] if k return @options if @options.any? nil end + # Set a value in the option array, used as a way to store the results of the parsed value. def []=(k,v) @options[k] = v end + # Check to see if the option hash has any k/v pairs. + # @return [Boolean] true if any pairs at all, false otherwise. def empty? @options.empty? end end + # Add a built in Options to the Pushover namespace, purely a convenience thing. Options = OptionParser.new end