Unparsing skips arguments with values equal to the argument type's default value
還沒有人認領這個 Issue。
評估
研究方向
使用提供的 Program 和 TestArgs 應用程式重現問題,重點關注 settings.SkipDefault = true 時的 Parser.FormatCommandLine。將它對值 0、1 和 2 的輸出與預期的命令列進行比較,然後確認每個結果經過往返解析後都能還原為原始值。
由索引模型根據 Issue 內容生成。
描述
Using CommandLineParser 2.9.1, the unparsing behaviour with SkipDefault = true seems to be incorrect. Here is a test app to reproduce the behaviour.
using CommandLine;
namespace UnparsingTest
{
public class Program
{
public class TestArgs
{
[Option('c', Required = true)]
public int CompulsoryWithoutDefault { get; set; }
[Option('d', Required = true, Default = 1)]
public int CompulsoryWithDefault { get; set; } = 1;
[Option('o', Required = false, Default = 1)]
public int Optional { get; set; } = 1;
}
static void Main()
{
Parser parser = new Parser();
for (int i = 0; i <= 2; ++i)
{
TestArgs args = new TestArgs()
{
CompulsoryWithoutDefault = i,
CompulsoryWithDefault = i,
Optional = i,
};
string commandLine = parser.FormatCommandLine(args, settings => settings.SkipDefault = true);
System.Console.WriteLine($"CompulsoryWithDefault: {args.CompulsoryWithDefault}, CompulsoryWithoutDefault: {args.CompulsoryWithoutDefault}, Optional: {args.Optional}, Command line: \"{commandLine}\"");
}
}
}
}
There are 2 compulsory arguments, one with a Default value and one without a Default value. CompulsoryWithDefault and Optional have their Default values set to any value that is not default(int).
The test app's console output shows the unparsed command line for the 3 sets of arguments
CompulsoryWithDefault: 0, CompulsoryWithoutDefault: 0, Optional: 0, Command line: ""
CompulsoryWithDefault: 1, CompulsoryWithoutDefault: 1, Optional: 1, Command line: "-c 1"
CompulsoryWithDefault: 2, CompulsoryWithoutDefault: 2, Optional: 2, Command line: "-c 2 -d 2 -o 2"
The unparsed command line for the first case will fail round trip parsing because the compulsory -c and -d arguments are missing and -o is missing so Optional will have a value of 1 instead of 0. The unparsed command line for the second case will also fail round trip parsing because the compulsory -d argument is missing. Only the third case is giving correct output.
The bug appears to be that the unparser is skipping Required = true arguments, arguments with value equal to Default = {value} and arguments with value equal to default(typeof(argument)).
The correct behaviour would be to only skip arguments that do not have Required = true and whose value equals the value specified in Default = {value}.
The expected console output would be
CompulsoryWithDefault: 0, CompulsoryWithoutDefault: 0, Optional: 0, Command line: "-c 0 -d 0 -o 0"
CompulsoryWithDefault: 1, CompulsoryWithoutDefault: 1, Optional: 1, Command line: "-c 1 -d 1"
CompulsoryWithDefault: 2, CompulsoryWithoutDefault: 2, Optional: 2, Command line: "-c 2 -d 2 -o 2"
These command lines would then correctly round trip parse/unparse.
- 主要語言
- C#
- 星號
- 4.8k
- 分支
- 478
- PR 合併指標
- 30 天內沒有已合併 PR
貢獻指南
這個儲存庫沒有索引到貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
commandlineparser/commandline 的其他 Issue
-
難度 2/5 1-3 小時 新手友好度 68/100
commandlineparser/commandline#951 · 1 則留言 ·
-
難度 1/5 1 小時以內 新手友好度 70/100
commandlineparser/commandline#880 · 2 個 reaction ·
-
難度 5/5 一週以上 新手友好度 45/100
commandlineparser/commandline#952 ·
-
難度 3/5 1-2 天 新手友好度 35/100
commandlineparser/commandline#948 ·
-
難度 5/5 一週以上 新手友好度 35/100
commandlineparser/commandline#947 ·
查看 commandlineparser/commandline 的全部 Issue
相似的 Issue
-
bug frontend good first issue
難度 2/5 1-3 小時 新手友好度 75/100
-
NavigationViewItemAutomationPeer implements IInvokeProvider but never advertises the Invoke pattern 未關閉
難度 2/5 1-3 小時 新手友好度 75/100
unoplatform/uno#24629 ·
-
agentic-workflows Needs: Triage :mag: State: In-PR
難度 2/5 1-3 小時 新手友好度 70/100
-
bug
難度 1/5 1 小時以內 新手友好度 90/100
-
Type: enhancement
難度 2/5 1-3 小時 新手友好度 65/100
apache/arrow-adbc#4809 ·