gpt4 book ai didi

C# commandline parser : produces BadFormatConversionError when a bool option is given without value(C#命令行解析器:当给定一个没有值的bool选项时,会产生BadFormatConversionError)

转载 作者:bug小助手 更新时间:2023-10-22 13:50:45 28 4
gpt4 key购买 nike



I'm trying to specify a flag using the CommandLineParser library

我正在尝试使用CommandLineParser库指定一个标志


[Option('d', "content", Required = false,  HelpText = "<Content file>\n (Mandatory) Path to the content file.")]
public String? ContentFile { get; set; }

[Option('e', "extend", Required = false, Default = false, HelpText = "Whether it is an extend operation or not")]
public bool? Extend { get; set; }

Execution command
\a.exe --extend --content kdssign.deps.json

执行命令\a.exe--extend--content kdssign.deps.json


If I move "--extend" to end then Execption is not thrown but still Extend varaible is not set.

如果我将“--extend”移动到末尾,则不会引发Exection,但仍然没有设置extend varible。


My Expectation is iff --extend is provided from command line then it should be set without waiting for any value to be provided.

我的期望是iff——extend是从命令行提供的,那么它应该在不等待提供任何值的情况下进行设置。


更多回答

Which command-line parser? There are a lot of CLI parsers written in C#. .NET's own System.CommandLine doesn't use attributes

哪个命令行解析器?有很多CLI解析器是用C#编写的。NET自己的System.CommandLine不使用属性

Are you sure the flag isn't set to the type's default, ie null? Have you tried with public bool Extend? After all, if the absence of the flag means it's false, the property should never be null. BTW the Quickstart example uses public bool Verbose { get; set; } with the Required=false to specify a flag. false is already the default

你确定标志没有设置为类型的默认值(null)吗?你试过公共bool Extend吗?毕竟,如果没有标志意味着它是false,那么该属性永远不应该为null。顺便说一句,Quickstart示例使用公共bool Verbose{get;set;}和Required=false来指定标志。false已经是默认值

I think issue was setting extend as nullable only. Once I removed nullable feature it started working thanks.

我认为问题是将extend设置为只能为null。一旦我删除了可以为null的功能,它就开始工作了,谢谢。

优秀答案推荐

Use public bool Extend { get; set; } instead of bool. Flags in CommandLineParser are supported through bool properties. If the flag is missing, the property will be false by default. A bool? on the other hand can have three values: the default null, true or false.

使用公共布尔扩展{get;set;}而不是布尔。CommandLineParser中的标志通过布尔属性得到支持。如果缺少标志,则默认情况下该属性为false。嘘声?另一方面可以有三个值:默认的null、true或false。


The library's quickstart example shows how to use a flag:

库的快速启动示例显示了如何使用标志:


public class Options
{
[Option('v', "verbose", Required = false, HelpText = "Set output to verbose messages.")]
public bool Verbose { get; set; }
}

static void Main(string[] args)
{
Parser.Default.ParseArguments<Options>(args)
.WithParsed<Options>(o =>
{
if (o.Verbose)
{
Console.WriteLine($"Verbose output enabled. Current Arguments: -v {o.Verbose}");
Console.WriteLine("Quick Start Example! App is in Verbose mode!");
}
else
{
Console.WriteLine($"Current Arguments: -v {o.Verbose}");
Console.WriteLine("Quick Start Example!");
}
});
}

更多回答

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