Class: Ncn::CliParser

Inherits:
Object
  • Object
show all
Defined in:
lib/ncn/cli_parser.rb

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCliParser

Returns a new instance of CliParser.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ncn/cli_parser.rb', line 17

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

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



3
4
5
# File 'lib/ncn/cli_parser.rb', line 3

def arguments
  @arguments
end

#commandObject (readonly)

Returns the value of attribute command.



3
4
5
# File 'lib/ncn/cli_parser.rb', line 3

def command
  @command
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/ncn/cli_parser.rb', line 3

def options
  @options
end