class ArgParser

require_relative 'constants'

Public Class Methods

parse(args) click to toggle source

Returns a structure describing the options.

# File lib/argparser.rb, line 37
def self.parse(args)
        # The options specified on the command line will be collected in <b>options</b>.
        # We set default values here.
        options = OpenStruct.new
        @log = @@log
        $log_level = Logger::INFO

        options.continue = false
        options.list_only = false
        options.queued_item = nil

        op = OptionParser.new do |opts|
                opts.banner = "\nUsage" << ":\t" << "%s <-cp> [url] [local file]" %($0) << "\n\t" << "%s [url] [local file] <-cp>" %($0)
                opts.banner << "\n\t" << "%s [-q NUMBER] <-cp>" %($0)
                opts.banner << "\n\t" << "%s [-lpchv]" %($0)

                opts.separator ""
                opts.separator "Specific options:"

                opts.on('-q' << ' NUMBER', '--queued NUMBER', "Retry to download an item from the queue") do |item|
                        if(item.to_i > 0)
                                options.queued_item = item.to_i 
                                @@log.info('stored queued_item '  << item.to_s)
                        end
                end

                opts.separator ""
                opts.separator ("Common options") << ':'

                # No argument. Show list of queued items.
                opts.on_tail(("-l"), ("--list"), ("List previously interrupted downloads")) do
                        options.list_only = true
                end

                # No argument. Retry download after interruption.
                opts.on_tail(("-c"), ("--continue"), ("Continue interrupted downloads")) do
                        options.continue = true
                        @log.debug('continue is true')
                end
                # No argument. Set the log-level to debug.
                opts.on_tail(("-p"), ("--protocol"), ("Be verbose")) do
                        $log_level = Logger::DEBUG
                        @log.level = $log_level
                        @log.debug('Will write out protocol-messages (level: debug).')
                end
                # No argument, shows at tail.  This will print an options summary.
                opts.on_tail(("-h"), ("--help"), ("Show this message") ) do
                        puts opts
                        exit true
                end

                opts.on_tail(("-v"), ("--version"), ("Show version and program information") ) do
                        puts "\t#{$GEMNAME}, version #{$version}"
                        puts "\tThis program is free software. Use, modify and distribute it\n\tunder the terms of the Gnu General Public License,\n\tversion 3 or later."
                        puts "\t©#{$YEARS} #{$AUTHORS.join(', ')}"
                        exit true
                end
        end

        op.parse!(args)
        options
end