[英]Parsing a string using command Line parser
我下載了這個 package https://github.com/commandlineparser/commandline我想對字符串進行解析
string str = "file:xxxx\\xxxx\\xxxxx.sh val:-a nsdd m";
所以
file = xxxx\\xxxx\\xxxxx.sh
val = -a nsdd m
我想知道是否有人想到了庫或使用了指定的庫來獲取字符串中指定的參數。 我很難理解有關如何解析該字符串並獲取文件參數和 val 參數的示例。 我知道我可以進行字符串操作,但我寧願為此使用現有的經過測試的持久解決方案。
我使用過這個庫,這是一個不錯的選擇。
這是一個非常基本的示例,使用了您發布的一些內容,請參閱代碼注釋以進行說明。
class Program
{
static void Main(string[] args)
{
// args a space separated array so you should use an array for your test
// args are identified with the `-` so you should set args like `-f somefilenamehere`
// args specified are -f and -v
string[] arguments = new[] {"-f file:xxxx\\xxxx\\xxxxx.sh", "-v nsdd" };
string file = string.Empty;
string value = string.Empty;
// you would pull your args off the options, if they are successfully parsed
// and map them to your applications properties/settings
Parser.Default.ParseArguments<Options>(arguments)
.WithParsed<Options>(o =>
{
file = o.InputFile; // map InputFile arg to file property
value = o.Value; // map Value arg to value property
});
Console.WriteLine($"file = {file}");
Console.WriteLine($"value = {value}");
Console.ReadLine();
// output:
// file = file:xxxx\xxxx\xxxxx.sh
// value = nsdd
}
}
// the options class is used to define your arg tokens and map them to the Options property
class Options
{
[Option('f', "file", Required = true, HelpText = "Input files to be processed.")]
public string InputFile { get; set; }
[Option('v', "value", Required = true, HelpText = "Value to be used")]
public string Value { get; set; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.