command-line-api
command-line-api copied to clipboard
How to allow for starting using URI protocol activation?
With URI protocol activation, the URI of the (custom) protocol will be passed as the first argument on the command line, like this:
myapp.exe myscheme://90413064-fa7f-4750-afa6-2f34047042ca
How can we best support this in parallel to supporting commands set using System.CommandLine, like
myapp.exe mycommand --myoption
https://docs.microsoft.com/en-us/windows/win32/shell/app-registration https://blog.magnusmontin.net/2019/05/10/handle-protocol-activation-and-redirection-in-packaged-apps/
Parsing this is fairly straightforward. Setting up your parser like this should work:
var uriArgument = new Argument<Uri>();
var myOption = new Option<int>("--myoption");
var myCommand = new Command("mycommand")
{
myOption
};
var rootCommand = new RootCommand
{
uriArgument,
myCommand
};
rootCommand.SetHandler(uri => Console.WriteLine($"Received URI: {uri}"), uriArgument);
myCommand.SetHandler(i => Console.WriteLine($"Received option: {i}"), myOption);
rootCommand.Invoke("myscheme://90413064-fa7f-4750-afa6-2f34047042ca");
rootCommand.Invoke("mycommand --myoption 123");