解析C#中的命令行选项 [英] Parsing command-line options in C#

查看:83
本文介绍了解析C#中的命令行选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到人们编写自定义类来更轻松地处理各种语言的命令行选项.我想知道.NET(3.5或更低版本)是否内置任何东西,以便您不必自定义解析诸如以下内容:

I've seen people write custom classes to more easily handle command line options in various languages. I wondered if .NET (3.5 or lower) has anything built in so that you don't have to custom-parse things like:

myapp.exe file = text.txt

myapp.exe file=text.txt

推荐答案

对于不需要任何复杂内容的快速脏乱的实用程序,控制台应用程序通常采用以下形式的命令行:

For quick-and-dirty utilities where you don't need anything sophisticated, many times a console application takes command lines of the form:

program.exe command -option1 optionparameter option2 optionparameter

在这种情况下,要获取命令",只需使用 args [0]

When that's the case, to get the 'command', just use args[0]

要获取一个选项,请使用类似以下的内容:

To get an option, use something like this:

var outputFile = GetArgument(args, "-o");

其中 GetArgument 定义为:

string GetArgument(IEnumerable<string> args, string option)
    => args.SkipWhile(i => i != option).Skip(1).Take(1).FirstOrDefault();

这篇关于解析C#中的命令行选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆