Class: Pushover::OptionParser

Inherits:
OptionParser
  • Object
show all
Defined in:
lib/pushover/optparser.rb

Overview

override the built-in [OptionParser], adding some nifty features. Options[] is a hash value designed to collect the contents added to @options.

Instance Method Summary collapse

Constructor Details

#initializeOptionParser

Returns a new instance of OptionParser.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/pushover/optparser.rb', line 8

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

Instance Method Details

#[](k = nil) ⇒ Object

Entry point to the options hash

Returns:

  • will return the value of key if provided, else the entire [Hash]



49
50
51
52
53
# File 'lib/pushover/optparser.rb', line 49

def [](k = nil)
  return @options[k] if k
  return @options if @options.any?
  nil
end

#[]=(k, v) ⇒ Object

Set a value in the option array, used as a way to store the results of the parsed value.



56
57
58
# File 'lib/pushover/optparser.rb', line 56

def []=(k,v)
  @options[k] = v
end

#bool_on(word, description = "") ⇒ Object

This will build an on/off option with a default value set to false.



22
23
24
25
26
27
# File 'lib/pushover/optparser.rb', line 22

def bool_on(word, description = "")
  Options[word.to_sym] = false
  on "-#{word.chars.first}", "--[no]#{word}", description  do |o|
    Options[word.to_sym] == o
  end
end

#empty?Boolean

Check to see if the option hash has any k/v pairs.

Returns:

  • (Boolean)

    true if any pairs at all, false otherwise.



62
63
64
# File 'lib/pushover/optparser.rb', line 62

def empty?
  @options.empty?
end

#parse!Object

Build out the banner and calls the built in parse! Loads any saved options automatically.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pushover/optparser.rb', line 31

def parse!
  @banner = "Send notifications over to pushover.net.\n\n"
  super

  if @options[:version]
    puts Pushover::VERSION
    exit 0
  end

  # we need to mash in our config array.  To do this we want to make config
  # options that don't overwrite cli options.
  Config.each do |k,v|
    @options[k] = v if !@options[k] && ["applications", "users"].include?(k)
  end
end