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 29 30 |
# 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! raise(FileMissingError, 'Missing required file argument') if args.empty? [:file] = File.(args[0]) [:start_line], [:end_line] = parse_range([:range]) () end |
.parse_range(range) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/git_evolution/option_handler.rb', line 32 def self.parse_range(range) return if range.nil? regex_matches = range.match(/^(\d+):(\d+)/) raise(InvalidRangeFormatError, '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
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/git_evolution/option_handler.rb', line 44 def self.() if .file.nil? raise(FileMissingError, 'Missing required file argument') elsif !File.exist?(.file) raise(FileDoesNotExistError, "File #{options.file} does not exist") end if !.range.nil? raise(RangeOutOfBoundsError, 'Start line cannot be greater than the end line') if .start_line > .end_line file_length = File.new(.file).readlines.size raise(RangeOutOfBoundsError, "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(TimeParseError, 'The since time could not be properly parsed') if .since.nil? end end |