Skip to content

add Uri to ArgumentConverter.StringConverters #2574

Open

Description

@andrewschmidgit

Right now, if you have:

public class UrlOption : Option<Uri>
{
  public UrlOption() : base("--url")
  {
    Required = true;
  }
}

an InvalidOperationException is thrown, with the message:

Cannot parse argument 'http://example.com' for option '--url' as expected type 'System.Uri'.

Activity

  1. andrewschmidgit commented on Jun 18, 2025

    @andrewschmidgit
    Author

    The workaround I'm using is

    public class ExampleCommand : Command
    {
        public ExampleCommand() : base("example")
        {
            Option<string> urlOption = new("--url")
            {
                Required = true,
            };
    
            urlOption.Validators.Add(result =>
            {
                var url = 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) =>
            {
                Uri url = new(result.GetRequiredValue(urlOption));
    
                logger.LogInformation("handling url: {}", url);
    
                await Task.CompletedTask;
                logger.LogInformation("handled");
            });
        }
    }
  2. jonsequitur commented on Jun 18, 2025

    @jonsequitur
    Contributor

    Using Option.CustomParser combines validation and strong typing so that you can use Option<Uri>.

  3. andrewschmidgit commented on Jun 18, 2025

    @andrewschmidgit
    Author

    wow thank you. should've looked a little harder, I see that example in TypeConversionTests.cs

  4. jonsequitur commented on Jun 18, 2025

    @jonsequitur
    Contributor

    Reopening while we discuss whether to include Uri support by default. It's such a common type it's probably worthwhile.

  5. added this to the 2.0.0-beta6 milestone on Jun 19, 2025
  6. self-assigned this
    on Aug 8, 2025
  7. added a commit that references this issue on Aug 8, 2025
    f3d3716
  8. modified the milestones: 2.0.0-beta7, 3.0.0 on Sep 18, 2025
  9. ohadschn commented on Feb 10, 2026

    @ohadschn

    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:

    static public readonly Option<Uri> OpenAiEndpointOption = new("--openai-endpoint", ["-e"])
        {
            Description = "OpenAI API endpoint",
            Required = false,
            DefaultValueFactory = (_) => new Uri("https://api.openai.com/"),
            CustomParser = result => 
            {
                var token = result.Tokens.SingleOrDefault();
                if (token == null)
                {
                    result.AddError("A single argument is required for the OpenAI endpoint");
                    return null;
                }
    
                if (!Uri.TryCreate(token.Value, UriKind.Absolute, out var parsedUri))
                {
                    result.AddError("The OpenAI endpoint must be a valid absolute URI");
                    return null;
                }
                
                return parsedUri;
            }
        };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions