Class: HomeQ::Base::Options::Options

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/homeq/base/options.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOptions

Returns a new instance of Options.



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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/homeq/base/options.rb', line 49

def initialize
  @options               = OpenStruct.new
  @options.log_level     = false
  @options.config_file   = File.join(HOMEQ_APP_ROOT,
                                     '/config/homeq.cfg')
  @options.queue_name    = nil
  @options.cp_port       = nil
  @options.foreground    = false
  @options.pid_file      = false
  @options.log_file      = false
  @options.dump_topology = false
  @options.enable_debugging = false
  
  @parser = OptionParser.new do |opts|
    opts.banner = "Usage: #{File.basename($0)} [options]"
    
    opts.separator ""
    opts.separator "HOMEQ options:"

    # Mandatory argument.
    opts.on("-c", "--config-file FILE",
            "Configuration file; defaults to \n\
                             #{@options.config_file}") { |c|
      @options.config_file = c
    }
    opts.on("-q", "--queuename NAME",
            "This process's queue name") { |q|
      @options.queue_name = q
    }
    opts.on("-p", "--port PORT",
            Integer,
            "Control port to listen on.") { |p|
      @options.cp_port = p
    }
    opts.on("-l", "--[no-]log-file LOGFILE",
            "Log to this file; defaults to \n\
                             #{@options.log_file}") { |o|
      @options.log_file = o
    }
    opts.on("-P", "--pid-file FILE",
            "Write pid to this file;\n \
                            defaults to " +
            "/var/run/hq_<queuename>.pid") { |p|
      @options.pid_file = p
    }

    # Boolean switches
    
    opts.on("-v",
            "--[no-]verbose",
            "Run verbosely. Additively -vvv") do |v|
      @options.log_level = 0 unless @options.log_level
      @options.log_level += 1
    end
    opts.on("-f", "--[no-]foreground", "Run in the foreground") do |o|
      @options.foreground = o
    end
    opts.on("-T", "--print-topology",
            "Using command line options and config file,\n \
                            dump a topology in YAML format.") do |o|
      @options.dump_topology = o
    end
    opts.on("-E", "--print-homeq-env",
            "Using command line options and config file,\n \
                            display currently set HOMEQ_ENV.") do |o|
      @options.dump_env = o
    end
    opts.on("-D", "--enable-debugging",
            "Enable rdebug debugging") do |o|
      @options.enable_debugging = o
    end

    opts.separator ""
    opts.separator "Common options:"

    opts.on("-h", "--help", "Show this message") do
      puts opts
      exit
    end

    # Another typical switch to print the version.
    opts.on("--version", "Show version") do
      puts OptionParser::Version.join('.')
      Kernel::exit
    end
  end
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



46
47
48
# File 'lib/homeq/base/options.rb', line 46

def options
  @options
end

#parserObject

Returns the value of attribute parser.



47
48
49
# File 'lib/homeq/base/options.rb', line 47

def parser
  @parser
end

Instance Method Details

#parseObject

Return a structure describing the options.



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/homeq/base/options.rb', line 140

def parse
  # The options specified on the command line will be
  # collected in *options*.  We set default values here.

  begin
    @parser.parse!(ARGV)
  rescue OptionParser::ParseError => pe
    puts @parser
    raise
  end

  # Add our cute little override method
  class << @options
    def set_configuration(config)
      @table.each { |k,v|
        if config.respond_to?(k) && v
          config.send(k, v)
        end
      }
    end
  end
  
  @options
end