RCommand is a generic way for ruby scripts to present a full-featured command interface to users, complete with command history and tab completion.
Example
require 'rcommand'
command_line = RCommand.new(STDIN, STDOUT)
command_line.for_prompt do
command_line.io_write.print("#{command_line.history.size}:> ")
end
# Default events shown here in full, for demonstration purposes. Omitting
# them will give the same behavior.
command_line.on_single_tab do |partial|
matches = RCommand.matches(partial, command_line.history)
common_prefix = RCommand.common_prefix(matches)
common_prefix.nil? ? partial : common_prefix
end
command_line.on_double_tab do |partial|
matches = RCommand.matches(partial, command_line.history)
if matches.size > 1
command_line.io_write.puts()
for match in matches.uniq
command_line.io_write.puts(match)
end
command_line.prompt()
command_line.io_write.print(partial)
end
partial
end
command_line.on_key_up do |partial|
command_line.prev()
end
command_line.on_key_down do |partial|
command_line.next()
end
loop do
command_line.prompt()
result = command_line.gets()
command_line.io_write.puts("Example command: #{result}")
end