Class: TTY::Option::Parser::Arguments

Inherits:
Object
  • Object
show all
Includes:
ParamTypes
Defined in:
lib/tty/option/parser/arguments.rb

Constant Summary

Constants included from ParamTypes

ParamTypes::ARGUMENT_PARAMETER, ParamTypes::ENV_VAR_PARAMETER, ParamTypes::KEYWORD_PARAMETER, ParamTypes::OPTION_PARAMETER

Instance Method Summary collapse

Methods included from ParamTypes

#argument?, #env_var?, #keyword?, #option?

Constructor Details

#initialize(arguments, check_invalid_params: true, raise_on_parse_error: false) ⇒ Arguments

Create a command line arguments parser

Parameters:

  • arguments (Array<Argument>)

    the list of arguments

  • config (Hash)

    the configuration settings



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tty/option/parser/arguments.rb', line 22

def initialize(arguments, check_invalid_params: true,
               raise_on_parse_error: false)
  @arguments = arguments
  @error_aggregator =
    ErrorAggregator.new(raise_on_parse_error: raise_on_parse_error)
  @required_check = RequiredCheck.new(@error_aggregator)
  @pipeline = Pipeline.new(@error_aggregator)
  @parsed = {}
  @remaining = []

  @defaults = {}
  @arguments.each do |arg|
    if arg.default?
      case arg.default
      when Proc
        @defaults[arg.key] = arg.default.()
      else
        @defaults[arg.key] = arg.default
      end
    elsif arg.required?
      @required_check << arg
    end
  end
end

Instance Method Details

#parse(argv) ⇒ Array<Hash, Array, Hash>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Read positional arguments from the command line

Parameters:

  • argv (Array<String>)

Returns:

  • (Array<Hash, Array, Hash>)

    a list of parsed and unparsed arguments and errors



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/tty/option/parser/arguments.rb', line 55

def parse(argv)
  @argv = argv.dup

  @arguments.each do |arg|
    values = next_argument(arg)
    @required_check.delete(arg) unless values.empty?

    assign_argument(arg, values)
  end

  while (val = @argv.shift)
    @remaining << val
  end

  @required_check.()

  [@parsed, @remaining, @error_aggregator.errors]
end