I have a .net console app where I'm trying to make the code more concise. Currently to setup a command using System.CommandLine I have something like this:
using System.CommandLine;
var rootCommand = new RootCommand("CLI");
var theOption = new Option<int?>("--id", "Database ID");
var theCommand = new Command("stuff", "Do Stuff.") { theOption };
ret.SetHandler((id) =>
{
Console.WriteLine($"Do stuff on id: {id}");
}, theOption);
rootCommand.Add(theCommand);
return rootCommand.Invoke(args);
What I'd like to do is make a one liner, but I'm not sure how to do so since I'm using the variable theOption twice. How could I do this? Looking for something like:
using System.CommandLine;
var rootCommand = new RootCommand("CLI");
//refactor theOption into below statement
var theOption = new Option<int?>("--id", "Database ID");
rootCommand.Add(
new Command("stuff", "Do Stuff.") { theOption }
).SetHandler((id) =>
{
Console.WriteLine($"Do stuff on id: {id}");
}, theOption);
return rootCommand.Invoke(args);
Any recommendations would be appreciated. My OCD is acting up without the indentation.