简体   繁体   English

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

[英]Parsing command line arguments using C# CommandLine library

I am using CommandLine library v 1.9.71.2 from nuget. 我正在使用来自nuget的CommandLine库v 1.9.71.2。 Unfortunately documentation is not up to date and I don't understand advanced C# language constructions used for this library, so I couldn't come up with working solution just by watching interface available in library. 不幸的是,文档不是最新的,并且我不了解该库使用的高级C#语言构造,因此我无法仅通过查看库中可用的接口来提出有效的解决方案。

My options class looks like this: 我的选项类如下所示:

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

I need only long names of the options, but when I put null 's (as documentation suggests) in place of short names I get compiler errors. 我只需要选项的长名称,但是当我将null的名称(如文档所示)代替短名称时,会出现编译器错误。 I'd also prefer to have maturities as a list of doubles ( IList<double> maturities ), but IDK how to achieve this - i think with pure CommandLine it works only for list of string s. 我也希望将成熟度作为IList<double> maturities精度项列表( IList<double> maturities ),但是IDK如何实现这一点-我认为使用纯CommandLine只能用于string s列表。

Second part is that I can't parse options from command line args to the ProgramOptions object. 第二部分是我无法从命令行args解析选项到ProgramOptions对象。 Was trying something like: 正在尝试类似:

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

Unfortunately it doesn't work and gives False in is_succes variable. 不幸的是,它不起作用,并且在is_succes变量中给出False All other variables display 0 . 所有其他变量显示为0 My command line arguments were 我的命令行参数是 Any chance to get the library to parse something like: 有机会解析该库的任何机会:

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

and 10 is indeed displayed by te first WriteLine . 10确实是由first WriteLine显示的。

I'm not too familiar with the CommandLine library, but from a quick look at the documentation , it looks like you need to use a single dash ( - ) for single-character arguments or a double dash ( -- ) for multiple-character arguments, ie 我不太熟悉CommandLine库,但是从文档的快速浏览来看,您似乎需要为单字符参数使用单破折号( - )或对于多字符使用双破折号( -- )。参数,即

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

Edit: if you really need the input arguments to have slashes, you need to convert them to dashes: 编辑:如果您确实需要输入参数具有斜线,则需要将它们转换为破折号:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM