Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用C命令行库解析命令行参数_C#_.net_Parsing_Command Line_Command Line Arguments - Fatal编程技术网

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

C# 使用C命令行库解析命令行参数,c#,.net,parsing,command-line,command-line-arguments,C#,.net,Parsing,Command Line,Command Line Arguments,我正在使用nuget的命令行库V1.9.71.2。不幸的是,文档不是最新的,我不理解这个库使用的高级C语言结构,所以我不能仅仅通过观察库中可用的接口来提出有效的解决方案 我的选项类如下所示: class ProgramOptions { [Option('l', "spotsL", Required = true, HelpText = "Lowest stock price used for calculations.")] public double lowestSpot {

我正在使用nuget的命令行库V1.9.71.2。不幸的是,文档不是最新的,我不理解这个库使用的高级C语言结构,所以我不能仅仅通过观察库中可用的接口来提出有效的解决方案

我的选项类如下所示:

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; }
}
不幸的是,它不起作用,在is_success变量中给出False。所有其他变量显示为0。 我的命令行参数可以让库解析如下内容:

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");
    }
}
/spotsL 10 /spotsH 1000 /spotsN 9 /maturities 1,0.5,0.001 /strike 495 /vol 0.1

10确实是由te first WriteLine显示的。

我对命令行库不太熟悉,但快速查看一下,它看起来需要使用单破折号-用于单字符参数或双破折号-用于多字符参数,即

--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);
}

非常感谢。我忽略了这个。在将/to改为-it之后,它确实正确地解析了输入,但问题是我需要使用/-这是我的规范。IK我可以通过在参数字符串中用双破折号替换斜杠来解决这个问题,但它似乎并不正确。。。有什么想法吗?库似乎不允许感知字符配置。当然,所有剩余的问题仍然存在,我将非常感谢您的帮助。该库似乎相当有限-不幸的是,如果不像您所说的那样手动转换/到,似乎无法做到这一点。我用一种你可以做到的方法编辑了我的答案。很抱歉,似乎没有更好的解决方案。我想这是解决这个问题的唯一可能的办法。你知道用空值来禁用短选项吗?我还想,如果到期日列表是双倍列表,而不是字符串,我需要创建另一个包含所需双倍列表的对象,并将前者转换为后者?有什么简单的方法可以合并多态性或类似的东西吗?或者我应该简单地创建另一个类,重复每个属性,除了将创建不同类型的ouf的列表之外?