-
Notifications
You must be signed in to change notification settings - Fork 0
Command output events
In Message types and outputs page it is described how to return different message types from an application using this library. It is described also the format of each message and how use them.
InterAppConnector provides also some events related to outputs that are raised when a command output is invoked.
These events are very useful if you want to customize user output or you want to add some action when a message output is generated
In this page you will find information about these events and in particular details regarding:
- Events related to command outputs (
ErrorMessageEmitted,WarningMessageEmitted,InfoMessageEmitted,SuccessMessageEmitted) - Parameters used by the events
- An example that shows how to use them
In Message types and outputs page you have seen that there are different message types
- Error messages
- Informational messages
- Warning messages
- Success messages
These messages are generated respectively by these methods
CommandOutput.Error<T>()CommandOutput.Info<T>()CommandOutput.Warning<T>()CommandOutput.Ok<T>()
You can customize the behaviour of these methods by using the events associated to each message type (ErrorMessageEmitted, WarningMessageEmitted, InfoMessageEmitted, SuccessMessageEmitted). The table below describes the association between the message types, commands and events
| Message Type | Method | Associated event |
|---|---|---|
| Informational message | CommandOutput.Info<T>() |
InfoMessageEmitted |
| Warning message | CommandOutput.Warning<T>() |
WarningMessageEmitted |
| Success message | CommandOutput.Ok<T>() |
SuccessMessageEmitted |
| Error message | CommandOutput.Error<T>() |
ErrorMessageEmitted |
You don't have to use all events if you need to customize only a specific message type
These events have the same parameters. They are:
- the
CommandExecutionMessageTypeenumeration value, that could be a value between-
CommandExecutionMessageType.Successfor success messages -
CommandExecutionMessageType.Infofor informational messages -
CommandExecutionMessageType.Warningfor warning messages -
CommandExecutionMessageType.Failedfor error messages
-
- the exit code number
- the message object
When you use the events, the default behaviour of the commands are replaced with your custom one, so for example if you are creating a console application and you want to write messages to the console, you will have to manage this task by yourself.
Since you have to write cutom codes in the events, you may need to manage two cases:
- the command is executed in batch or non interactive mode
- the command is executed in interactive mode or in a command line interface
If you want to know in which mode the command is executed, InterAppConnector provides a static property, InterAppCommunication.CommandExecutionType that is set in this way:
- if the value is
CommandExecutionType.Interactive, the command is executed in interactive mode - if the value is
CommandExecutionType.Batch, the command is executed in batch mode
The library set this value automatically according to the method used in the code for the command execution. If you use ExecuteAsInteractiveCLI(), the value is set automatically to CommandExecutionType.Interactive. If you use ExecuteAsBatch(), the value is set automatically to CommandExecutionType.Batch.
This property is read-only, so you cannot change the value
In this example, you are creating a simple file reader console application that reads the content of the file if exists, otherwise returns a custom error message
For this example you are creating:
- A project library that contains the commands
- A console application that will be used to call the command via cli
- A web application
- A gui that uses MAUI
For this example it is assumed that you have learned what is a command and how to implement it
Follow this steps to complete this example:
-
Clone the https://github.com/interappconnectorproject/interappconnector-samples repository and open the solution BasicCommandOutputEvents available in basic folder
-
Build the solution (you can leave the debug mode if you want)
-
Open the Command Prompt and go to the location of the executable. For instance, if you have placed your solution in C:\SampleProjects you can go to the executable by typing the above command
cd C:\SampleProjects\BasicCommandOutputEvents\BasicCommandOutputEvents.CLI\bin\Debug\net6.0
-
Type the executable name with the extension (for instance BasicCommandOutputEvents.CLI.exe). You should see an output similar to the one below
------------------------------------------------------------------------------ BasicCommandOutputEvents.CLI 1.0.0.0 ------------------------------------------------------------------------------ Available actions: - read Read the content of the file -
Type the executable name with the extension (for instance ReadCommand.CLI.exe) followed by the verb read. You should see an output similar to the one below
[11/10/2023 23:50:34] INFO (2) : Checking if the file C:\SampleProjects\BasicCommandOutputEvents\BasicCommandOutputEvents.CLI\bin\Debug\net6.0\testfile.txt exists This is a custom error that replaces the standard one
-
Create the file testfile.txt in the same directory of the executable
-
Write a string in the file created at step 8, for example "This a text inside the file"
-
Retype the ommand describe at step 7. You should see an output similar to the one below
[11/10/2023 23:50:34] INFO (2) : Checking if the file C:\SampleProjects\BasicCommandOutputEvents\BasicCommandOutputEvents.CLI\bin\Debug\net6.0\testfile.txt exists [11/10/2023 23:50:34] INFO (2) : The file C:\SampleProjects\BasicCommandOutputEvents\BasicCommandOutputEvents.CLI\bin\Debug\net6.0\testfile.txt exists. Reading its content [11/10/2023 23:50:34] SUCCESS (0) : This a text inside the file
- Getting started
- Create Commands
- Manage arguments
- Argument basics
- Shared arguments between different commands
- Argument aliases
- Argument description
- Exclude an argument from argument list
- Use enumerations as argument
- Use objects and structs as argument type
- Validate argument value before command execution
- Customize the command example in command help
- Define rules