I have a System.CommandLine .NET 9 command line application. It's mostly used for scheduled tasks and the like, but sometimes users need to run it manually.
When run manually, while the help is good, users find it quite difficult to build one command line.
I want to add an --interactive mode flag. When this is true any required missing commands or options should become prompts instead.
var rootCommand = new RootCommand("Command-line interface") {
new Command(["example-a", "a"], "Do something"),
new Command(["example-b", "b"], "Do something else")
}
If a user calls this without the commands they get something like:
C:\Users\me>myApp
Required command was not provided.
Description:
Command-line interface
Usage:
myApp [command] [options]
Options:
-?, -h, --help Show help and usage information
Commands:
a, example-a Do something
b, example-b Do something else
Which is helpful, and needs to be the default, but we have a lot of commands that go quite deep and some required many options (which can be things with their own validation, like file paths or URLs).
I want to add an opt-in interactive mode for manual users that reads the System.CommandLine implementation, so something like:
C:\Users\me>myApp --interactive
Choose a Command:
example-a Do something
> example-b Do something else
I could do this by going through every command and adding Console.Readline into every SetHandler implementation, but that is a lot of duplication.
It seems much cleaner to AddMiddleware that picks up the ParseResult.Errors collection and builds a set of prompts to ask for the missing options, but building this from scratch involves a lot of messy work and corner cases.
Is there a better way to do this? Am I missing a built in option on System.CommandLine? I can't be the only person trying to do this, even System.CommandLine's own documentation reserves --interactive and -i for this kind of opt-in prompts.