#!/usr/bin/ruby -w
#
# $Id: stockdump,v 1.2 2004/07/21 10:04:28 ianmacd Exp $
#
# A Ruby port of Perl's Finance::Quote's sample script, stockdump.pl.

require 'optparse'
require 'ostruct'
require 'pp'
require 'finance/quote'

class Optparse

  PROGRAM = $0.sub( /^.*\//, '' )
  USAGE_BANNER = "Usage: #{PROGRAM} [options] source symbol"

  def self.parse( args )
    options = OpenStruct.new
    options.currency = nil

    opts = OptionParser.new do |opts|
      opts.banner = USAGE_BANNER

      opts.on( '-c', '--currency CURRENCY',
	       'Currency in which to return data.' ) do |currency|
	options.currency = currency
      end
    end

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

    begin
      opts.parse!( args )
      options
    rescue OptionParser::InvalidOption
      puts opts
      exit 1
    end

  end

end

include Finance

options = Optparse.parse( ARGV )

unless ARGV.size == 2
  puts Optparse::USAGE_BANNER
  exit 1
end

q = Quote.new
q.currency = options.currency if options.currency
pp q.fetch( *ARGV )
