Sha256: 70237ed2d1b3de42fabad392e03c2ee5ecd7ff5053748e641f7e24bfa832eda9

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

module Calrom
  class CLI
    def self.call(argv)
      begin
        config_files = OptionParser.call(argv).configs

        config = OptionParser.call(
          rc_options(config_files.empty? ? nil : config_files) +
          argv
        )

        calendar = config.calendar
      rescue ::OptionParser::ParseError, InputError => e
        STDERR.puts e.message
        exit 1
      end

      I18n.locale = config.locale

      config.formatter.call calendar, config.date_range
    end

    private

    # options loaded from configuration files
    def self.rc_options(paths = nil)
      return [] if paths == ['']

      paths ||=
        ['/etc/calromrc', '~/.calromrc']
          .collect {|f| File.expand_path f }
          .select {|f| File.file? f }

      paths.collect do |f|
        begin
          content = File.read(f)
        rescue Errno::ENOENT
          raise InputError.new("Configuration file \"#{f}\" not found")
        end

        options = RcParser.call content

        begin
          OptionParser.call(options)
        rescue ::OptionParser::ParseError => e
          raise InputError.new("Error loading '#{f}': #{e.message}")
        end

        options
      end.flatten
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
calrom-0.3.0 lib/calrom/cli.rb