module Reply::Reader::Commands

Overview

Include this module to add the capability to parse and execute commands.

The methods starting with do_ will be treating as commands.

#run_commands(expression) will parse expression and execute command if the command match, elsewhere it shows an help message.

ex:

class MyReader < Reply::Reader
  include Commands

  def do_method(arg1, arg2 = nil)
    puts "executing command 'method' with arguments: #{arg1} #{arg2}"
  end
end

reader = MyReader.new
reader.read_loop do |expression|
  reader.run_commands(expression)
end

See /examples/command_relp.cr for more details.

Defined in:

commands.cr

Constructors

Macro Summary

Instance Method Summary

Constructor Detail

def self.new #

[View source]

Macro Detail

macro command_docs(help_doc = nil) #

Returns the command documentations defined by Help annotation, as Hash(String, {summary: String, details: String})


[View source]
macro command_names #

Returns the command names defined by do_method, as Array(String)


[View source]

Instance Method Detail

def auto_complete(name_filter : String, expression_before : String) : Tuple(String, Array(String)) #

Commands#auto_complete (this method) matches the first command to the commands defined by the do_methods, and hands off any subsequent completion requests to #auto_complete_arguments.


[View source]
def auto_complete_arguments(command_name : String, name_filter : String, expression_before : String) : Tuple(String, Array(String)) #

Override this method to provide name completion for arguments to a command.


[View source]
def do_help(command_name : String | Nil = nil) #

Override this method to customize help message.


[View source]
def parse_command(expression : String) : Tuple(String, Array(String)) #

Override this method to customize how command and arguments are parsed from expression.

ameba:disable Metrics/CyclomaticComplexity


[View source]
def run_commands(expression) #

Executes a do_method if expression starts with 'method'. Passes the rest of the expression as arguments to the do_method, separated by spaces ex: run_command("method 1 2 3") will run do_method("1", "2", "3")

Shows a help message if arguments mismatch.

Returns the result of executed do_method, if any.


[View source]
def run_commands_loop #

Prompts and #run_commands in loop.


[View source]
def unknown_command(command_name : String, arguments : Array(String)) #

Override this method to customize message on unknown command.


[View source]