module CLAide::ARGV::Parser

Public Class Methods

argument_type(argument) click to toggle source

@return [Symbol] Returns the type of an argument. The types can be

either: `:arg`, `:flag`, `:option`.

@param [String] argument

The argument to check.
# File lib/claide/argv.rb, line 278
def self.argument_type(argument)
  if argument.start_with?('--')
    if argument.include?('=')
      :option
    else
      :flag
    end
  else
    :arg
  end
end
parse(argv) click to toggle source

@return [Array<Array<Symbol, String, Array>>] A list of tuples for each

parameter, where the first entry is the `type` and the second
entry the actual parsed parameter.

@example

list = parse(['tea', '--no-milk', '--sweetner=honey'])
list # => [[:arg, "tea"],
           [:flag, ["milk", false]],
           [:option, ["sweetner", "honey"]]]
# File lib/claide/argv.rb, line 259
def self.parse(argv)
  entries = []
  copy = argv.map(&:to_s)
  double_dash = false
  while argument = copy.shift
    next if !double_dash && double_dash = (argument == '--')
    type = double_dash ? :arg : argument_type(argument)
    parsed_argument = parse_argument(type, argument)
    entries << [type, parsed_argument]
  end
  entries
end
parse_argument(type, argument) click to toggle source

@return [String, Array<String, String>] Returns the argument itself for

normal arguments (like commands) and a tuple with the key and
the value for options and flags.

@param [Symbol] type

The type of the argument.

@param [String] argument

The argument to check.
# File lib/claide/argv.rb, line 300
def self.parse_argument(type, argument)
  case type
  when :arg
    return argument
  when :flag
    return parse_flag(argument)
  when :option
    return argument[2..-1].split('=', 2)
  end
end
parse_flag(argument) click to toggle source

@return [String, Array<String, String>] Returns the parameter

describing a flag arguments.

@param [String] argument

The flag argument to check.
# File lib/claide/argv.rb, line 317
def self.parse_flag(argument)
  if argument.start_with?('--no-')
    key = argument[5..-1]
    value = false
  else
    key = argument[2..-1]
    value = true
  end
  [key, value]
end