You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
add Uri to ArgumentConverter.StringConverters #2574
an InvalidOperationException is thrown, with the message:
Cannot parse argument 'http://example.com' for option '--url' as expected type 'System.Uri'.
Reacted by Ilona Tomkowicz, Maleaume and Anton Egorov
publicclassExampleCommand:Command{publicExampleCommand():base("example"){Option<string>urlOption=new("--url"){Required=true,};urlOption.Validators.Add(result =>{varurl=result.GetRequiredValue(urlOption);if(!Uri.IsWellFormedUriString(url,UriKind.Absolute))result.AddError($"'{url}' is not a well-formed uri");});Options.Add(urlOption);SetAction(async(result,cancellation)=>{Uriurl=new(result.GetRequiredValue(urlOption));logger.LogInformation("handling url: {}",url);awaitTask.CompletedTask;logger.LogInformation("handled");});}}
wow thank you. should've looked a little harder, I see that example in TypeConversionTests.cs
To save people some time, here's my adaptation of the test's approach:
staticpublicreadonlyOption<Uri>OpenAiEndpointOption=new("--openai-endpoint",["-e"]){Description="OpenAI API endpoint",Required=false,DefaultValueFactory=(_)=>newUri("https://api.openai.com/"),CustomParser= result =>{vartoken=result.Tokens.SingleOrDefault();if(token==null){result.AddError("A single argument is required for the OpenAI endpoint");returnnull;}if(!Uri.TryCreate(token.Value,UriKind.Absolute,outvarparsedUri)){result.AddError("The OpenAI endpoint must be a valid absolute URI");returnnull;}returnparsedUri;}};
Activity
andrewschmidgit commentedon Jun 18, 2025
The workaround I'm using is
jonsequitur commentedon Jun 18, 2025
Using
Option.CustomParsercombines validation and strong typing so that you can useOption<Uri>.andrewschmidgit commentedon Jun 18, 2025
wow thank you. should've looked a little harder, I see that example in
TypeConversionTests.csjonsequitur commentedon Jun 18, 2025
Reopening while we discuss whether to include
Urisupport by default. It's such a common type it's probably worthwhile.Add custom parsing logic for URI
Add custom parsing logic for URI
Add custom parsing logic for URI
fix dotnet#2574
ohadschn commentedon Feb 10, 2026
To save people some time, here's my adaptation of the test's approach:
Fix System.CommandLine 2.0.0 Uri parsing in GrpcClient benchmark app
Fix System.CommandLine 2.0.0 Uri parsing in GrpcClient benchmark app
Fix System.CommandLine 2.0.0 Uri parsing in GrpcClient benchmark app
Fix System.CommandLine 2.0.0 Uri parsing in GrpcClient benchmark app