Module: GitEvolution::OptionHandler
- Defined in:
- lib/git_evolution/option_handler.rb
Class Method Summary collapse
Class Method Details
.parse_options(args) ⇒ Object
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 |
# File 'lib/git_evolution/option_handler.rb', line 3 def self.(args) = OpenStruct.new( range: nil, since: nil, start_line: nil, end_line: nil, ) OptionParser.new do |opts| opts. = 'Usage: git_evolution [options] <file>' opts.version = VERSION opts.on '-r', '--range N:N', String, 'The specified range of lines to consider within the file (optional)' do |value| .range = value end opts.on '-s', '--since STRING', String, 'Consider the commits which are more recent than the specified time (optional)' do |value| .since = value end end.parse! [:file] = File.(args[0]) [:start_line], [:end_line] = parse_range([:range]) () end |
.parse_range(range) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/git_evolution/option_handler.rb', line 30 def self.parse_range(range) return if range.nil? regex_matches = range.match(/^(\d+):(\d+)/) raise 'The --range option was not in the valid format (N:N)' if regex_matches.nil? start_line = regex_matches[1].to_i end_line = regex_matches[2].to_i return start_line, end_line end |
.validate_options!(options) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/git_evolution/option_handler.rb', line 42 def self.() if .file.nil? raise 'Missing required file argument' elsif !File.exist?(.file) raise "File #{.file} does not exist" end if !.range.nil? raise 'Start line cannot be greater than the end line' if .start_line > .end_line file_length = File.new(.file).readlines.size raise "End line cannot be larger than the length of the file (#{file_length})" if .end_line > file_length end if !.since.nil? .since = Chronic.parse(.since) raise 'The since time could not be properly parsed' if .since.nil? end end |