Looking for a Command Line Argument Parser for .NE

2019-01-08 06:00发布

问题:

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:

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)



回答2:

Have a look at ndesk.options.

It is called Mono.Options now.



回答3:

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,



回答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.



回答5:

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



回答6:

I suggest NDesk.Options

look here:

Best way to parse command line arguments in C#?



回答7:

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.



回答8:

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



回答9:

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



回答10:

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.


回答11:

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.



回答12:

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.