class CddbParser::GetOpts

Constants

LIST_ABBREV
LIST_CODES

Public Class Methods

parse(args) click to toggle source
# File lib/cddb_parser.rb, line 11
def self.parse(args)
  #options = {}
  options = OpenStruct.new
  #options.list = 'all'
  options.verbose = false

  OptionParser.new do |opts|
    opts.banner = "Usage: cddb_parser [options]"
    opts.separator ""
    opts.separator "Specific options:"

    opts.on("-C", "--check", "check files only") do |c|
      options.check = 1
    end

    fmt_list = (LIST_ABBREV.keys + LIST_CODES).join(',')
    #fmt_list = LIST_ABBREV.each {|k,v| "#{k}/#{v}"}
    opts.on("-L", "--list [FMT]", LIST_ABBREV, LIST_CODES, "Select list format",
      "(#{fmt_list})") do |fmt|
      if options.check
        STDERR.puts "error: cannot use -L and -C together"
        exit 1
      end
      options.list = fmt.nil? ? 'all' : fmt
    end

    opts.on("-S", "--store DATABASE", "store info in database") do |s|
      if options.check
        STDERR.puts "Can't run store and check together"
        exit 1
      elsif options.list
        STDERR.puts "Can't run list and store together"
        exit 1
      end
      options.store = s
    end

    opts.separator ""
    opts.separator "Store options:"

    opts.on("-p", "--password PASSWORD", "database password") do |p|
      unless options.store
        STDERR.puts "Store option must be specified for password"
        exit 1
      end
      options.password = p
    end

    opts.on("-u", "--user USER", "database username") do |u|
      unless options.store
        STDERR.puts "Store option must be specified for user"
        exit 1
      end
      options.user = u
    end

    opts.separator ""
    opts.separator "Helper options:"

    opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
      options.verbose = v
    end

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

    # No argument, shows at tail.  This will print an options summary.
    # Try it and see!
    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end

  end.parse!

  options.files = ARGV
  if options.files.empty?
    STDERR.puts "At least one CDDB file must be specified!"
    exit 1
  end
  options
end