C# 为 .NET 寻找命令行参数解析器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/631410/
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-04 11:05:18  来源:igfitidea点击:

Looking for a Command Line Argument Parser for .NET

c#.netcommand-linearguments

提问by dr. evil

I'm looking for a command line argument parser, such as "Command line parser" from http://www.sellsbrothers.com/tools/Genghis/.

我正在寻找命令行参数解析器,例如来自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"
  • 自动生成使用情况
  • 应该能够检查必需和可选参数
  • 参数应支持带有分隔符支持的 IEnumerable
  • 应该支持标志参数
  • 支持组合参数会很好,例如“/fx”==“/f /x”
  • 最好不要在诸如“/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.

PS: “命令行解析器”非常好,我真的很喜欢它的设计,但没有文档,没有新的更新,我无法弄清楚如何做某些事情,例如如何检查所需的参数。

采纳答案by Raoul

My personal favourite 3rd party commandline parsing library is Command Line Parserand 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)

我个人最喜欢的第 3 方命令行解析库是Command Line Parser,我认为这就是您所指的那个。最近的版本是在不到 2 个月前发布的,并且有定期提交。如果您想要更成熟的产品,您可以查看 mono 项目中的控制台库(抱歉,我目前似乎找不到指向命名空间的直接链接,但它是 mono 框架的一部分)

回答by John Leidegren

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.

可悲的是,没有内置支持以标准方式处理它。你有没有研究过 PowerShell?我敢打赌,shell 中有一个类可以完全满足您的要求或类似的操作。

回答by John Saunders

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.

我还发现,当解析变得过于复杂时,可能是停止使用命令行的时候了。

回答by Ken

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

这里有人遇到了这个问题,他的第一个想法是“嘿,ocaml 有一个很好的!”,并迅速将其移植到 F#。

回答by Oliver

I'm using the parser out of the C# 3.0 cookbook.

我正在使用 C# 3.0 说明书中的解析器。

All the examples from this book can be downloaded here: http://examples.oreilly.com/9780596516109/

本书的所有例子都可以在这里下载: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).

它支持除最后两个点之外的所有点(参数组合和缺失空间)。

回答by John

Have a look at ndesk.options.

看看 ndesk.options。

It is called Mono.Optionsnow.

它现在被称为Mono.Options

回答by Stewart

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:

一个流行且非常全面的 C 命令行解析器是GNU getopt。这已被多次移植(或克隆)用于 C#/.Net。其中一些包括:

Take your pick! There are several others, and google can tell you about those,

随你挑!还有其他几个,谷歌可以告诉你这些,

回答by Mike Two

Edit:as fatcat1111 points out, this feature did not ship with the final version of .net 4.0.

编辑:正如 fatcat1111 指出的那样,此功能并未随 .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

C# 4.0 有一个很好的。可能还不是很有帮助,但是您可能需要考虑查看一些可以轻松跳转到内置的东西。Bart De Smet 在他的B# 博客中谈到了它

回答by Daniel Goldberg

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.

我是 OptParse 的 C# 端口的粉丝,OptParse 是 Python 中的一个内置库。与此处的大多数其他建议相比,它使用起来相当简单,并且除了自动解析之外还包含许多有用的功能。

回答by Brian

The BizArk librarycontains 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!

基本上,您只需创建一个继承自 CmdLineObject 的类,添加要从命令行填充的属性,向属性添加 CmdLineArgAttribute,然后在程序中调用 Initialize。它也支持 ClickOnce URL 参数!

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.
  • 自动初始化:根据命令行参数自动设置类属性。
  • 默认属性:在不指定属性名称的情况下发送值。
  • 值转换:使用强大的 ConvertEx 类也包含在 BizArk 中,将值转换为正确的类型。
  • 布尔标志。可以通过简单地使用参数(例如,/b 表示真和 /b- 表示假)或通过添加值 true/false、yes/no 等来指定标志。
  • 参数数组。只需在命令行名称后添加多个值即可设置定义为数组的属性。例如, /x 1 2 3 将用数组 { 1, 2, 3 } 填充 x (假设 x 被定义为一个整数数组)。
  • 命令行别名:一个属性可以支持多个命令行别名。例如,帮助使用别名 ?。
  • 部分名称识别。您不需要拼出全名或别名,只需拼出足以让解析器消除其他属性/别名的歧义即可。
  • 支持 ClickOnce:即使属性被指定为 ClickOnce 部署应用程序的 URL 中的查询字符串,也可以初始化这些属性。命令行初始化方法将检测它是否作为 ClickOnce 运行,因此您的代码在使用时不需要更改。
  • 自动创建 /? 帮助:这包括考虑到控制台宽度的漂亮格式。
  • 将命令行参数加载/保存到文件:如果您有多个大型、复杂的命令行参数集想要多次运行,这将特别有用。