我有一个名为MyTool.exe的控制台应用程序
收集传递给该控制台应用程序的命名参数的最简单方法是将其放在Dictionarty< string,string>()中,该参数的名称作为关键字,值作为参数?
例如:
MyTool foo=123432 bar=Alora barFoo=45.9
我应该能够获得一个字典:
MyArguments["foo"]=123432 MyArguments["bar"]="Alora" MyArguments["barFoo"]="45.9"
解决方法
使用这个Nuget软件包
Command Line Parser Library.需要几秒钟来配置和添加即时专业的触摸到您的应用程序.
// Define a class to receive parsed values class Options { [Option('r',"read",required = true,HelpText = "Input file to be processed.")] public string InputFile { get; set; } [Option('v',"verbose",DefaultValue = true,HelpText = "Prints all messages to standard output.")] public bool Verbose { get; set; } [ParserState] public IParserState LastParserState { get; set; } [HelpOption] public string GetUsage() { return HelpText.AutoBuild(this,(HelpText current) => HelpText.DefaultParsingErrorsHandler(this,current)); } } // Consume them static void Main(string[] args) { var options = new Options(); if (CommandLine.Parser.Default.ParseArguments(args,options)) { // Values are available here if (options.Verbose) Console.WriteLine("Filename: {0}",options.InputFile); } }