From c4cdef998116d994c41f29b48829590447187de9 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 10 Apr 2026 20:45:53 +0000 Subject: [PATCH 1/5] Initial plan From 310a690f5342f5d290e4330a35be315ad514ad78 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 10 Apr 2026 20:49:11 +0000 Subject: [PATCH 2/5] Fix DefaultValueFactory misuse in System.CommandLine get-started tutorial Agent-Logs-Url: https://github.com/dotnet/docs/sessions/4b7b37c1-c6d1-4827-ba43-221d160d501a Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com> --- .../commandline/get-started-tutorial.md | 7 +++---- .../csharp/Stage3/Program.cs | 20 +++++++++---------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/docs/standard/commandline/get-started-tutorial.md b/docs/standard/commandline/get-started-tutorial.md index d0bfd241287f0..db414ab8d8e98 100644 --- a/docs/standard/commandline/get-started-tutorial.md +++ b/docs/standard/commandline/get-started-tutorial.md @@ -340,11 +340,10 @@ scl quotes delete --search-terms David "You can do" Antoine "Perfection is achie :::code language="csharp" source="snippets/get-started-tutorial/csharp/Stage3/Program.cs" id="fileoption" ::: - This code uses to provide custom parsing, validation, and error handling. + This code uses two separate mechanisms: - Without this code, missing files are reported with an exception and stack trace. With this code just the specified error message is displayed. - - This code also specifies a default value, which is why it sets to custom parsing method. + * provides the default `sampleQuotes.txt` value when the user doesn't provide `--file`, and converts the provided token to a `FileInfo` when they do. + * adds a custom validator that runs *after* the value is parsed, regardless of whether the user provided a value or the default was used. Without the validator, a missing file would cause an unhandled `FileNotFoundException` with a stack trace. With the validator, just the specified error message is displayed. 1. After the code that creates `lightModeOption`, add options and arguments for the `add` and `delete` commands: diff --git a/docs/standard/commandline/snippets/get-started-tutorial/csharp/Stage3/Program.cs b/docs/standard/commandline/snippets/get-started-tutorial/csharp/Stage3/Program.cs index f0f96143fd75b..5ec25616f2a77 100644 --- a/docs/standard/commandline/snippets/get-started-tutorial/csharp/Stage3/Program.cs +++ b/docs/standard/commandline/snippets/get-started-tutorial/csharp/Stage3/Program.cs @@ -17,20 +17,18 @@ static int Main(string[] args) if (result.Tokens.Count == 0) { return new FileInfo("sampleQuotes.txt"); - - } - string filePath = result.Tokens.Single().Value; - if (!File.Exists(filePath)) - { - result.AddError("File does not exist"); - return null; - } - else - { - return new FileInfo(filePath); } + return new FileInfo(result.Tokens.Single().Value); } }; + fileOption.Validators.Add(result => + { + var file = result.GetValueOrDefault(); + if (file is not null && !file.Exists) + { + result.AddError("File does not exist"); + } + }); // Option delayOption = new("--delay") From 13b850d23bf4ba57781d724e86d76a2b5a31fbc4 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 10 Apr 2026 21:02:48 +0000 Subject: [PATCH 3/5] Simplify DefaultValueFactory to default-only; fix doc bullet to reflect actual behavior Agent-Logs-Url: https://github.com/dotnet/docs/sessions/b9fafcae-b41f-4097-a3e1-a91119d26d47 Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com> --- docs/standard/commandline/get-started-tutorial.md | 4 ++-- .../get-started-tutorial/csharp/Stage3/Program.cs | 9 +-------- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/docs/standard/commandline/get-started-tutorial.md b/docs/standard/commandline/get-started-tutorial.md index db414ab8d8e98..8200c03c60fd2 100644 --- a/docs/standard/commandline/get-started-tutorial.md +++ b/docs/standard/commandline/get-started-tutorial.md @@ -342,8 +342,8 @@ scl quotes delete --search-terms David "You can do" Antoine "Perfection is achie This code uses two separate mechanisms: - * provides the default `sampleQuotes.txt` value when the user doesn't provide `--file`, and converts the provided token to a `FileInfo` when they do. - * adds a custom validator that runs *after* the value is parsed, regardless of whether the user provided a value or the default was used. Without the validator, a missing file would cause an unhandled `FileNotFoundException` with a stack trace. With the validator, just the specified error message is displayed. + * supplies `sampleQuotes.txt` as the default when you don't provide `--file`. When you do provide `--file`, the option's built-in parser converts the token to a `FileInfo` directly — `DefaultValueFactory` doesn't run. + * adds a custom validator that runs *after* the value is parsed, whether you supplied a path or the default was used. Without the validator, a missing file would cause an unhandled `FileNotFoundException` with a stack trace. With the validator, just the specified error message is displayed. 1. After the code that creates `lightModeOption`, add options and arguments for the `add` and `delete` commands: diff --git a/docs/standard/commandline/snippets/get-started-tutorial/csharp/Stage3/Program.cs b/docs/standard/commandline/snippets/get-started-tutorial/csharp/Stage3/Program.cs index 5ec25616f2a77..c2790a0fc1ad3 100644 --- a/docs/standard/commandline/snippets/get-started-tutorial/csharp/Stage3/Program.cs +++ b/docs/standard/commandline/snippets/get-started-tutorial/csharp/Stage3/Program.cs @@ -12,14 +12,7 @@ static int Main(string[] args) { Description = "An option whose argument is parsed as a FileInfo", Required = true, - DefaultValueFactory = result => - { - if (result.Tokens.Count == 0) - { - return new FileInfo("sampleQuotes.txt"); - } - return new FileInfo(result.Tokens.Single().Value); - } + DefaultValueFactory = _ => new FileInfo("sampleQuotes.txt") }; fileOption.Validators.Add(result => { From 4dfbef1404aacca3f510eb75ddb214b696cc5209 Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Fri, 10 Apr 2026 14:04:00 -0700 Subject: [PATCH 4/5] Update docs/standard/commandline/get-started-tutorial.md --- docs/standard/commandline/get-started-tutorial.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/standard/commandline/get-started-tutorial.md b/docs/standard/commandline/get-started-tutorial.md index 8200c03c60fd2..82d495f8ee6e7 100644 --- a/docs/standard/commandline/get-started-tutorial.md +++ b/docs/standard/commandline/get-started-tutorial.md @@ -343,7 +343,7 @@ scl quotes delete --search-terms David "You can do" Antoine "Perfection is achie This code uses two separate mechanisms: * supplies `sampleQuotes.txt` as the default when you don't provide `--file`. When you do provide `--file`, the option's built-in parser converts the token to a `FileInfo` directly — `DefaultValueFactory` doesn't run. - * adds a custom validator that runs *after* the value is parsed, whether you supplied a path or the default was used. Without the validator, a missing file would cause an unhandled `FileNotFoundException` with a stack trace. With the validator, just the specified error message is displayed. + * adds a custom validator that runs *after* the value is parsed, whether you supplied a path or the default was used. Without the validator, a missing file would cause an unhandled `FileNotFoundException` with a stack trace. With the validator, just the specified error message is displayed. 1. After the code that creates `lightModeOption`, add options and arguments for the `add` and `delete` commands: From 0595bafc684f7874cd500f065e397591fa4dbe11 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 10 Apr 2026 21:13:04 +0000 Subject: [PATCH 5/5] Use CustomParser for token-to-FileInfo conversion and validation; DefaultValueFactory for default only Agent-Logs-Url: https://github.com/dotnet/docs/sessions/e25acb40-859f-466e-849f-81c336ec617b Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com> --- .../commandline/get-started-tutorial.md | 4 ++-- .../csharp/Stage3/Program.cs | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/docs/standard/commandline/get-started-tutorial.md b/docs/standard/commandline/get-started-tutorial.md index 82d495f8ee6e7..7e943ade204d0 100644 --- a/docs/standard/commandline/get-started-tutorial.md +++ b/docs/standard/commandline/get-started-tutorial.md @@ -342,8 +342,8 @@ scl quotes delete --search-terms David "You can do" Antoine "Perfection is achie This code uses two separate mechanisms: - * supplies `sampleQuotes.txt` as the default when you don't provide `--file`. When you do provide `--file`, the option's built-in parser converts the token to a `FileInfo` directly — `DefaultValueFactory` doesn't run. - * adds a custom validator that runs *after* the value is parsed, whether you supplied a path or the default was used. Without the validator, a missing file would cause an unhandled `FileNotFoundException` with a stack trace. With the validator, just the specified error message is displayed. + * supplies `sampleQuotes.txt` as the default when you don't provide `--file`. `DefaultValueFactory` doesn't run when you do provide `--file`. + * runs when you explicitly provide `--file`. It converts the token to a `FileInfo` and validates that the file exists. Without validation, a missing file would cause an unhandled `FileNotFoundException` with a stack trace. With validation, just the specified error message is displayed. 1. After the code that creates `lightModeOption`, add options and arguments for the `add` and `delete` commands: diff --git a/docs/standard/commandline/snippets/get-started-tutorial/csharp/Stage3/Program.cs b/docs/standard/commandline/snippets/get-started-tutorial/csharp/Stage3/Program.cs index c2790a0fc1ad3..62a291c6179c8 100644 --- a/docs/standard/commandline/snippets/get-started-tutorial/csharp/Stage3/Program.cs +++ b/docs/standard/commandline/snippets/get-started-tutorial/csharp/Stage3/Program.cs @@ -11,17 +11,17 @@ static int Main(string[] args) Option fileOption = new("--file") { Description = "An option whose argument is parsed as a FileInfo", - Required = true, - DefaultValueFactory = _ => new FileInfo("sampleQuotes.txt") - }; - fileOption.Validators.Add(result => - { - var file = result.GetValueOrDefault(); - if (file is not null && !file.Exists) + DefaultValueFactory = _ => new FileInfo("sampleQuotes.txt"), + CustomParser = result => { - result.AddError("File does not exist"); + var file = new FileInfo(result.Tokens.Single().Value); + if (!file.Exists) + { + result.AddError("File does not exist"); + } + return file; } - }); + }; // Option delayOption = new("--delay")