-
Notifications
You must be signed in to change notification settings - Fork 2
/
template.rb
executable file
·68 lines (56 loc) · 1.55 KB
/
template.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
#!/usr/bin/env ruby
# Author: Anders Janmyr
require 'rubygems'
require 'optparse'
require 'ostruct'
# This is the name of the script that is called
PROGRAM_NAME = $0
PROGRAM_VERSION = 1.0
option_parser = OptionParser.new do |opts|
opts.banner = "#{PROGRAM_NAME} [options] file..."
opts.separator ""
opts.separator "Options are ..."
# Add the command on_tail, to make it appear as the last option in the list.
opts.on_tail("-h", "--help", "-H", "Display this help message.") do
puts opts
exit
end
default_options.each { |args| opts.on(*args) }
program_options.each { |args| opts.on(*args) }
end
begin
# Parse the options and remove them from the ARGV array
option_parser.parse!
rescue OptionParser::ParseError => error
puts error.message
puts option_parser
exit
end
# Create an OpenStruct to save the options.
def options
@options ||= OpenStruct.new
end
def default_options
[
# The values of the array are,
# [long_option, short_option and parameter, description, code to execute]
['--verbose', '-v', "Log to standard output.",
lambda { |value| options.verbose = true }
],
['--version', '-V', "Display the program version.",
lambda { |value|
puts "#{program_name}, version #{PROGRAM_VERSION}"
exit
}
]
]
end
# This is the options of the program, see OptionParser
# http://ruby-doc.org/stdlib/libdoc/optparse/rdoc/classes/OptionParser.html
def program_options
[]
end
# Only the non options (the filenames) are left in ARGV
ARGV.each do|f|
puts "Parsing file #{f}..." if options.verbose
end