lib/wgif/argument_parser.rb in wgif-0.5.1 vs lib/wgif/argument_parser.rb in wgif-0.5.2

- old
+ new

@@ -1,21 +1,19 @@ require 'optparse' -require 'wgif/exceptions' +require 'wgif/validator' module WGif class ArgumentParser - URL = %r{\Ahttps?://.*\z} - TIMESTAMP = /\A\d{1,2}(?::\d{2})+(?:\.\d*)?\z/ + DEFAULTS = { + trim_from: '00:00:00', + duration: 1.0, + dimensions: '480' + } def initialize @options = {} - @defaults = { - trim_from: '00:00:00', - duration: 1.0, - dimensions: '480' - } @parser = OptionParser.new do |opts| opts.on('-f N', '--frames N', 'Number of frames in the final gif. (Default 20)') { |n| @options[:frames] = n.to_i @@ -59,31 +57,29 @@ end end def parse(args) options = parse_args(args) - validate_args(options) + validate(options) options end + def validate(args) + WGif::Validator.new(args).validate + end + def argument_summary @parser.summarize end def parse_args(args) - options = @defaults.merge(parse_options args) + options = DEFAULTS.merge(parse_options args) options.merge(url: args[0], output: args[1]) end def parse_options(args) @parser.parse! args @options - end - - def validate_args(args) - fail WGif::InvalidUrlException unless args[:url] =~ URL - fail WGif::InvalidTimestampException unless args[:trim_from] =~ TIMESTAMP - fail WGif::MissingOutputFileException unless args[:output] end def print_help puts 'Usage: wgif [YouTube URL] [output file] [options]', "\n" puts argument_summary, "\n"