Module: Bovem::ParserMethods::General::ClassMethods

Defined in:
lib/bovem/parser.rb

Overview

Class methods

Instance Method Summary collapse

Instance Method Details

#find_command(arg, command, args: {}, separator: ":") ⇒ Hash|NilClass

Finds a command which corresponds to an argument.

Parameters:

  • arg (String)

    The string to match.

  • command (Command)

    The command to search subcommand in.

  • args (String) (defaults to: {})

    The complete list of arguments passed.

  • separator (String) (defaults to: ":")

    The separator for joined syntax commands.

Returns:

  • (Hash|NilClass)

    An hash with name and args keys if a valid subcommand is found, nil otherwise.



37
38
39
40
41
42
43
44
45
46
47
48
# 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) ⇒ Hash|NilClass

Parses a command/application.

Parameters:

  • command (Command)

    The command or application to parse.

  • args (Array)

    The arguments to parse.

Returns:

  • (Hash|NilClass)

    An hash with name (of a subcommand to execute) and args keys if a valid subcommand is found, nil otherwise.



55
56
57
# 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: "\"") ⇒ String

Joins an array using multiple separators.

Parameters:

  • array (Array)

    The array to join.

  • separator (String) (defaults to: ", ")

    The separator to use for all but last join.

  • last_separator (String) (defaults to: " and ")

    The separator to use for the last join.

  • quote (String) (defaults to: "\"")

    If not nil, elements are quoted with that element.

Returns:

  • (String)

    The joined array.



23
24
25
26
27
28
# 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