82

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.

Guss
  • 30,470
  • 17
  • 104
  • 128
dr. evil
  • 26,944
  • 33
  • 131
  • 201
  • The most flexible .NET command line parser seems to be: [gsscoder's CommandLineParser](https://github.com/commandlineparser/commandline) – AlexMelw Jan 15 '18 at 17:36

12 Answers12

54

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

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Raoul
  • 2,043
  • 1
  • 21
  • 29
  • I'm trying to implement this, does it support "flag" arguments? such as "-enable" which doesn't require a value just true/false stuff. I couldn't find it. – dr. evil Mar 11 '09 at 13:40
  • If i remember right it doesn't out of the box, but it shouldn't be too much work to modify the parser to look for unqualified switches for boolean values rather than expect true / false to be specified. – Raoul Mar 12 '09 at 21:16
  • The mono parser is called Mono.GetOptions by the way. That definitely supports what you are looking for. You could download just that part of the mono framework source and build it and reference it in your project as an alernative. – Raoul Mar 12 '09 at 21:24
  • I've used this library myself, and agree it's an excellent library. – Noldorin Sep 09 '09 at 19:14
  • @dr. evil, yes it does support bool switches. You don't have to do anything special. There is an example of that with the -i switch in the sample code that comes with the download. – dan-gph Jul 30 '10 at 02:28
  • This is a pretty cool library. Thanks for the tip. What would be nice though, would be a parser which set the application settings file from arguments as soon as the application runs. That way one could divest oneself of the additional class coding required to support this library. – Tom W Mar 05 '11 at 10:12
  • Two off-the-beaten-track ones: https://github.com/nessos/UnionArgParser and https://github.com/docopt/docopt.net – Ruben Bartelink Oct 18 '14 at 15:11
  • Tried Command Line Parser recommended by Raoul, but did not like it. There is hardly any documentation and when things go wrong, the only option is diving in the source code. After testing a number of the most popular implementations, I settled on Plossum.CommandLine (http://goo.gl/3g5K91, or nuget). Also imperfect, but at least it's documented. (Btw, hate when moderators close such valuable posts.) – Alek Davis Nov 13 '14 at 22:12
  • In the meantime the dotnet team is also working on a command line api [System.CommandLine](https://github.com/dotnet/command-line-api) (Still in preview) – Sil Mar 25 '20 at 10:16
21

Have a look at ndesk.options.

It is called Mono.Options now.

Pavel Chuchuva
  • 22,633
  • 10
  • 99
  • 115
John
  • 211
  • 1
  • 2
  • 3
    We've used ndesk.options with a lot of success. It's a single class you can just compile into your code: http://www.ndesk.org/Options – Sean Carpenter Mar 26 '09 at 13:11
  • Currently, their site has a warning about NDesk.Options being unstable. Just a warning...maybe use an older version. – Kilhoffer Apr 15 '11 at 16:18
  • I also have had a good experience with NDesk.Options. It is modeled on Perl's Getopt::Long (which is excellent). – jwd Jan 23 '12 at 17:16
5

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,

Sophie Alpert
  • 139,698
  • 36
  • 220
  • 238
Stewart
  • 4,223
  • 6
  • 31
  • 39
4

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.

John Leidegren
  • 59,920
  • 20
  • 131
  • 152
  • Yeah, PowerShell is probably the way to go in most cases. Writing a cmdlet is fairly easy, yet opens up so many possibilities, with little or no work. – John Saunders Mar 10 '09 at 17:29
  • I'm not quite sure what you mean by looking into powershell. It's not open source or do you mean I might find a cmdlet and convert it to .NET syntax . – dr. evil Mar 10 '09 at 18:26
  • 3
    I think he means "write your code as a PowerShell cmdlet instead of a stand-alone executable." – Joel Mueller Apr 13 '09 at 20:25
  • My default plan of action these days is to write command line oriented programs as powershell cmdlets. That way I don't have to worry about how the command line is parsed. – Peter Stephens Oct 10 '11 at 14:40
  • I think powerhell's arg parsing is horrible. – Tim Abell May 14 '15 at 17:28
3

I suggest NDesk.Options

look here:

Best way to parse command line arguments in C#?

Community
  • 1
  • 1
danyolgiax
  • 12,798
  • 10
  • 65
  • 116
2

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

Mike Two
  • 44,935
  • 9
  • 80
  • 96
1

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.
Brian
  • 37,399
  • 24
  • 94
  • 109
1

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • 1
    Since this is a well known problem and almost a must for everysingle commandline tool out there I was assuming there should be some mature solutions, and maintaining and writing my own sound a bit spending my time. But hey, since there is not enough answer here maybe you are right. – dr. evil Mar 10 '09 at 18:28
  • 2
    @Downvoter: -2 rep doesn't bother me. If you want to make a difference, then please give the reason for the downvote. – John Saunders Sep 10 '09 at 16:16
1

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

Ken
  • 2,651
  • 3
  • 19
  • 17
1

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

Oliver
  • 43,366
  • 8
  • 94
  • 151
  • https://resources.oreilly.com/examples/9780596516109/-/blob/master/CSharpRecipes/03_ClassAndStructs.cs is the direct URL. It's interesting. – Charlesdwm Feb 05 '22 at 18:31
0

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.

Daniel Goldberg
  • 19,908
  • 4
  • 21
  • 29
0

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.

Phill Tew
  • 152
  • 8