# Parsing console application arguments

In our console app, the parsing of application arguments is done like so:

using System.Linq;

namespace Generator
{
internal class Program
{
public static void Main(string[] args)
{
var param1 = args.SingleOrDefault(arg => arg.StartsWith("p1:"));
if (!string.IsNullOrEmpty(param1))
{
param1  = param1.Replace("p1:", "");
}
//...
}
}
}


It's supposed to be called like this:

Generator.exe p1:somevalue


Is there a better/simpler way to parse arguments?

-
Also, the FubuCore library has a pretty powerful and self-documenting command line args parser, that I briefly described here – w0lf Mar 24 '12 at 11:22

I'd recommend taking advantage of the excellent Mono.Options module. It's a single .cs file you can drop in to your solution and get full-featured parsing of GNU getopt-style command lines. (Things like -x -y -z, which is equivalent to -xyz, or -k value, or --long-opt, and so forth.)

-

With such an implementation you will have to repeat yourself for each param.

Alternative:

var parsedArgs = args
.Select(s => s.Split(new[] {':'}, 1))
.ToDictionary(s => s[0], s => s[1]);
string p1 = parsedArgs["p1"];

-
+1: I would have used '='. And check that arguments are prefixed with '--'. And what about where the value is the next argument not in the same argument? – Loki Astari Jan 28 '11 at 18:19
@Showbear very nice solution I like it. I'll wait for more suggestions though. – frennky Jan 28 '11 at 18:36
@Martin why would you prefix arguments with '--'? – frennky Jan 28 '11 at 18:38
@frennky: Its sort of a standard for command line arguments (--<longName> or -<shortName>). It separates flags (which modify behavior) from inputs/outputs. But re-reading your original question I was over generalizing and this is not what you want. – Loki Astari Jan 28 '11 at 20:15
@frennky - I agree with Martin, if you are writing an application which somebody (except you) will use then you should read about console argument standarts. As far as I know '--' is kind of *nix style, in windows it is more about slashes. – Snowbear Jan 28 '11 at 20:22

There's a related question on Stack Overflow. There, the consensus seems to be Mono.Options as already suggested here by josh3736.

-
Read further down the list of answers on that SO question and you'll see that ndesk.org/Options has 2x the upvotes of the accepted answer. – Handcraftsman Mar 4 '11 at 17:00
Actually, the answer you refer to says "I would strongly suggest using NDesk.Options (Documentation) and/or Mono.Options (same API, different namespace)." – Don Kirkby Mar 4 '11 at 18:00

I usually don't use complex command line arguments, so I use a very Simple Command Line Arguments Parser, but it can be used as a foundation for your own application specific parameter presenter.

-

You could use foreach for iterating through the agruments and then for your argument with index 1 you could use regular expression to retrieve parsed text after p1:

-
I was hoping to avoid using loops. As for Regex, I think it would be too much, the pattern is quite simple. – frennky Jan 28 '11 at 18:36

Please check solution from link. IT uses linq. It short and reuseble in my opinion. Provides extension method so you just do somethink like:

args.Process(
() => Console.WriteLine("Usage is switch1=value1,value2 switch2=value3"),
new CommandLine.Switch("switch1",
val => Console.WriteLine("switch 1 with value {0}",
string.Join(" ", val))),
new CommandLine.Switch("switch2",
val => Console.WriteLine("switch 2 with value {0}",
string.Join(" ", val)), "s1"));
}


for details please visit my blog

-

## protected by syb0rgMar 1 '14 at 23:47

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site.