Module: Dry::CLI::Parser Private

Defined in:
lib/dry/cli/parser.rb

Overview

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

Parse command line arguments and options

Since:

  • 0.1.0

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Class Method Details

.call(command, arguments, prog_name) ⇒ Object

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.

Since:

  • 0.1.0



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/dry/cli/parser.rb', line 16

def self.call(command, arguments, prog_name)
  original_arguments = arguments.dup
  parsed_options = {}

  OptionParser.new do |opts|
    command.options.each do |option|
      opts.on(*option.parser_options) do |value|
        parsed_options[option.name.to_sym] = value
      end
    end

    opts.on_tail("-h", "--help") do
      return Result.help
    end
  end.parse!(arguments)

  parsed_options = command.default_params.merge(parsed_options)
  parse_required_params(command, arguments, prog_name, parsed_options)
rescue ::OptionParser::ParseError
  Result.failure("ERROR: \"#{prog_name}\" was called with arguments \"#{original_arguments.join(" ")}\"") # rubocop:disable Layout/LineLength
end

.match_arguments(command_arguments, arguments) ⇒ Object

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.

rubocop:enable Metrics/AbcSize, Metrics/PerceivedComplexity

Since:

  • 0.1.0



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/dry/cli/parser.rb', line 72

def self.match_arguments(command_arguments, arguments)
  result = {}

  command_arguments.each_with_index do |cmd_arg, index|
    if cmd_arg.array?
      result[cmd_arg.name] = arguments[index..]
      break
    else
      result[cmd_arg.name] = arguments.at(index)
    end
  end

  result
end

.parse_required_params(command, arguments, prog_name, parsed_options) ⇒ Object

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.

rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity

Since:

  • 0.1.0



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
# File 'lib/dry/cli/parser.rb', line 42

def self.parse_required_params(command, arguments, prog_name, parsed_options)
  parsed_params          = match_arguments(command.arguments, arguments)
  parsed_required_params = match_arguments(command.required_arguments, arguments)
  all_required_params_satisfied = command.required_arguments.all? { |param| !parsed_required_params[param.name].nil? } # rubocop:disable Layout/LineLength

  unused_arguments = arguments.drop(command.required_arguments.length)

  unless all_required_params_satisfied
    parsed_required_params_values = parsed_required_params.values.compact

    usage = "\nUsage: \"#{prog_name} #{command.required_arguments.map(&:description_name).join(" ")}" # rubocop:disable Layout/LineLength

    usage += " | #{prog_name} SUBCOMMAND" if command.subcommands.any?

    usage += '"'

    if parsed_required_params_values.empty?
      return Result.failure("ERROR: \"#{prog_name}\" was called with no arguments#{usage}")
    else
      return Result.failure("ERROR: \"#{prog_name}\" was called with arguments #{parsed_required_params_values}#{usage}") # rubocop:disable Layout/LineLength
    end
  end

  parsed_params.reject! { |_key, value| value.nil? }
  parsed_options = parsed_options.merge(parsed_params)
  parsed_options = parsed_options.merge(args: unused_arguments) if unused_arguments.any?
  Result.success(parsed_options)
end