Fix file-existence validation bypassed when --file is provided explicitly#52974
Fix file-existence validation bypassed when --file is provided explicitly#52974
Conversation
…rial Agent-Logs-Url: https://github.com/dotnet/docs/sessions/4b7b37c1-c6d1-4827-ba43-221d160d501a Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Fixes a validation gap in the System.CommandLine “get started” tutorial where file-existence checks placed in DefaultValueFactory were bypassed when --file is explicitly provided, resulting in an unhandled FileNotFoundException instead of a parse error.
Changes:
- Moves file-existence validation to
fileOption.Validators.Add(...)so it runs for both default and user-supplied values. - Updates the tutorial text to describe the split between defaulting/parsing and validation.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
docs/standard/commandline/snippets/get-started-tutorial/csharp/Stage3/Program.cs |
Refactors option setup by separating default value creation from file-existence validation via validators. |
docs/standard/commandline/get-started-tutorial.md |
Revises the tutorial explanation to match the intended validation mechanism. |
docs/standard/commandline/snippets/get-started-tutorial/csharp/Stage3/Program.cs
Outdated
Show resolved
Hide resolved
…ct 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>
…aultValueFactory 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>
Head branch was pushed to by a user without write access
|
Checked this PR. This resolves the issue I reported. Validation is no longer tied to Execution methods should still handle I/O failures independently but for tutorial illustration this fixes it. |
DefaultValueFactoryonly runs when no value is provided, so placing file-existence validation there silently bypasses it when the user supplies--file nofile, causing an unhandledFileNotFoundExceptioninstead of the expected error message.Changes
Stage3/Program.cs— Uses two properties together to correctly separate concerns:DefaultValueFactorysupplies only the defaultsampleQuotes.txtvalue when--fileis omittedCustomParserhandles token→FileInfoconversion and file-existence validation when--fileis explicitly provided;Required = trueis removed sinceDefaultValueFactoryprovides the fallbackget-started-tutorial.md— Updates the step explanation to accurately describe each mechanism using second-person voice:DefaultValueFactorysupplies the default when you don't provide--file(and doesn't run when you do);CustomParserhandles conversion and validation when you do provide itFixes #52979.
Internal previews