Best way to parse command line arguments in C#?

When building console applications that take parameters, you can use the arguments passed to Main(string[] args) .

In the past I've simply indexed/looped that array and done a few regular expressions to extract the values. However, when the commands get more complicated, the parsing can get pretty ugly.

So I'm interested in:

  • Libraries that you use
  • Patterns that you use
  • Assume the commands always adhere to common standards such as answered here.


    I would strongly suggest using NDesk.Options (Documentation) and/or Mono.Options (same API, different namespace). An example from the documentation:

    bool show_help = false;
    List<string> names = new List<string> ();
    int repeat = 1;
    
    var p = new OptionSet () {
        { "n|name=", "the {NAME} of someone to greet.",
           v => names.Add (v) },
        { "r|repeat=", 
           "the number of {TIMES} to repeat the greeting.n" + 
              "this must be an integer.",
            (int v) => repeat = v },
        { "v", "increase debug message verbosity",
           v => { if (v != null) ++verbosity; } },
        { "h|help",  "show this message and exit", 
           v => show_help = v != null },
    };
    
    List<string> extra;
    try {
        extra = p.Parse (args);
    }
    catch (OptionException e) {
        Console.Write ("greet: ");
        Console.WriteLine (e.Message);
        Console.WriteLine ("Try `greet --help' for more information.");
        return;
    }
    

    I really like the Command Line Parser Library ( http://commandline.codeplex.com/ ). It has a very simple and elegant way of setting up parameters via attributes:

    class Options
    {
        [Option("i", "input", Required = true, HelpText = "Input file to read.")]
        public string InputFile { get; set; }
    
        [Option(null, "length", HelpText = "The maximum number of bytes to process.")]
        public int MaximumLenght { get; set; }
    
        [Option("v", null, HelpText = "Print details during execution.")]
        public bool Verbose { get; set; }
    
        [HelpOption(HelpText = "Display this help screen.")]
        public string GetUsage()
        {
            var usage = new StringBuilder();
            usage.AppendLine("Quickstart Application 1.0");
            usage.AppendLine("Read user manual for usage instructions...");
            return usage.ToString();
        }
    }
    

    The WPF TestApi library comes with one of the nicest command line parsers for C# development. I highly recommend looking into it, from Ivo Manolov's blog on the API:

    // EXAMPLE #2:
    // Sample for parsing the following command-line:
    // Test.exe /verbose /runId=10
    // This sample declares a class in which the strongly-
    // typed arguments are populated
    public class CommandLineArguments
    {
       bool? Verbose { get; set; }
       int? RunId { get; set; }
    }
    
    CommandLineArguments a = new CommandLineArguments();
    CommandLineParser.ParseArguments(args, a);
    
    链接地址: http://www.djcxy.com/p/5150.html

    上一篇: 在Python中创建一个包含列表理解的词典

    下一篇: 在C#中解析命令行参数的最佳方法?