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

A random argument value is passed in place of an optional argument

未关闭
#2,502 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

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

调研方向

使用报告中显示的 C# 命令重现该问题,然后跟踪省略的可选 Argument 如何绑定到 handler,以及如何获取其默认值。当 arg4 为 null,或调用引发明确的运行时异常,并且有一个涵盖省略最后一个参数的回归测试时,即视为完成。

由索引模型根据 Issue 内容生成。

描述

This very simple command takes four arguments, the first three being mandatory.

When the last argument (arg4) is omitted, the handler receives the value of arg2 instead of null.

var root = new RootCommand();
var arg1 = new Argument<string>("arg1") { Arity = ArgumentArity.ExactlyOne };
var arg2 = new Argument<string>("arg2") { Arity = ArgumentArity.ExactlyOne };
var arg3 = new Argument<string>("arg3") { Arity = ArgumentArity.ExactlyOne };
var arg4 = new Argument<string>("arg4") { Arity = ArgumentArity.ZeroOrOne };
root.AddArgument(arg1);
root.AddArgument(arg2);
root.AddArgument(arg3);
root.AddArgument(arg4);

root.SetHandler((v1, v2, v3, v4) =>
{
    Console.WriteLine($"arg1: {v1}\narg2: {v2}\narg3: {v3}\narg4: {v4}");
}, arg1, arg2, arg3, arg4);

root.Invoke(["one", "TWO", "three"]);

This prints:

arg1: one
arg2: TWO
arg3: three
arg4: TWO

Expected: arg4 = null. Or, if I misunderstood how to set up such a syntax, a runtime exception. But certainly not "TWO".

It's possible to work around this by adding an explicit default to arg4:

var arg4 = new Argument<string>("arg4", () => null) { Arity = ArgumentArity.ZeroOrOne };

Version: 2.0.0-beta4.22272.1

主要语言
C#
星标
3.7k
派生
434
平均合并
4 小时 46 分钟
30 天内合并 PR
1

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

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

dotnet/command-line-api 的其他 Issue

查看 dotnet/command-line-api 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

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