
NConsoler - parsing console application arguments
Usually you have to spend a lot of time parsing console arguments in console applications. I found several systems on the Internet that simplify this task, but they seemed cumbersome to me, so it was decided to create a new system based on meta-information - NConsoler.
The task was to turn a certain set of arguments at the entrance to the call of a certain method. If something is wrong with the arguments, you should output an error. Also, meta-information can be used to compose simple help messages.
So, so that the system knows which method to use, it must be marked in some way. Attributes do a good job of this. I made an attribute
since all parameters must be passed to the method, for optional ones you must specify the default value.
For starters, that's enough. Now you need to run the method on execution. We will do this in the method
or with flag inversion:
Naturally, an error may creep into the description of Action methods. In many libraries, the source of the error is often not so obvious, so it is very important to display an adequate error message. Before starting the Action method, NConsoler checks the meta information, as well as the command line parameters and displays a detailed error message with a possible cause. We can say this development of the idea of “Design by contract” which allows you to work with the library without resorting to help.
You can find more information, download the library and its source code on the NConsoler website: nconsoler.csharpus.com
The task was to turn a certain set of arguments at the entrance to the call of a certain method. If something is wrong with the arguments, you should output an error. Also, meta-information can be used to compose simple help messages.
So, so that the system knows which method to use, it must be marked in some way. Attributes do a good job of this. I made an attribute
Action
:
Naturally, some arguments may be required, and some may not. This should also be indicated as metadata.[Action]
public static void Method(...)...
[Action]
public static void Method(
[Required] string name,
[Optional(true)] bool flag)...
since all parameters must be passed to the method, for optional ones you must specify the default value.
For starters, that's enough. Now you need to run the method on execution. We will do this in the method
Main
: we must pass
to the method a type that contains our method marked with an attribute , as well as arguments.
Here is the complete listing of the application: it
remains to run the application:public static void Main(params string[] args) {
Consolery.Run(typeof(Program), args);
}
Run
Action
using System;
using NConsoler;
public class Program {
public static void Main(params string[] args) {
Consolery.Run(typeof(Program), args);
}
[Action]
public static void Method(
[Required] string name,
[Optional(true)] bool flag) {
Console.WriteLine("name: {0}, flag: {1}", name, flag);
}
}
> Program.exe «Max»
name: Max, flag: to true
or with flag inversion:
> program.exe "Max" / -flag
name: Max, flag: false
Naturally, an error may creep into the description of Action methods. In many libraries, the source of the error is often not so obvious, so it is very important to display an adequate error message. Before starting the Action method, NConsoler checks the meta information, as well as the command line parameters and displays a detailed error message with a possible cause. We can say this development of the idea of “Design by contract” which allows you to work with the library without resorting to help.
You can find more information, download the library and its source code on the NConsoler website: nconsoler.csharpus.com