Sha256: 9efaf000d6ba67edb49ffaaddf8ae4af506ceca84bc7e59347120d5f5a483024

Contents?: true

Size: 1.69 KB

Versions: 2

Compression:

Stored size: 1.69 KB

Contents

require 'optparse'

module Subshift
  class Options
    attr_reader :source, :delay, :destination

    def initialize(argv)
      parse(argv)

      self.source, self.delay = argv

      unless destination
        self.destination = source
      end
    end

  private

    def source=(source)
      unless source
        puts option_parser.help
        exit
      end

      @source = normalize_filename(source)

      unless File.exist? @source
        puts "Error: The file #{@source} doesn't exist"
        exit
      end
    end

    def delay=(delay)
      unless delay
        puts option_parser.help
        exit
      end

      @delay = delay.to_f
    end

    def destination=(destination)
      @destination = normalize_filename(destination)
    end

    def parse(argv)
      begin
        option_parser.parse(argv)
      rescue OptionParser::InvalidOption => e
        if /[[:digit:]]/ === e.args[0]
          option_parser.parse(argv - e.args)
        else
          raise OptionParser::InvalidOption.new(*e.args)
        end
      end
    end

    def option_parser
      @option_parser ||= OptionParser.new do |opts|
        executable_name = File.basename($PROGRAM_NAME, '.rb')
        opts.banner = "A simple yet robust SRT Resync Tool

        Usage: #{executable_name} SOURCE DELAY [options]"

        opts.separator 'Options:'

        opts.on '-d DESTINATION', '--destination DESTINATION', 'Destination file' do |destination|
          send :destination=, destination
        end

        opts.on_tail '-h', '--help', 'Show this message' do
          puts opts
          exit
        end
      end
    end

    def normalize_filename(filename)
      File.basename(filename, '.*') + '.srt'
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
subshift-0.0.2 lib/subshift/options.rb
subshift-0.0.1.pre lib/subshift/options.rb