C# 的 GetOpt 库

发布于 2024-07-07 20:31:02 字数 366 浏览 11 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

原来是傀儡 2024-07-14 20:31:03

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

以下是一些示例代码:

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);
            }
        }
    }
}

您可以在第一个链接和此处找到它的文档(包括其帮助消息格式) 。

希望它能帮助别人!

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 <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!

过度放纵 2024-07-14 20:31:03

郑重声明,NUnit 在 src\ClientUtilities\util\CommandLineOptions 中包含一个简单的单文件命令行解析器。 cs (请参阅位于 src\ConsoleRunner\nunit-console 下的 ConsoleRunner.csRunner.cs 中的示例用法)。 文件本身不包含任何许可信息,以及指向其“上游”的链接似乎已经死亡,因此其法律地位不确定。

解析器支持命名标志参数(如 /verbose)、带值的命名参数(如 /filename:bar.txt)和未命名参数,即,很多风格Windows 脚本宿主如何解释它们。 选项可能以 /--- 为前缀。

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

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

梦幻之岛 2024-07-14 20:31:02

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

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

三岁铭 2024-07-14 20:31:02

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

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

-残月青衣踏尘吟 2024-07-14 20:31:02

这里是我写的,它有效相当不错,并且以少量的代码提供了相当多的功能。 然而,它不是 getopts,但它可能适合您的需求。

请随意问一些问题。

Here is 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.

Feel free to ask some questions.

爱你不解释 2024-07-14 20:31:02

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

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

孤独患者 2024-07-14 20:31:02

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文