C# 的 GetOpt 库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/172443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:36:07  来源:igfitidea点击:

GetOpt library for C#

c#getopt

提问by Marijn Deé

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 C's getopt. Is there a full getopt c# implementation?

我正在寻找 c# 的 getopt 库。到目前为止,我发现了一些(phpguruXGetOptCSgetoptfordotnet),但这些看起来更像是未完成的尝试,只支持 C 的 getopt 的一部分。是否有完整的 getopt c# 实现?

采纳答案by Thomas

Here is a .NET Implementation of getopt: http://www.codeplex.com/getopt

这是 getopt 的 .NET 实现:http: //www.codeplex.com/getopt

回答by Matt

It's not getopt, but you might try NConsoler. It uses attributes to define arguments and actions.

这不是 getopt,但您可以尝试NConsoler。它使用属性来定义参数和操作。

回答by leppie

Hereis something I wrote, it works rather nice, and has quite a lot of features for the tiny amount of code. It is not getopts however, but it may suit your needs.

是我写的东西,它工作得相当好,并且为少量代码提供了相当多的功能。然而,它不是 getopts,但它可能适合您的需求。

Feel free to ask some questions.

随意问一些问题。

回答by Ian McLaird

The Mono Project has (or rather had) one based on attributes, but last I checked it was marked as obsolete. But since it's open source, you might be able to pull the code out and use it yourself.

Mono Project 有(或者更确切地说有)一个基于属性的,但最后我检查它被标记为过时。但由于它是开源的,您也许可以将代码拉出来并自己使用。

回答by Remco Schoeman

For posterity:

为后人:

CommandParser is another onewith a BSD license

CommandParser 是另一个具有 BSD 许可证的

回答by Wim Coenen

Miguel de Icaza raves about Mono.Options. You can use the nuget package, or just copy the single C# source fileinto your project.

Miguel de Icaza对 Mono.Options 赞不绝口。您可以使用nuget 包,或者只是将单个 C# 源文件复制到您的项目中。

回答by kostix

For the record, NUnitincludes a simple one-file command-line parser in src\ClientUtilities\util\CommandLineOptions.cs(see example usage in ConsoleRunner.csand Runner.cslocated under src\ConsoleRunner\nunit-console). The file itself does not include any licencing information, and a linkto its "upstream" seems to be dead, so its legal status is uncertain.

作为记录,NUnit包含一个简单的单文件命令行解析器src\ClientUtilities\util\CommandLineOptions.cs(请参阅 中ConsoleRunner.csRunner.cs位于 下的示例用法src\ConsoleRunner\nunit-console)。该文件本身不包含任何许可信息,指向其“上游”的链接似乎已失效,因此其法律地位尚不确定。

The parser supports named flag parameters (like /verbose), named parameters with values (like /filename:bar.txt) and unnamed parameters, that is, much in style of how Windows Scripting Host interprets them. Options might be prefixed with /, -and --.

解析器支持命名标志参数(如/verbose)、带值的命名参数(如/filename:bar.txt)和未命名参数,也就是说,与Windows Scripting Host 解释它们的方式非常相似。选项可能以/,-和为前缀--

回答by Watercolor Games

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

我的一个朋友推荐了docopt.net,这是一个基于Node.JS 的 docopt库的命令行参数解析库。它使用起来非常简单,但非常先进,并且可以根据您编写的帮助字符串解析参数。

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 <something>
  program do <something> [-o <optionalthing>]
  program do <something> [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!

希望它可以帮助某人!