module Bovem::ParserMethods::General::ClassMethods

Class methods

Public Instance Methods

find_command(arg, command, args: {}, separator: ":") click to toggle source

Finds a command which corresponds to an argument.

@param arg [String] The string to match. @param command [Command] The command to search subcommand in. @param args [String] The complete list of arguments passed. @param separator [String] The separator for joined syntax commands. @return [Hash|NilClass] An hash with `name` and `args` keys if a valid subcommand is found, `nil` otherwise.

# File lib/bovem/parser.rb, line 37
def find_command(arg, command, args: {}, separator: ":")
  return nil unless command.commands.present?

  arg, args = adjust_command(arg, args, separator)

  matching = match_subcommands(arg, command)
  if matching.length == 1 # Found a command
    {name: matching[0], args: args}
  elsif matching.length > 1 # Ambiguous match
    raise Bovem::Errors::Error.new(command, :ambiguous_command, command.i18n.ambigous_command(arg, format_alternatives(matching, command)))
  end
end
parse(command, args) click to toggle source

Parses a command/application.

@param command [Command] The command or application to parse. @param args [Array] The arguments to parse. @return [Hash|NilClass] An hash with `name` (of a subcommand to execute) and `args` keys if a valid subcommand is found, `nil` otherwise.

# File lib/bovem/parser.rb, line 55
def parse(command, args)
  Bovem::Parser.new.parse(command, args)
end
smart_join(array, separator: ", ", last_separator: " and ", quote: "\"") click to toggle source

Joins an array using multiple separators.

@param array [Array] The array to join. @param separator [String] The separator to use for all but last join. @param last_separator [String] The separator to use for the last join. @param quote [String] If not nil, elements are quoted with that element. @return [String] The joined array.

# File lib/bovem/parser.rb, line 23
def smart_join(array, separator: ", ", last_separator: " and ", quote: "\"")
  separator = separator.ensure_string
  last_separator = last_separator.ensure_string
  array = array.ensure_array { |a| quote.present? ? "#{quote}#{a}#{quote}" : a.ensure_string }
  perform_smart_join(array, last_separator, separator)
end