Pau*_*ell 733 .net c# command-line-arguments
构建带参数的控制台应用程序时,可以使用传递给的参数Main(string[] args)
.
在过去,我只是索引/循环该数组并完成一些正则表达式来提取值.但是,当命令变得更复杂时,解析会非常难看.
所以我对以下内容感兴趣:
假设命令始终遵循通用标准,例如此处的答案.
jon*_*onp 324
我强烈建议使用NDesk.Options(Documentation)和/或Mono.Options(相同的API,不同的命名空间).文档中的一个示例:
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;
}
Run Code Online (Sandbox Code Playgroud)
Adr*_*ore 198
我非常喜欢Command Line Parser Library(http://commandline.codeplex.com/).它有一种非常简单而优雅的方式来通过属性设置参数:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
use*_*116 50
在WPF TestApi库配备了最好的命令行解析器C#开发的一个.我强烈建议您从Ivo Manolov关于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);
Run Code Online (Sandbox Code Playgroud)
aba*_*hev 24
请查看http://github.com/mono/mono/tree/master/mcs/class/Mono.Options/
Bri*_*ian 14
看起来每个人都有他们自己的宠物命令行解析器,图我最好添加我的:).
该库包含一个命令行解析器,它将使用命令行中的值初始化一个类.它有很多功能(我已经建立了很多年).
从文档 ......
BizArk框架中的命令行解析具有以下主要功能:
CLAP(命令行参数解析器)具有可用的API,并且记录得非常好.你创建了一个方法,注释参数. https://github.com/adrianaisemberg/CLAP
这个问题有很多解决方案.为了完整性并提供备选,如果有人想要我在谷歌代码库中添加两个有用的类的答案.
第一个是ArgumentList,它只负责解析命令行参数.它收集由开关'/ x:y'或'-x = y'定义的名称 - 值对,并且还收集'未命名'条目的列表.它的基本用法是这里讨论,查看这里的类.
第二部分是CommandInterpreter,它从.Net类中创建一个功能齐全的命令行应用程序.举个例子:
using CSharpTest.Net.Commands;
static class Program
{
static void Main(string[] args)
{
new CommandInterpreter(new Commands()).Run(args);
}
//example ‘Commands’ class:
class Commands
{
public int SomeValue { get; set; }
public void DoSomething(string svalue, int ivalue)
{ ... }
Run Code Online (Sandbox Code Playgroud)
使用上面的示例代码,您可以运行以下代码:
Program.exe DoSomething"字符串值"5
- 要么 -
Program.exe dosomething/ivalue = 5 -svalue:"string value"
它就像你需要的那样简单或复杂.您可以查看源代码,查看帮助或下载二进制文件.
归档时间: |
|
查看次数: |
488015 次 |
最近记录: |