如果你愿意,你可以编写一个解析命令行的代码。
但是有很多库可供您使用。
有一个很好的命令行库:'https://github.com/commandlineparser/commandline'
它非常易于使用,希望对您有所帮助。
您必须创建一个包含所有选项的“选项”类。
要声明一个选项,请使用“选项”属性:
[Option(char shortoption, string longoption, Required = bool, MetaValue = string, Min = int, Seperator = char, SetName = string)]
public <type> <name> { get; set; }
然后您可以将字符串数组解析为您的“选项”类,然后您可以从类变量中读取选项。
例子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using CommandLine;
namespace ConsoleApp1
{
//Declare some options
public class Options
{
//Format:
//[Option(char shortoption, string longoption, Required = bool, MetaValue = string, Min = int, Seperator = char, SetName = string)]
//public <type> <name> { get; set; }
[Option('r', "read", Required = true, HelpText = "Filename", Separator = ' ')]
public IEnumerable<string> InputFiles { get; set; }
[Option('e', "echo", Required = true, HelpText = "Echo message")]
public string echo { get; set; }
}
class Program
{
static void Main(string[] args)
{
bool s = true;
CommandLine.Parser.Default.ParseArguments<Options>(args).WithNotParsed<Options>((errs) =>
{
foreach (Error e in errs)
{
Console.WriteLine("ERROR " + e.Tag.ToString());
s = e.StopsProcessing ? false : s;
}
if(!s)
{
return;
}
}).WithParsed<Options>(opts => {
Console.WriteLine(opts.echo);
foreach(string filename in opts.InputFiles)
{
Console.WriteLine(File.ReadAllText(filename));
}
});
}
}
}
控制台:
ConsoleApp1\bin\Debug>ConsoleApp1.exe -r helps.txt sayhello.txt -e "Thanks for reading"
Thanks for reading
I hope this helps
Hello world
ConsoleApp1\bin\Debug>ConsoleApp1.exe -r -fds fds fds-f dsf -
ConsoleApp1 1.0.0.0
Copyright © 2018
ERROR(S):
Option 'f' is unknown.
Required option 'e, echo' is missing.
-r, --read Required. Filename
-e, --echo Required. Echo message
--help Display this help screen.
--version Display version information.
ERROR UnknownOptionError
ERROR MissingRequiredOptionError
ConsoleApp1\bin\Debug>ConsoleApp1.exe -e f
ConsoleApp1 1.0.0.0
Copyright © 2018
ERROR(S):
Required option 'r, read' is missing.
-r, --read Required. Filename
-e, --echo Required. Echo message
--help Display this help screen.
--version Display version information.
ERROR MissingRequiredOptionError
ConsoleApp1\bin\Debug>ConsoleApp1.exe -r helps.txt sayhello.txt -e ":)"
:)
I hope this helps
Hello world
ConsoleApp1\bin\Debug>
如何安装?
转到查看 -> 其他窗口 -> 包管理器控制台
输入命令:
Install-Package CommandLineParser -Version 2.2.1 -ProjectName <yourprojectname>
Github:
https://github.com/commandlineparser/commandline
我希望这会有所帮助。
阅读:Split string containing command-line parameters into string[] in C#。 Windows 已经导入了该功能。 (拆分命令参数)
但你也可以自己制作(简单的函数,不会在“之间分割”):
static string[] ParseArguments(string commandLine)
{
char[] parmChars = commandLine.ToCharArray();
bool inQuote = false;
for (int index = 0; index < parmChars.Length; index++)
{
if (parmChars[index] == '"')
inQuote = !inQuote;
if (!inQuote && parmChars[index] == ' ')
parmChars[index] = '\n';
}
return (new string(parmChars)).Split('\n');
}