[LRUG] Extending Optparse options in Sinatra?

Jared Patterson Jared.Patterson at betfair.com
Sat Jan 14 10:01:49 PST 2012


Hey Guys,

sinatra: 1.3.2

I seem to have got completely stuck right at the end of building a little app. The app tails a log file, which each line is a monitoring event, xml based, and after making some decisions on the alert, I store them as hash keys and am using Sinatra to respond to a particular route and display all listed alerts (hash keys) stored. The issue is that I want to be able to specify some of my own options, which i'm doing by using Optparse, however Sinatra is already using this for its own options, and i'm confused as to how I can extend these built in options. From posts I have read, seems I need to do the following:

------------------
require 'optparse'
require 'logger'
require 'rubygems'
require 'sinatra/base'
Include Sinatra::Delegator

#
# Classes removed to keep this short
#

# Logger Defaults
logger = Logger.new(STDOUT)
logger.level = Logger::INFO

# Optparser Options
options = {}

OptionParser.new do |opts|
  script_name = File.basename($0)
  opts.banner = "Usage: #{script_name} [options]"

  opts.on('-x')        {       set :lock, true }
  opts.on('-e env')    { |opt| set :environment, opt.to_sym }
  opts.on('-s server') { |opt| set :server, opt }
  opts.on('-p port')   { |opt| set :port, opt.to_i }
  opts.on('-o addr')   { |opt| set :bind, opt }

  opts.on('-l', '--logfilepath', 'Path to EventMonitor log file') do |opt|
    options[:logfilepath] = opt
    puts opt
  end

  opts.on('-v', '--verbose', 'Enable verbose logging') do
    logger.level = Logger::DEBUG
  end

  opts.on_tail('-h', '--help', 'Show this message') do
    puts opts.help
    exit
  end
end.parse!

raise OptionParser::MissingArgument, "logfilepath option is missing?" if options[:logfilepath].nil?

puts options[:logfilepath]
------------------

However, running this app specifying only "-l /path/to/my/log.file", it seems options[:logfilepath] just gets set to "true", any ideas?


------------------
iHAM10523:~ pattersonj$ ruby ~/study/ruby/scripts/sonic_webping.rb -l /tmp/monitor.xml
true
/Users/pattersonj/study/ruby/scripts/sonic_webping.rb:20:in `initialize': can't convert true into String (TypeError)
from /Users/pattersonj/study/ruby/scripts/sonic_webping.rb:20:in `open'
from /Users/pattersonj/study/ruby/scripts/sonic_webping.rb:20:in `tail'
from /Users/pattersonj/study/ruby/scripts/sonic_webping.rb:263
------------------


Cheers,

Jared Patterson
Linux Application Engineer
Product Delivery and Support

Office: +44 (0) 20 8834 6506
Yahoo IM: patterson.jared84

Betfair. The World’s Biggest Betting Community.

Please consider the environment before printing this e-mail.
Betfair Limited | Winslow Road | Hammersmith Embankment | London | W6 9HP. Registered in England and Wales under company number 5140986.


________________________________________________________________________
In order to protect our email recipients, Betfair Group use SkyScan from 
MessageLabs to scan all Incoming and Outgoing mail for viruses.

________________________________________________________________________
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20120114/53f7eb6a/attachment.html>


More information about the Chat mailing list