Removing the version command from System.CommandLine
The
System.CommandLine
library is a powerful tool for building command-line interfaces (CLIs) in .NET applications. It simplifies the process of parsing command-line arguments, options, and subcommands. However, the default parser adds a version command and a help command, which might not always be desirable for every CLI application.
Although there's no way to directly remove said commands, you can create a parser without them:
var root = new RootCommand(ResourceStrings.RootCommandDescription)
{
/*
* More subcommands
*/
};
/*
* Create a new CommandLineBuilder
* without the version and help commands.
*/
var cmdBuilder = new CommandLineBuilder(root)
// .UseVersion() // Commenting out the Version command
// .UseHelp() // Commenting out the Help command
.UseEnvironmentVariableDirective()
.UseParseDirective()
.UseSuggestDirective()
.RegisterWithDotnetSuggest()
.UseTypoCorrections()
.UseParseErrorReporting()
.UseExceptionHandler()
.CancelOnProcessTermination();
/*
* Builds a parser.
*/
var parser = cmdBuilder.Build();
/*
* Parse the command line arguments
*/
parser.Invoke(args);
So you can create your own “default” parser, and be happy.
Comments
Post a Comment