Class: ArgParser

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

Class Method Summary collapse

Methods included from Logging

#init_logger, #log_level=, #log_target=

Class Method Details

.parse(args) ⇒ Object

Returns a structure describing the options.



35
36
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
# File 'lib/argparser.rb', line 35

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
	options.fast = false 
	options.line = 0 
	options.file = nil

	op = OptionParser.new do |opts|
		opts.banner = "Usage:\t" << "%s -s [textfile] <options>" %($0) <<"\n"

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

		opts.on('-' << "sFILE", '--' << "source FILE", "read this file") do |file|
			options.file = file
		end

		opts.on('-' << "f", '--' << "fast", "display at once entire lines") do |fast|
			options.fast = true
		end
		opts.on('-' << 'lLINE', '--' << "line LINE", "start reading file at line 'line'") do |line|
			options.line = line.to_i - 1
		end

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

		# 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#{APPNAME}, version #{VERSION} (called as #{$0})"
			puts "\t© 2015-#{YEAR} #{AUTHOR} #{AMAIL}>"
			exit true
		end
	end

	op.parse!(args)
	options
end