Class: Rubycli::ArgumentParser

Inherits:
Object
  • Object
show all
Includes:
TypeUtils
Defined in:
lib/rubycli/argument_parser.rb

Constant Summary collapse

OPTION_TOKEN_PATTERN =
/\A-{1,2}([a-zA-Z0-9_-]+)(?:=(.*))?\z/

Instance Method Summary collapse

Methods included from TypeUtils

analyze_placeholder, boolean_string?, boolean_type?, convert_boolean, default_placeholder_for, determine_requires_value, infer_types_from_placeholder, nil_type?, normalize_long_option, normalize_short_option, normalize_type_list, normalize_type_token, parse_list

Constructor Details

#initialize(environment:, documentation_registry:, json_coercer:, debug_logger:) ⇒ ArgumentParser

Returns a new instance of ArgumentParser.



16
17
18
19
20
21
22
# File 'lib/rubycli/argument_parser.rb', line 16

def initialize(environment:, documentation_registry:, json_coercer:, debug_logger:)
  @environment = environment
  @documentation_registry = documentation_registry
  @json_coercer = json_coercer
  @debug_logger = debug_logger
  @value_converter = Arguments::ValueConverter.new
end

Instance Method Details

#parse(args, method = nil) ⇒ Object



24
25
26
27
28
29
30
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rubycli/argument_parser.rb', line 24

def parse(args, method = nil)
  pos_args = []
  raw_pos_args = []
  kw_args = {}

  kw_param_names = extract_keyword_parameter_names(method)
  required_kw_param_names = extract_required_keyword_parameter_names(method)
  debug_log "Available keyword parameters: #{kw_param_names.inspect}"

   = method ? @documentation_registry.(method) : { options: [], returns: [], summary: nil }
  option_defs = [:options] || []
  cli_aliases = build_cli_alias_map(option_defs)
  option_lookup = build_option_lookup(option_defs)
  type_converters = build_type_converter_map(option_defs)
  known_option_names = (
    kw_param_names + cli_aliases.keys + option_lookup.keys.map(&:to_s)
  ).uniq

  stream = Arguments::TokenStream.new(args)

  until stream.finished?
    token = stream.current

    if token == '--'
      stream.advance
      rest_tokens = stream.consume_remaining
      raw_pos_args.concat(rest_tokens)
      pos_args.concat(rest_tokens.map { |value| convert_arg(value) })
      break
    elsif option_token?(token)
      stream.advance
      process_option_token(
        token,
        stream,
        kw_param_names,
        kw_args,
        cli_aliases,
        option_lookup,
        type_converters,
        required_kw_param_names,
        known_option_names
      )
    elsif assignment_token_for_method?(token, method, kw_param_names)
      stream.advance
      process_assignment_token(token, kw_args, option_lookup, type_converters)
    else
      raw_pos_args << token
      pos_args << convert_arg(token)
      stream.advance
    end
  end

  pos_args = convert_positional_arguments(pos_args, raw_pos_args, method, )
  debug_log "Final parsed - pos_args: #{pos_args.inspect}, kw_args: #{kw_args.inspect}"
  [pos_args, kw_args]
end

#validate_inputs(method_obj, positional_args, keyword_args) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/rubycli/argument_parser.rb', line 81

def validate_inputs(method_obj, positional_args, keyword_args)
  return unless method_obj

   = @documentation_registry.(method_obj)
  validate_positional_arguments(method_obj, , positional_args)
  validate_keyword_arguments(, keyword_args)
end