Module: Bovem::ParserMethods::General::ClassMethods
- Defined in:
- lib/bovem/parser.rb
Overview
Class methods
Instance Method Summary (collapse)
-
- (Hash|NilClass) find_command(arg, command, args, separator = ":")
Finds a command which corresponds to an argument.
-
- (Hash|NilClass) parse(command, args)
Parses a command/application.
-
- (String) smart_join(array, separator = ", ", last_separator = " and ", quote = "\"")
Joins an array using multiple separators.
Instance Method Details
- (Hash|NilClass) find_command(arg, command, args, separator = ":")
Finds a command which corresponds to an argument.
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.
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.
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 |