gpt4 book ai didi

c# - 命令行解析器 NUGet 包使简单的示例程序工作

转载 作者:行者123 更新时间:2023-12-04 10:59:14 25 4
gpt4 key购买 nike

我发现这非常具有挑战性,我感谢您愿意为我提供的任何帮助。
目前我正在尝试实现命令行解析器( https://github.com/commandlineparser/commandline )。
我只想让一个基本的示例应用程序工作,但我被卡住了。
最终我想要以下模式
MyProgram -soureid 1231 -domain alpha
我在哪里获得 sourceid 和 domain 作为有效变量。 sourceid 的值为 1231,domain 的值为“alpha”。
这是一个 C# .net 核心应用程序 (2.3),我正在运行 Visual Studio 2017。
这是我到目前为止的代码......

    using System;
using CommandLine;

namespace Program
{
class Program
{
public static void Main(String[] args)
{
var options = new SomeOptions();

CommandLine.Parser.Default.ParseArguments(args, typeof(SomeOptions));

Console.WriteLine(options.Age);

Console.ReadLine();
}
}
class SomeOptions
{
[Option('n', "name", Required = true)]
public string Name { get; set; }

[Option('a', "age")]
public int Age { get; set; }
}
}
此代码不起作用。当我通过 -n Jason 我得到这个..
CommandLineArgumentParsing 1.0.0
Copyright (C) 2019 CommandLineArgumentParsing

ERROR(S):
Verb '-n' is not recognized.

--help Display this help screen.

--version Display version information.

0
我相信这个问题与这条线有关..
 CommandLine.Parser.Default.ParseArguments(args, typeof(SomeOptions));
这条线好像应该是这样..
 CommandLine.Parser.Default.ParseArguments(args, typeof(options));
然而,编译器提示“'options' 是一个变量,但像类型一样使用”
我究竟做错了什么?

最佳答案

在我问这个问题大约两秒钟后,我想通了这一点..

代替..

CommandLine.Parser.Default.ParseArguments(args, typeof(SomeOptions));

和...
Parser.Default.ParseArguments<SomeOptions>(args).WithParsed(parsed => options = parsed);

关于c# - 命令行解析器 NUGet 包使简单的示例程序工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58941148/

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