Module: Hanami::CLI::Parser Private
- Defined in:
- lib/hanami/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
Defined Under Namespace
Classes: Result
Class Method Summary collapse
-
.call(command, arguments, names) ⇒ Object
private
rubocop:disable Metrics/MethodLength, Metrics/AbcSize.
- .full_command_name(names) ⇒ Object private
-
.match_arguments(command_arguments, arguments) ⇒ Object
private
rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize.
-
.parse_required_params(command, arguments, names, parsed_options) ⇒ Object
private
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength.
Class Method Details
.call(command, arguments, names) ⇒ 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/MethodLength, Metrics/AbcSize
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/hanami/cli/parser.rb', line 15 def self.call(command, arguments, names) original_arguments = arguments.dup = {} OptionParser.new do |opts| command..each do |option| opts.on(*option.) do |value| [option.name.to_sym] = value end end opts.on_tail("-h", "--help") do return Result.help end end.parse!(arguments) = command.default_params.merge() parse_required_params(command, arguments, names, ) rescue ::OptionParser::ParseError Result.failure("Error: \"#{command.command_name}\" was called with arguments \"#{original_arguments.join(' ')}\"") end |
.full_command_name(names) ⇒ 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.
40 41 42 |
# File 'lib/hanami/cli/parser.rb', line 40 def self.full_command_name(names) ProgramName.call(names) 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/MethodLength rubocop:enable Metrics/AbcSize
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/hanami/cli/parser.rb', line 76 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..-1] break else result[cmd_arg.name] = arguments.at(index) end end result end |
.parse_required_params(command, arguments, names, 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 rubocop:disable Metrics/MethodLength
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/hanami/cli/parser.rb', line 49 def self.parse_required_params(command, arguments, names, ) 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? } unused_arguments = arguments.drop(command.required_arguments.length) unless all_required_params_satisfied parsed_required_params_values = parsed_required_params.values.compact usage = "\nUsage: \"#{full_command_name(names)} #{command.required_arguments.map(&:description_name).join(' ')}\"" if parsed_required_params_values.empty? # rubocop:disable Style/GuardClause return Result.failure("ERROR: \"#{full_command_name(names)}\" was called with no arguments#{usage}") else return Result.failure("ERROR: \"#{full_command_name(names)}\" was called with arguments #{parsed_required_params_values}#{usage}") end end parsed_params.reject! { |_key, value| value.nil? } = .merge(parsed_params) = .merge(args: unused_arguments) if unused_arguments.any? Result.success() end |