Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking for a Command Line Argument Parser for .NET [closed]

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.

like image 645
dr. evil Avatar asked Mar 10 '09 17:03

dr. evil


People also ask

What is command line arguments in c# net?

The arguments which are passed by the user or programmer to the Main() method is termed as Command-Line Arguments. Main() method is the entry point of execution of a program. Main() method accepts array of strings. But it never accepts parameters from any other method in the program.

What is String args in C#?

The string[] args is a variable that has all the values passed from the command line as shown above. Now to print those arguments, let's say we have an argument, “One” − Console. WriteLine("Length of the arguments: "+args. Length); Console. WriteLine("Arguments:"); foreach (Object obj in args) { Console.

What is argument parsing?

argparse — parse the arguments. Using argparse is how you let the user of your program provide values for variables at runtime. It's a means of communication between the writer of a program and the user.


10 Answers

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)

like image 140
Raoul Avatar answered Oct 06 '22 01:10

Raoul


Have a look at ndesk.options.

It is called Mono.Options now.

like image 38
John Avatar answered Oct 06 '22 01:10

John


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:

  • getopt# on freshmeat.net
  • C# getopt at PHPGuru
  • XGetoptCS on CodeProject
  • GetOpt for .NET on CodeProject
  • Getopt C#.NET on Codeplex

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

like image 38
Stewart Avatar answered Oct 06 '22 00:10

Stewart


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.

like image 41
John Leidegren Avatar answered Oct 06 '22 01:10

John Leidegren


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

like image 45
Mike Two Avatar answered Oct 06 '22 02:10

Mike Two


I suggest NDesk.Options

look here:

Best way to parse command line arguments in C#?

like image 26
danyolgiax Avatar answered Oct 06 '22 01:10

danyolgiax


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.

like image 31
John Saunders Avatar answered Oct 06 '22 01:10

John Saunders


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

like image 30
Ken Avatar answered Oct 06 '22 02:10

Ken


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

like image 26
Oliver Avatar answered Oct 06 '22 01:10

Oliver


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.
like image 42
Brian Avatar answered Oct 06 '22 02:10

Brian