Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
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
up vote 21 down vote accepted

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.)

share|improve this answer

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


var parsedArgs = args
    .Select(s => s.Split(new[] {':'}, 1))
    .ToDictionary(s => s[0], s => s[1]);
string p1 = parsedArgs["p1"];
share|improve this answer
+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.

share|improve this answer
Read further down the list of answers on that SO question and you'll see that 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.

share|improve this answer

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:

share|improve this answer
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:

           () => 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

share|improve this answer

protected by syb0rg Mar 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.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.