Is there any .net magic to get parameter values ​​by name in a console application?

I developed .net console applications using C # and always just determined which order parameters to insert, so args [0] is always the start date, and args [1] is always the end date, for example.

however, I would like to move on to using named parameters so that any combination of parameters can be sent in any order, for example, the typical "-sd" start date prefix.

I know that I could analyze the arguments of [], looking for “-”, and then read the name and look at the next position for the accompanying value, but before you do this, you need to find out if there is any method, this is pretty standard practice.

is there something like this out there that could do as such:

DateTime startDate = (DateTime)((ConsoleParameters)args[])["sd"]

I am using C # and .Net 4

+5
source share
4 answers

Nothing is embedded in the main structure.

Many people think that NDesk.Options is useful for these kinds of things. Check out this example (taken directly from the provided link):

string data = null;
bool help   = false;
int verbose = 0;

var p = new OptionSet () {
    { "file=",      v => data = v },
    { "v|verbose",  v => { ++verbose } },
    { "h|?|help",   v => help = v != null },
};
List<string> extra = p.Parse (args);
+5
source

Yes, the “magic” is that this is a common problem, and it has been adequately resolved. Therefore, I recommend using the already written library to handle parsing command line arguments.

CommandLineParser . , . , .

, , , , , . , , , .

+3
0

, . "-sd" - . "/sd". "sd=". .

, .NET Framework , "-sd".

, " " .

: , , , .

: @Sander Rijken, , : "-sd" .NET 4.0. , - .

0

All Articles