Looking for a Command Line Argument Parser for .NET

C#.NetCommand LineArguments

C# Problem Overview


I'm looking for a command line argument parser, such as "Command line parser" from http://www.sellsbrothers.com/tools/Genghis/ .

Features I'm looking for:

  • Auto-generation of usage
  • Should able to check required and optional parameters
  • Parameters should support IEnumerable with separator support
  • Should support flag parameters
  • Would be nice to support combining parameters such as "/fx" == "/f /x"
  • Would be nice to not force for a space after a parameter such as "/ftest.txt" == "/f test.txt"

P.S : "Command line parser" is quite good, I really like the design of it but there is no documentation, no new updates and I couldn't figure out to do certain stuff such as how to check for required parameters.

C# Solutions


Solution 1 - C#

My personal favourite 3rd party commandline parsing library is Command Line Parser and I assume this is the one you are referring to. The most recent release was less than 2 months ago and there are regular commits. If you want a more mature offering you could chek out the console library in the mono project (sorry I can't seem to find a direct link to the namespace at the moment, but its a part of the mono framework)

Solution 2 - C#

Have a look at ndesk.options.

It is called Mono.Options now.

Solution 3 - C#

A popular and pretty comprehensive C command-line parser is GNU getopt. This has been ported (or cloned) for C#/.Net several times. Some of these include:

Take your pick! There are several others, and google can tell you about those,

Solution 4 - C#

Sadly there's no built in support for handling that in a standard manner. Have you looked into PowerShell? I bet there's a class in that shell which does exactly what you want or something similar.

Solution 5 - C#

Edit: as fatcat1111 points out, this feature did not ship with the final version of .net 4.0.

C# 4.0 has a pretty good one. Probably not very helpful yet, but you might want to consider looking at something that will make the jump to the built in one easy when it comes out. Bart De Smet talked about it on his B# blog

Solution 6 - C#

Solution 7 - C#

Consider that once you start using this parser, you'll either have to maintain it yourself, or else depend on someone else to maintain it for you. You may be better off writing your own, starting from your most critical, immediate, requirements. I've found that it doesn't take too much work to produce some fairly complicated command-line parsing for most console-based applications I've worked on.

I've also found that when the parsing gets too complicated, it may be time to stop using the command line.

Solution 8 - C#

I'm betting this is not quite what you're looking for, but:

Somebody here had that problem, and his first thought was "hey, ocaml has a pretty good one!", and quickly ported it to F#.

Solution 9 - C#

I'm using the parser out of the C# 3.0 cookbook.

All the examples from this book can be downloaded here: http://examples.oreilly.com/9780596516109/

Search for 'Arguments' and you'll find it. You have to do some little code changes to get it out of the whole thing into your own class, but this is no big problem.

It supports all your points except the last two ones (parameter combining & missing space).

Solution 10 - C#

The BizArk library contains a command-line parser.

Basically you just create a class that inherits from CmdLineObject, add properties that you want populated from the command-line, add a CmdLineArgAttribute to the properties, then call Initialize in your program. It supports ClickOnce URL arguments too!

Features (from the site)...

  • Automatic initialization: Class properties are automatically set based on the command-line arguments.
  • Default properties: Send in a value without specifying the property name.
  • Value conversion: Uses the powerful ConvertEx class also included in BizArk to convert values to the proper type.
  • Boolean flags. Flags can be specified by simply using the argument (ex, /b for true and /b- for false) or by adding the value true/false, yes/no, etc.
  • Argument arrays. Simply add multiple values after the command-line name to set a property that is defined as an array. Ex, /x 1 2 3 will populate x with the array { 1, 2, 3 } (assuming x is defined as an array of integers).
  • Command-line aliases: A property can support multiple command-line aliases for it. For example, Help uses the alias ?.
  • Partial name recognition. You don’t need to spell out the full name or alias, just spell enough for the parser to disambiguate the property/alias from the others.
  • Supports ClickOnce: Can initialize properties even when they are specified as the query string in a URL for ClickOnce deployed applications. The command-line initialization method will detect if it is running as ClickOnce or not so your code doesn’t need to change when using it.
  • Automatically creates /? help: This includes nice formatting that takes into account the width of the console.
  • Load/Save command-line arguments to a file: This is especially useful if you have multiple large, complex sets of command-line arguments that you want to run multiple times.

Solution 11 - C#

I'm a fan of the C# port to OptParse, a built in library in Python. It's rather simple to use compared to most of the other suggestions here and contains a number of useful features in addition to just auto parsing.

Solution 12 - C#

You may like my one Rug.Cmd

Easy to use and expandable command line argument parser. Handles: Bool, Plus / Minus, String, String List, CSV, Enumeration.

Built in '/?' help mode.

Built in '/??' and '/?D' document generator modes.

static void Main(string[] args) 
{            
    // create the argument parser
    ArgumentParser parser = new ArgumentParser("ArgumentExample", "Example of argument parsing");

    // create the argument for a string
    StringArgument StringArg = new StringArgument("String", "Example string argument", "This argument demonstrates string arguments");

    // add the argument to the parser 
    parser.Add("/", "String", StringArg);
        
    // parse arguemnts
    parser.Parse(args);

    // did the parser detect a /? argument 
    if (parser.HelpMode == false) 
    {
        // was the string argument defined 
        if (StringArg.Defined == true)
        {
            // write its value
            RC.WriteLine("String argument was defined");
            RC.WriteLine(StringArg.Value);
        }
    }
}

Edit: This is my project and as such this answer should not be seen as an endorsement from a third party. That said I do use it for every command line based program I write, it is open source and it is my hope that others may benefit from it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiondr. evilView Question on Stackoverflow
Solution 1 - C#RaoulView Answer on Stackoverflow
Solution 2 - C#JohnView Answer on Stackoverflow
Solution 3 - C#StewartView Answer on Stackoverflow
Solution 4 - C#John LeidegrenView Answer on Stackoverflow
Solution 5 - C#Mike TwoView Answer on Stackoverflow
Solution 6 - C#danyolgiaxView Answer on Stackoverflow
Solution 7 - C#John SaundersView Answer on Stackoverflow
Solution 8 - C#KenView Answer on Stackoverflow
Solution 9 - C#OliverView Answer on Stackoverflow
Solution 10 - C#BrianView Answer on Stackoverflow
Solution 11 - C#Daniel GoldbergView Answer on Stackoverflow
Solution 12 - C#Phill TewView Answer on Stackoverflow