require 'optparse' require 'highline/import' module Vmail class Options DEFAULT_CONTACTS_FILENAME = "vmail-contacts.txt" attr_accessor :config attr_accessor :contacts_file def initialize(argv) config_file_locations = ['.vmailrc', "#{ENV['HOME']}/.vmailrc"] @config_file = config_file_locations.detect do |path| File.exists?(File.expand_path(path)) end @contacts_file = [DEFAULT_CONTACTS_FILENAME, "#{ENV['HOME']}/#{DEFAULT_CONTACTS_FILENAME}"].detect do |path| File.exists?(File.expand_path(path)) end @config = {} parse argv end def parse(argv) OptionParser.new do |opts| opts.banner = "Usage: vmail [ options ] [ limit ] [ imap search query ]" opts.separator "" opts.separator "Specific options:" opts.on("-c", "--config path", String, "Path to config file") do |config_file| @config_file = config_file end opts.on("-t", "--contacts path", String, "Path to contacts file") do |file| @contacts_file = file end opts.on("-g[n]", "--getcontacts[n]", Integer, "Generate contacts file. n is number of emails to scan (default 500).") do |n| @get_contacts = true @max_messages_to_scan = n || 500 end opts.on("-v", "--version", "Show version") do require 'vmail/version' puts "vmail #{Vmail::VERSION}\nCopyright 2010 Daniel Choi under the MIT license" exit end opts.on("-h", "--help", "Show this message") do puts opts exit end opts.separator "" opts.separator INSTRUCTIONS begin opts.parse!(argv) if @config_file && File.exists?(@config_file) puts "using config file: #{@config_file}" else puts < e STDERR.puts e.message, "\n", opts end end end end INSTRUCTIONS = <<-EOF Please visit http://danielchoi.com/software/vmail.html for instructions on how to configure and run vmail. EOF end