Class: DNSOMatic::Opts

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/dnsomatic/opts.rb

Overview

A class to handle option parsing for the dnsomatic client. It acts as a singleton and will recreate its internal representation of the options each time parse(ARGS) is called.

Constant Summary collapse

@@opts =
OpenStruct.new

Instance Method Summary collapse

Constructor Details

#initializeOpts

No arguments, but sets the internal options to the defaults.



15
16
17
# File 'lib/dnsomatic/opts.rb', line 15

def initialize
  setdefaults()
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object

This is a simple wrapper that passes method calls to our internal option store (an openstruct) so that a client can call opts.alert and get the value from the @@opts.alert variable.



95
96
97
# File 'lib/dnsomatic/opts.rb', line 95

def method_missing(meth, *args)
  @@opts.send(meth)
end

Instance Method Details

#parse(args) ⇒ Object

Parse and array of arguments and set the interal options. Typically ARGV is passed, but that is not required. Any array of strings that look like arguments will work (makes testing easier).



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/dnsomatic/opts.rb', line 22

def parse(args)
  setdefaults()
  begin
    opts = OptionParser.new do |o|
      o.on('-m', '--minimum SEC', 'The minimum time between updates (def: 30m)') do |i|
        @@opts.minimum = i.to_i
      end

      o.on('-M', '--maximum SEC', 'The maximum time between updates (def: 15d)') do |i|
        @@opts.maximum = i.to_i
      end
      #
      #making this an option (off by default) means we can operate
      #completely silently by default.
      o.on('-a', '--alert', 'Emit an alert if the IP is updated') do |a|
        @@opts.alert = true
      end

      o.on('-n', '--name NAME', 'Only update host stanza NAME') do |n|
        @@opts.name = n
      end

      o.on('-d', '--display-config', 'Display the configuration and exit') do
        @@opts.showcf = true
      end

      o.on('-c', '--config FILE', 'Use an alternate config file') do |f|
        @@opts.cf = f
      end

      o.on('-f', '--force', 'Force an update, even if IP is unchanged') do
        @@opts.force = true
      end

      o.on('-p', '--print', 'Output the update URLs.  No action taken') do
        @@opts.print = true
      end

      o.on('-v', '--verbose', 'Display runtime messages') do
        @@opts.verbose = true
      end

      o.on('-V', '--version', 'Display version and exit') do
        DNSOMatic::Logger.warn(DNSOMatic::VERSION)
        exit
      end

      o.on('-x', '--debug', 'Output additional info in error situations') do
        @@opts.debug = true
      end

      o.on('-h', '--help', 'Display this help text') do
        DNSOMatic::Logger.warn(o)
        exit
      end
    end
    opts.parse!(args)

    if args.size > 0
      raise(DNSOMatic::Error, "Extra arguments given: #{args.join(', ')}")
    end

  rescue OptionParser::ParseError => e
    msg = "Extra/Unknown arguments used:\n"
    msg += "\t#{e.message}\n"
    msg += "Remaining args: #{args.join(', ')}\n" if args.size > 0
    raise(DNSOMatic::Error, msg)
  end
end