Class: GLI::CommandFinder
- Inherits:
-
Object
- Object
- GLI::CommandFinder
- Defined in:
- lib/gli/command_finder.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
{ :default_command => nil, :autocomplete => true }
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #find_command(name) ⇒ Object
-
#initialize(commands, options = {}) ⇒ CommandFinder
constructor
A new instance of CommandFinder.
Constructor Details
#initialize(commands, options = {}) ⇒ CommandFinder
Returns a new instance of CommandFinder.
10 11 12 13 |
# File 'lib/gli/command_finder.rb', line 10 def initialize(commands, = {}) self. = DEFAULT_OPTIONS.merge() self.commands_with_aliases = (commands) end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
3 4 5 |
# File 'lib/gli/command_finder.rb', line 3 def @options end |
Instance Method Details
#find_command(name) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/gli/command_finder.rb', line 15 def find_command(name) name = String(name || [:default_command]).strip raise UnknownCommand.new("No command name given nor default available") if name == '' command_found = commands_with_aliases.fetch(name) do |command_to_match| if [:autocomplete] found_match = find_command_by_partial_name(commands_with_aliases, command_to_match) if found_match.kind_of? GLI::Command if ENV["GLI_DEBUG"] == 'true' $stderr.puts "Using '#{name}' as it's is short for #{found_match.name}." $stderr.puts "Set autocomplete false for any command you don't want matched like this" end elsif found_match.kind_of?(Array) && !found_match.empty? raise AmbiguousCommand.new("Ambiguous command '#{name}'. It matches #{found_match.sort.join(',')}") end found_match end end raise UnknownCommand.new("Unknown command '#{name}'") if Array(command_found).empty? command_found end |