C#相当于Perl的Getopt :: Long

时间:2016-06-23 14:05:05

标签: c# perl command-line-arguments long-integer getopt

我正在努力将一些旧的Perl脚本转换为C#,我需要保持功能基本相同。为了做到这一点,我需要解析命令行,类似于Perl Getopt::Long的工作方式,但我很难弄清楚如何做到这一点。如果有人能让我知道这样做的好方法或指出我的方向,我将不胜感激。

1 个答案:

答案 0 :(得分:2)

Jonathon Pryor用同样的动机写了mono.Options,如他的博客文章所示:http://www.jprl.com/Blog/archive/development/mono/2008/Jan-07.html

我在几个项目中使用它,保留所有归因注释,并将文件放在我的项目目录中。对我很有用。

我在这里工作:

string iniFilePath = null;

OptionSet os = new OptionSet()
    { { "ini="
      , "Valid path to an ini file."
      , (s) => { iniFilePath = s; }
      } };
List<string> extra;
try  {
    extra = os.Parse( Application.CommandLineArguments );
    return iniFilePath;
}
catch ( OptionException oex ) {
    Library.ExceptionHandler.handle( oex );
}
return null;
相关问题