Skip to content

Command output events

Giuseppe Cramarossa edited this page Jan 14, 2024 · 4 revisions

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

Events related to command output messages and their respective parameters

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 CommandExecutionMessageType enumeration value, that could be a value between
    • CommandExecutionMessageType.Success for success messages
    • CommandExecutionMessageType.Info for informational messages
    • CommandExecutionMessageType.Warning for warning messages
    • CommandExecutionMessageType.Failed for 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

Example

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:

  1. Clone the https://github.com/interappconnectorproject/interappconnector-samples repository and open the solution BasicCommandOutputEvents available in basic folder

  2. Build the solution (you can leave the debug mode if you want)

  3. 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
  4. 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
  5. 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
  6. Create the file testfile.txt in the same directory of the executable

  7. Write a string in the file created at step 8, for example "This a text inside the file"

  8. 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

Clone this wiki locally