Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Unparsing skips arguments with values equal to the argument type's default value

未关闭
#850 5 条评论 3 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
csharp
领域
cli

调研方向

使用提供的 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

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

commandlineparser/commandline 的其他 Issue

查看 commandlineparser/commandline 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。