-
Notifications
You must be signed in to change notification settings - Fork 0
Command basics
In this section there are information about how to create commands using the library. In partcular, the topics covered in this section are:
- Commands definition
-
Commandattribute -
ICommand<ParameterType>interface and itsMain(ParameterType)method -
EmptyDataModelstruct - Command example and how to run it in a command line interface application
A command is a specific and minimal action that can be reused in different type of projects and applications. It is very useful in the case you have different applications that work independently and you want to integrate them.
As the commands can be used in different project types, it is recommended to create them in a library project instead of putting it directly into the main one, and link the library to the main project.
For instance, if you want to create a console application that performs CRUD operations in a file, create a library project that contains these commands
- create command
- read command
- update command
- delete command
Then, create a console application, add the library project as reference and integrate it in your code as described in detail in the page Integration with console application
If you want to create a GUI application in your project, you can do it easily without changing any line of code in the library. You need only to reference the project library in your application and integrate it in your code as described in details in the page Integration with GUI application.
The same thing described for a GUI application can be used also for non-interactive applications (such as daemons, batch, scheduled tasks, services and so on)
If you need to create a command, define a new class that implements the ICommand<ParameterType> interface.
ParameterType is the object type where you have defined the parameters to use in your command (it could be a class or a struct).
As the parameter definition will be treated in the next section, in this section you are using only the EmptyDataModel struct as parameter type. EmptyDataModel is already included in the library and does not contain any parameter, so it's very useful for commands that don't need any arguments in order to be executed.
The ICommand<ParameterType> interface contains one method, Main(ParameterType). It is the entry point of your command and must return a string that should be generated by one of the CommandOutput static method. For more information regarding CommandOutput static methods, see message types handled by the library
Once the command has been created and all methods of the ICommand<ParameterType> interface have been implemented, it is needed to define a string that will be used to call this command. In a command line application, this string will be the verb, while in other applications this string is used to define an action.
This string should be passed as the first parameter in Command attribute. This attribute is mandatory and must be unique for each command.
You can also define a description for the command, and even if it is not mandatory, it is strongly recommended to define a description, as it will be used in the command documentation
In this example, you are creating a simple console application that prints an "Hello world" success message when you call the application with the verb hello. In particular, you are creating a solution that contains
- 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
In order to complete the example:
-
Clone the https://github.com/interappconnectorproject/interappconnector-samples repository and open the solution BasicCommand 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\BasicCommand\BasicCommand.CLI\bin\Debug\net6.0
-
Type the executable name with the extension (for instance BasicCommand.CLI.exe). You should see an output similar to the one below
------------------------------------------------------------------------------- BasicCommand.CLI 1.0.0. ------------------------------------------------------------------------------- Available actions: - hello A simple hello world command -
Type the executable name with the extension (for instance BasicCommand.CLI.exe) followed by the verb hello. You should see a text in green silmilar like the one below
[11/10/2023 23:50:34] SUCCESS (0) : Hello World
- 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