Skip to content

[PROPOSED] Unify CustomParser and DefaultValueFactory#2791

Draft
jonsequitur wants to merge 1 commit intodotnet:mainfrom
jonsequitur:custom-parsing-redesign
Draft

[PROPOSED] Unify CustomParser and DefaultValueFactory#2791
jonsequitur wants to merge 1 commit intodotnet:mainfrom
jonsequitur:custom-parsing-redesign

Conversation

@jonsequitur
Copy link
Copy Markdown
Contributor

Prior to the release of System.CommandLine 2.0.0, we found a number of things about the Argument/Option.CustomParser and Argument/Option.DefaultValueFactory API design to be awkward.

They have nearly identical signatures but one (DefaultValueFactory) is called only when no tokens are matched for the symbol, while the other (CustomParser) is called only when tokens are matched for the symbol. This was a regression from the original design where custom parsers were able to fulfill both of these roles at once. With the current API, if you want to use the same logic for both of these cases, you need to assign the same delegate to both properties.

The goal of this proposed API is to unify this functionality and, hopefully, to make the usage clearer.

Please let me know what you think. Alternative proposals are welcome. Consider this PR an example to start conversation over how to improve this area of the System.CommandLine API.

@jonsequitur jonsequitur force-pushed the custom-parsing-redesign branch from 9197564 to b3f7a8c Compare April 2, 2026 20:27
@jonsequitur jonsequitur force-pushed the custom-parsing-redesign branch from b3f7a8c to f29df73 Compare April 2, 2026 20:31
/// Specifies when a value factory should be invoked.
/// </summary>
[Flags]
public enum ValueFactoryInvocation
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This clearly needs a better name. It indicates when the value factory should be called, based on whether or not a symbol has matching tokens.

@KalleOlaviNiemitalo
Copy link
Copy Markdown

I find it handy that I can have a custom parser dedicated to a type but agnostic of default values; that also lets me use the same parser for multiple options with different default values. If the properties were merged again, then I'd instead have to make the parser method take the default value as a parameter, so its signature would no longer match that of the delegate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants