Sha256: 656788b8d0bc85f59b853af8b5eafb058ca7eb9468da9144e67956d24a4f5563

Contents?: true

Size: 858 Bytes

Versions: 1

Compression:

Stored size: 858 Bytes

Contents

module Ncn
  class CliParser
    attr_reader :options, :command, :arguments

    COMMON_OPTIONS = [
      ["--help", "-h", GetoptLong::NO_ARGUMENT],
      ["--tags", "-t", GetoptLong::REQUIRED_ARGUMENT],
      ["--path", GetoptLong::REQUIRED_ARGUMENT],
      ["--number", "-n", GetoptLong::REQUIRED_ARGUMENT]
    ].freeze

    NORMALIZERS = {
      "--tags" => ->(value) { value.to_s.split(",").map(&:strip).uniq(&:downcase) },
      "--number" => ->(value) { value.match?(/^\d+$/) ? Integer(value) : nil }
    }.freeze

    def initialize
      options_parser = GetoptLong.new(*COMMON_OPTIONS)

      @options = {}.tap do |result|
        options_parser.each do |option, argument|
          result[option] = NORMALIZERS.key?(option) ? NORMALIZERS[option].call(argument) : argument
        end
      end

      @command, *@arguments = ARGV
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ncn-0.1.0 lib/ncn/cli_parser.rb