command-line-api
command-line-api copied to clipboard
add `Uri` to `ArgumentConverter.StringConverters`
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'.
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");
});
}
}
Using Option.CustomParser combines validation and strong typing so that you can use Option<Uri>.
wow thank you. should've looked a little harder, I see that example in TypeConversionTests.cs
Reopening while we discuss whether to include Uri support by default. It's such a common type it's probably worthwhile.