gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:02:31 25 4
gpt4 key购买 nike

我正在使用来自 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com