|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionUsually, parsing arguments in console applications takes a lot of time. You can find a few solutions in the Internet that make it easier to implement this task, however, they are not very simple and flexible. Therefore, I decided to design a new system based on metadata and call it NConsoler. Using the codeThe main idea is to find a way to transform a set of arguments to call a particular method. In case the arguments are wrong, the application must return a human understandable error message, with error details, to provide user friendly behavior for the application. Also, it is easy to provide help information using the metainfo. Well, to get the required method with a set of arguments, the method should be marked. For this functionality, we could use attributes that ideally suit this job. I implemented an [Action]
public static void Method(...)...
Also, methods could have required and optional arguments to add flexibility in the development process. This could be realized by including attributes as well as using the [Action]
public static void Method(
[Required] string name,
[Optional(true)] bool flag)...
As far as we know, The last thing to do before running the application is to design a public static void Main(params string[] args) {
Consolery.Run(typeof(Program), args);
}
The type that has a method marked as The complete source code is available here: 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);
}
}
Let’s run our application: > program.exe Max /flag name: Max, flag: true And now, with the inversed flag: > program.exe Max /-flag name: Max, flag: false Certainly, in the definition of the More information can be found on the NConsoler site.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||