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

Defined in:
lib/bovem/parser.rb

Overview

Class methods

Instance Method Summary (collapse)

Instance Method Details

- (Hash|NilClass) find_command(arg, command, args, separator = ":")

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)

    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
49
50
51
52
# File 'lib/bovem/parser.rb', line 37

def find_command(arg, command, args, separator = ":")
  args = args.ensure_array.dup

  if command.commands.present? then
    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, Bovem::Parser.smart_join(matching)))
    end
  else
    nil
  end
end

- (Hash|NilClass) parse(command, args)

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.



59
60
61
# File 'lib/bovem/parser.rb', line 59

def parse(command, args)
  Bovem::Parser.new.parse(command, args)
end

- (String) smart_join(array, separator = ", ", last_separator = " and ", quote = "\"")

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 }
  array.length < 2 ? (array[0] || "") : (array[0, array.length - 1].join(separator) + last_separator + array[-1])
end