c# - 使用 C# CommandLine 库解析命令行参数

标签 c# .net parsing command-line command-line-arguments

我正在使用来自 nuget 的命令行库 v 1.9.71.2。不幸的是,文档不是最新的,而且我不了解用于此库的高级 C# 语言结构,因此我无法仅通过查看库中可用的接口(interface)来提出可行的解决方案。

我的选项类如下所示:

class ProgramOptions
{
    [Option('l', "spotsL", Required = true, HelpText = "Lowest stock price used for calculations.")]
    public double lowestSpot { get; set; }

    [Option('h', "spotsH", Required = true, HelpText = "Highest stock price used for calculations.")]
    public double highestSpot { get; set; }

    [Option('n', "spotsN", Required = true, HelpText = "Number of (equally distributed) stock prices [spotsL,spotsH].")]
    public int spotsNumber { get; set; }

    [OptionList('m', "maturities", ',', Required = true, HelpText = "Comma separated list of options maturities (as a fraction of a year).")]
    public IList<string> maturities { get; set; } //we want double here.

    [Option('s', "strike", Required = true, HelpText = "Strike price of options.")]
    public double strikePrice { get; set; }

    [Option('v', "vol", Required = true, HelpText = "Volatility used for calculation.")]
    public double volatility { get; set; }
}

我只需要选项的长名称,但是当我输入 null 时的(如文档所示)代替短名称我得到编译器错误。我也更喜欢将到期时间作为 double 列表( IList<double> maturities ),但 IDK 如何实现这一点 - 我认为使用纯 CommandLine 它仅适用于 string 列表

第二部分是我无法从命令行解析选项 argsProgramOptions目的。正在尝试类似的东西:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(args[1]);
        ///Parse Command Line
        var options = new ProgramOptions();
        bool is_succes = Parser.Default.ParseArguments(args, options);
        Console.WriteLine("parsed? {0}",is_succes);

        Console.WriteLine(options.highestSpot);
        Console.WriteLine(options.lowestSpot);
        Console.WriteLine(options.maturities.ToString());
        Console.WriteLine(options.spotsNumber);
        Console.WriteLine(options.strikePrice);
        Console.WriteLine(options.volatility);
        Console.WriteLine("program working");
    }
}

不幸的是它不起作用并给出 Falseis_succes多变的。所有其他变量显示 0 . 我的命令行参数是任何让库解析类似内容的机会:

/spotsL 10 /spotsH 1000 /spotsN 9 /maturities 1,0.5,0.001 /strike 495 /vol 0.1

10确实是te先显示的WriteLine .

最佳答案

我不太熟悉 CommandLine 库,但快速浏览了 documentation , 看起来您需要为单字符参数使用单破折号 (-) 或为多字符参数使用双破折号 (--),即

--spotsL 10 --spotsH 1000 --spotsN 9 --maturities 1,0.5,0.001 --strike 495 --vol 0.1

编辑:如果您确实需要输入参数有斜杠,您需要将它们转换为破折号:

public static void FormatArguments(string[] args)
{
    for (int i = 0; i < args.Length; i++)
        if (args[i].StartsWith("/"))
            args[i] = "--" + args[i].Substring(1);
}

关于c# - 使用 C# CommandLine 库解析命令行参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26834523/

相关文章:

c# - 如果我在 C# 中重载添加和删除,为什么我不能引发或调用事件

c# - 处理 URL 和获取数据

c# - 为引用对象指定 Entity Framework 中的列名称

parsing - ANTLR:自定义语法示例的词法错误帮助

c++ - 在 C++ 中解析和比较 IP 地址

Python feedparser 返回第一个条目中第一个媒体项的 URL

c# - Winforms Devexpress 应用部署

c# - ASP.Net c# 选择了给定 GroupName 中的哪个单选按钮?

c# - 为什么我通过 WCF Resful 服务在 "405 Method not allowed"操作上收到 "Put"错误?

c# - 提高 ASP.NET 的性能,使其能够被许多用户访问