Class: TTY::Option::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/option/parser.rb,
lib/tty/option/parser/options.rb,
lib/tty/option/parser/keywords.rb,
lib/tty/option/parser/arguments.rb,
lib/tty/option/parser/arity_check.rb,
lib/tty/option/parser/param_types.rb,
lib/tty/option/parser/environments.rb,
lib/tty/option/parser/required_check.rb

Defined Under Namespace

Modules: ParamTypes Classes: Arguments, ArityCheck, Environments, Keywords, Options, RequiredCheck

Constant Summary collapse

PARAMETER_PARSERS =
{
  options: TTY::Option::Parser::Options,
  keywords: TTY::Option::Parser::Keywords,
  arguments: TTY::Option::Parser::Arguments,
  environments: TTY::Option::Parser::Environments
}.freeze
ARGUMENT_SEPARATOR =
/^-{2,}$/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parameters, **config) ⇒ Parser

Returns a new instance of Parser.



26
27
28
29
# File 'lib/tty/option/parser.rb', line 26

def initialize(parameters, **config)
  @parameters = parameters
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



24
25
26
# File 'lib/tty/option/parser.rb', line 24

def config
  @config
end

#parametersObject (readonly)

Returns the value of attribute parameters.



22
23
24
# File 'lib/tty/option/parser.rb', line 22

def parameters
  @parameters
end

Instance Method Details

#parse(argv, env) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tty/option/parser.rb', line 31

def parse(argv, env)
  argv = argv.dup
  params = {}
  errors = []
  ignored = []

  # split argv into processable args and leftovers
  stop_index = argv.index { |arg| arg.match(ARGUMENT_SEPARATOR) }

  if stop_index
    ignored = argv.slice!(stop_index..-1)
    ignored.shift
  end

  PARAMETER_PARSERS.each do |name, parser_type|
    parser = parser_type.new(parameters.send(name), **config)
    if name == :environments
      parsed, argv, err = parser.parse(argv, env)
    else
      parsed, argv, err = parser.parse(argv)
    end
    params.merge!(parsed)
    errors.concat(err)
  end

  argv += ignored unless ignored.empty?

  [params, argv, errors]
end