Home >Backend Development >C++ >How to Efficiently Parse Command-Line Arguments in C#?
The efficient analysis method of the line parameters of the command line
When developing a console application that accepts command line parameters, it is important to effectively use parameters passed to
. Many developers initially used indexes and circulating technical processing parameters. However, as the command structure becomes more and more complicated, this method will cause the code to become awkward.
Main(string[] args)
In order to cope with this challenge, exploring effective libraries and models is very important:
Library:
This code defines four options:
: Accept the string parameters and add it to the names list.
<code class="language-csharp">bool show_help = false; List<string> names = new List<string>(); int repeat = 1; var p = new OptionSet() { { "n|name=", "问候对象的姓名。", v => names.Add(v) }, { "r|repeat=", "重复问候的次数(必须为整数)。", (int v) => repeat = v }, { "v", "提高调试消息的详细程度", v => { if (v != null) ++verbosity; } }, { "h|help", "显示此消息并退出", 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("尝试 `greet --help` 获取更多信息。"); return; }</code>: Accept the integer parameter and assign it to the Repeat variable.
: If specified, the Verbosity variable is increased (no value).
"n|name="
"r|repeat="
The above is the detailed content of How to Efficiently Parse Command-Line Arguments in C#?. For more information, please follow other related articles on the PHP Chinese website!