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
|
# File 'lib/shift_subtitle_cli.rb', line 17
def parse_options(arguments)
options = {}
mandatory_options = [ :operation, :seconds, :milliseconds, :input_file, :output_file ]
parser = OptionParser.new do |opts|
opts.banner = <<-BANNER.gsub(/^ /,'')
Shift Subtitle Ruby Challenge
Usage: #{File.basename($0)} --operation [add|sub] --time [seconds,milliseconds] input_file output_file
Options are:
BANNER
opts.separator ""
opts.on("-o", "--operation add|sub", [:add, :sub], "Add or Subtract time") do |operation|
options[:operation] = operation
end
opts.on("-t", "--time seconds,milliseconds", "Seconds AND milliseconds to add or subtract") do |time|
if (time =~ /\d+,\d+/)
options[:seconds], options[:milliseconds] = time.split(/,/).map!{ |t| t.to_i }
else
raise OptionParser::InvalidArgument, "should be seconds,milliseconds"
end
end
opts.on("-h", "--help", "Show this help message.") { puts opts; exit 0 }
opts.parse!(arguments)
options[:input_file], options[:output_file] = arguments[0], arguments[1]
if mandatory_options.find { |option| options[option].nil? }
$stderr.puts opts; exit 1
end
end
options
end
|