Sha256: 204f598bffee058ebab6020d4449c05d08ab39575998d6eea61e1a77b1c1f72a

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

module Dirwatch
  class Options
    def self.from_args args
      require 'optparse'
      require 'ostruct'

      show_help = false
      options = OpenStruct.new
      options.directory = './'
      parser = OptionParser.new do |opts|
        opts.banner = "Usage: #{$0} [options] [directory]"

        opts.on('-f', '--file-match MATCH', 'The bash match for the file. Default: *.tex') do |match|
          options.file_match = match
        end

        opts.on('-i', '--interval INTERVAL', OptionParser::DecimalNumeric, 'The number of seconds to wait unitl the next check. Default: 1') do |interval|
          options.interval = interval
        end

        opts.on('-d', '--daemonize', 'Run the programm as a daemon') do
          options.daemonize = true
        end

        opts.on('-h', '--help', 'Show this help') do
          puts opts
          exit
        end
      end
      parser.parse! args

      unless args.empty?
        if args.size > 1
          puts 'Too many arguments'
          puts parser
          exit
        end
        options.directory = args.first
      end

      new options.to_h
    end

    attr_reader :directory, :file_match, :interval, :daemonize

    def initialize directory: nil, file_match: nil, interval: nil, daemonize: nil
      raise 'The directory is required' unless directory
      @directory = directory
      @file_match = file_match || '*.tex'
      @interval = interval || 1
      @daemonize = !!daemonize
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dirwatch-0.0.0 lib/dirwatch/options.rb