Class: ArgParser

Inherits:
Object
  • Object
show all
Extended by:
Logging
Defined in:
lib/argparser.rb

Overview

require_relative 'constants'

Constant Summary collapse

@@log =
init_logger(STDOUT, Logger::DEBUG)

Class Method Summary collapse

Methods included from Logging

init_logger, log_level=, log_target=

Methods included from File_Checking

#file_check, file_check

Class Method Details

.parse(args) ⇒ Object

Returns a structure describing the options.


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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