views:

2544

answers:

10

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.

+1  A: 

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
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
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
I think he means "write your code as a PowerShell cmdlet instead of a stand-alone executable."
Joel Mueller
+1  A: 

Check this out: http://www.codeproject.com/KB/recipes/ccmdline.aspx. Hope it helps.

TuxMeister
Looks all right but This is not .NET though.
dr. evil
A: 

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
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
@Downvoter: -2 rep doesn't bother me. If you want to make a difference, then please give the reason for the downvote.
John Saunders
+15  A: 

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)

Raoul
This looks great I'll try it.
dr. evil
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
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
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
I've used this library myself, and agree it's an excellent library.
Noldorin
@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.
dangph
A: 

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
+1  A: 

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
+9  A: 

Have a look at ndesk.options

John
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
+1  A: 

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,

Stewart
+2  A: 

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
System.Shell.CommandLine ended up being cut from v4 of the Framework.
fatcat1111
A: 

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