All along, when we want our console programs to support command-line startup, we often need to write a lot of code to implement this seemingly simple feature. While there are some libraries that can simplify some operations, the whole process is still a rather boring and tedious one.
Today, I will introduce a new command line library here:, Through it, we can get command line support without any extra coding, which can greatly reduce the time programmers spend on providing command line APIs (CLIs), improve the experience of CLI program users, and allow developers to focus on writing applications.
Currently, this library is still a preview version. If you want to experience it, you can use the following library:. First demonstrate its functionality with a simple example.
static void Main(string input, string output) { ($"Input: {input}, Output: {output}"); }
We do not want to use this library explicitly here. We just need to change the parameters of the Main function to the type we need to use, and the program will automatically implement command line support. We can even use -help to view the program's command line configuration method
--help
Usage:
ConsoleApp1 [options]
Options:
--input <INPUT> input
--output <OUTPUT> output
--version Display version information
It can be seen that it can automatically parse the command line format according to the parameters of the Main function and generate help documents.
Next, let's take a look at the use of the command line:
ConsoleApp1 --input ii --output out
Input: ii, Output: out
Perfectly perform command line parsing, it can also read xml comments to implement more complex instructions.
/// <summary> /// Converts an image file from one format to another. /// </summary> /// <param name="input">The path to the image file that is to be converted.</param> /// <param name="output">The name of the output from the conversion.</param> /// <param name="xCropSize">The x dimension size to crop the picture. The default is 0 indicating no cropping is required.</param> /// <param name="yCropSize">The x dimension size to crop the picture. The default is 0 indicating no cropping is required.</param> static void Main(string input, string output, int xCropSize = 0, int yCropSize = 0) { }
The generated help output effect is as follows:
ConsoleApp1:
Converts an image file from one format to another.
Usage:
ConsoleApp1 [options]
Options:
--input <INPUT> The path to the image file that is to be converted.
--output <OUTPUT> The name of the output from the conversion.
--x-crop-size <X-CROP-SIZE> The x dimension size to crop the picture. The default is 0 indicating no cropping is required.
--y-crop-size <Y-CROP-SIZE> The x dimension size to crop the picture. The default is 0 indicating no cropping is required.
--version Display version information
Compared with the traditional command line library, the advantages of this library are very obvious. We can obtain support from command line programs without writing almost any code. For complex command-line programs, the method here may not meet the needs. Although it also supports writing complex command line support programs like traditional command line libraries, this is not within the scope of this article. Interested friends can take a look at the content of the reference article.
Reference article:
Use the Analyze Command Line
command-line-api
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.