CRecordCliAddHandler
The message adds a handler for one or more console commands. When the user enters these commands, a message containing the input is sent to the registered handler. In response, the handler sends back a text that is displayed on the console.
This message is part of the console API.
It is a nanoservice in the SYSTEM namespace.
{
"id": "3eaa3a56-5a84-4f11-b2fc-62da19141013",
"name": "CLI_ADD_HANDLER",
"isService": "true",
"namespaces": "SYSTEM",
"description": "Add a Command Handler.",
"slots": [
{
"key": "1",
"name": "COMMAND",
"direction": "REQUEST",
"mandatory": "true",
"type": "STRING_ARRAY",
"description": "The commands."
},
{
"key": "2",
"name": "HELP",
"direction": "REQUEST",
"mandatory": "true",
"type": "STRING_ARRAY",
"description": "The help texts."
}
]
}
Arguments
-
COMMANDis the command for which a handler should be registered. Multiple commands can be registered simultaneously; therefore, the argument is a string array. -
HELPis a help text that is displayed for the command when "?" or "help" is entered on the command line. Multiple commands can be registered simultaneously; therefore, the argument is a string array.
The handler itself is identified by the sender address of the message.
Usage
The following method registers a command handler.
private void registerCliQuit() throws CException { final CEnvelope env = CEnvelope.forLocalNanoService(CWellKnownNID.SYSTEM); final CRecord record = CRecordCliAddHandler.create(); CRecordCliAddHandler.setCommand(record, new String[]{ QUIT, EXIT }); CRecordCliAddHandler.setHelp(record, new String[]{ HELP_QUIT, HELP_QUIT }); sendNotification(env, record); }
Subsequently, your target will receive messages of type CRecordCliHandle when the user enters one of the registered commands.
Registration of the message handler for CRecordCliHandle:
addMessageHandler(CRecordCliHandle.ID, this::asyncCliHandle);
Example implementation of the handler:
private boolean asyncCliHandle(@NotNull final CEnvelope aEnvelope, @NotNull final CRecord aRecord) throws CException { if (aEnvelope.isAnswer()) { return false; } else { final String command = CRecordCliHandle.getCommand(aRecord, ""); if (QUIT.equals(command) || EXIT.equals(command)) { final String[] args = CRecordCliHandle.getArguments(aRecord, null); if (args != null && args.length >= 1 && args[0] != null) { int exitCode = CUtilInteger.fromObject(args[0], 0); } shutDown(exitCode); } aEnvelope.setResultSuccess(); return true; } }