GetOpt library for C#

前端 未结 8 593
长发绾君心
长发绾君心 2020-12-08 07:36

I\'m looking for a getopt library for c#. So far I found a few (phpguru, XGetOptCS, getoptfordotnet) but these look more like unfinished attempts that only support a part of

8条回答
  •  情书的邮戳
    2020-12-08 07:43

    A friend of mine suggested docopt.net, a command-line argument parsing library based on the docopt library for Node.JS. It is very simple to use, yet advanced and parses arguments based on the help string you write.

    Here's some sample code:

    using System;
    using DocoptNet;
    
    namespace MyProgram
    {
        static class Program
        {
            static void Main(string[] args)
            {
                // Usage string
                string usage = @"This program does this thing.
    
    Usage:
      program set 
      program do  [-o ]
      program do  [somethingelse]";
    
                try
                {
                    var arguments = new Docopt().Apply(usage, args, version: "My program v0.1.0", exit: false);
                    foreach(var argument in arguments)
                        Console.WriteLine("{0} = {1}", argument.Key, argument.Value);
                }
                catch(Exception ex)
                {
                    //Parser errors are thrown as exceptions.
                    Console.WriteLine(ex.Message);
                }
            }
        }
    }
    

    You can find documentation for it (including its help message format) at both the first link and here.

    Hope it helps someone!

提交回复
热议问题