I am trying to learn how to use System.CommandLine. I have created a simple code which need to accept a url and an optional parameter of file path.
static async Task<int> Main(string[] args)
{
var cmd = new RootCommand("Save URL content to file");
cmd.AddArgument(new Argument<string>("URL", "The network resource url you want to retrieve"));
cmd.AddOption(new Option<FileInfo>(new[] { "--output-document", "-O" }, "write document to FILE"));
cmd.Handler = CommandHandler.Create<string, FileInfo, IConsole >(HandleDownload);
return await cmd.InvokeAsync(args);
}
static void HandleDownload(string url, FileInfo file, IConsole console)
{
console.WriteLine(url);
console.WriteLine(file.FullName);
}
When I ran with below arguments
"http://www.google.com" --output-document c:\test.html
Inside HandleDownload url variable is gets assigned correctly but the 'file' variable is appearing as null. What am I doing wrong?