Yet another command line parsing system - Jon Galloway

Yet another command line parsing system

First Mike has linked to a command line parser library for .NET called Param.NET. Then Roy proposeda better command line parsing class which is attribute based. So, now's the time to mention a command line parameter class that worked well for me recently.

I used another arguments parser from Code Project, "C#/.NET Command Line Arguments Parser ". I like it because it works like the ASP.NET querystring parser - it handles the parsing (quoted strings, different delimiter styles) and exposes a string dictionary with the results.

I use a GetSettings accessor that reads the default from the app.config file, but allows overrides via command line. I like this approach because settings are their standard location (app.config), and any config setting can be overriden via command line without an attribute change and a recompile.

[STAThread]
private static int Main(string[] args)
{
    Processor processor1 = 
new Processor(args);
    
return processor1.Process();
}
private Arguments arguments;

public Processor(string[] args)
{
    
this.arguments = new Arguments(args);
}

public Process()
{
    Console.WriteLine(
this.GetSetting("PreventEvil"));
}

private string GetSetting(string key)
{
    
string setting = string.Empty;
    
if (this.arguments[key] != null)
    {
        setting = 
this.arguments[key];
    }
    
else
    
{
        setting = ConfigurationSettings.AppSettings.Get(key);
    }
    
if (setting == null)
    {
        
return string.Empty;
    }
    
return setting;
}
Published Monday, October 24, 2005 5:43 AM by Jon Galloway
Filed under:

Comments

# re: Yet another command line parsing system

Monday, October 24, 2005 10:22 AM by Fabrice

# re: Yet another command line parsing system

There's also Yet Another Command Line Parser in Genghis:

http://www.sellsbrothers.com/tools/genghis/

Monday, October 24, 2005 12:35 PM by Jeff Atwood

# re: Yet another command line parsing system

I've been using Nini for a several years for command line and ini parsing:  http://nini.sourceforge.net.

(but I see you just tweeted about that ;) )

Monday, March 17, 2008 2:23 PM by Tom Opgenorth

# re: Yet another command line parsing system

NConsoler is an open source library that provides command line parser functionality based on attribute metadata attached to type.

Library is very easy to add and use in your application. NConsoler gives an ability to display help and validation messages without any line of code.

http://nconsoler.csharpus.com/

Example code:

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

  }

}

and use it:

program.exe "Maxim" /-flag

Monday, September 08, 2008 1:07 PM by tihobrazov

# re: Yet another command line parsing system

I used the below, good thing is its part of a C# Commons library so the library has a lot of other useful features.

commonlibrarynet.codeplex.com

Thursday, January 28, 2010 1:44 AM by Warren

# re: Yet another command line parsing system

Fantastic beat ! I wish to apprentice while you amend your website, how can i subscribe for a blog site' The account helped me a acceptable deal. I had been a little bit acquainted of this your broadcast provided bright clear idea

<b><a href="brevardgreens.com/all-about-home-security

">Home Security Monitoring

<a/><b/>

Wednesday, April 06, 2011 11:36 AM by Home Security Monitoring

# re: Yet another command line parsing system

I've used the one at

cinchoo.wordpress.com/.../commandline

Used it couple of projects. Works pretty well.

Tuesday, January 10, 2012 11:00 PM by nittu

# re: Yet another command line parsing system

I found some nice solution with linq and extension methods. Just look at lukasz-lademann.blogspot.com/.../c-command-line-arguments-parser.html for details

Sunday, January 20, 2013 2:24 PM by Luke

Leave a Comment

(required) 
(required) 
(optional)
(required)