Console
ForgeTrust.Runnable.Console
Modular bootstrapping for .NET Console applications using CliFx.
Overview
ForgeTrust.Runnable.Console provides a structured way to build command-line tools. It automatically discovers and registers CliFx commands from modules and provides a hosted service to run them.
Usage
Create a startup class that inherits from ConsoleStartup<TModule>:
public class MyConsoleStartup : ConsoleStartup<MyRootModule> { }
In your Program.cs:
await ConsoleApp<MyRootModule>.RunAsync(args);
Features
- Command Discovery: Automatically registers classes implementing
ICommandfrom the entry point assembly and dependent modules. - Hosted Runner: Integrates with the .NET Generic Host to manage service lifecycles during command execution.
ConsoleApp<TStartup, TModule>
This class is used to run a console application with a specified startup class and module. This allows for further customization of the consoles startup process.
RunAsync
Task RunAsync(string[] args)
Runs the console application asynchronously using a custom startup class.
Parameters
argsCommand-line arguments.
Returns
A task representing the run operation.
ConsoleApp<TModule>
This class is used to run a console application with a specified module and a generic startup.
Type Parameters
TModuleThe type of the root module.
RunAsync
Task RunAsync(string[] args)
Runs the console application asynchronously with a generic startup.
Parameters
argsCommand-line arguments.
Returns
A task representing the run operation.
ChainedCommand
A command that can execute a sequence of other commands. Parameters and options defined on the parent command are automatically forwarded to the child commands when they share the same property name. Required parameters of child commands are validated before any command in the chain is executed.
Configure
void Configure(CommandChainBuilder builder)
Configures the sequence of commands to execute.
Parameters
builderFluent builder to define the chain.
CommandChainBuilder
Fluent builder used to configure the chain of commands.
Add
CommandChainBuilder Add<TCommand>()
Adds a command to the execution chain.
AddIf
CommandChainBuilder AddIf<TCommand>(Func<bool> condition)
Adds a command to the chain that will execute only when condition returns true.
IOptionSuggester
Defines a contract for suggesting alternative options when an unknown option is provided.
GetSuggestions
IReadOnlyList<string> GetSuggestions(string? unknownOption, IEnumerable<string>? validOptions)
Gets a list of suggested options based on the unknown option and the list of valid options.
Parameters
unknownOptionThe unknown option provided by the user.validOptionsThe list of valid options for the current command.
Returns
A collection of suggested options.
ConsoleStartup<TModule>
A base class for console application startup logic, extending the module-based initialization.
Type Parameters
TModuleThe type of the root module.
ConfigureAdditionalServices
void ConfigureAdditionalServices(StartupContext context, IServiceCollection services)
Allows derived classes to register additional services.
Parameters
contextThe startup context.servicesThe service collection.
CommandService
CheckForUnknownOptions
void CheckForUnknownOptions(IConsole console)
Analyzes the current command-line arguments to detect unknown or mistyped options after a command has failed and, when possible, displays suggestions for valid alternatives.
Parameters
consoleThe console used to write diagnostic messages and option suggestions to the user.
Remarks
This method performs three main steps:- Resolves the target command type from the parsed arguments and registered commands.
- Extracts the set of valid option names for the resolved command.
- Compares the provided arguments against the valid options and uses
IOptionSuggesterto present suggestions for any unknown options.
LevenshteinOptionSuggester
Suggests options using the Levenshtein distance algorithm.
GetSuggestions
IReadOnlyList<string> GetSuggestions(string? unknownOption, IEnumerable<string>? validOptions)
Gets a list of suggested options based on their Levenshtein distance to the unknown option.
Parameters
unknownOptionThe unknown command line option.validOptionsThe list of valid command line options.
Returns
A collection of suggested options that closely match the unknown option.
ComputeLevenshteinDistance
int ComputeLevenshteinDistance(string s, string t)
Computes the Levenshtein distance between two strings using a dynamic programming approach.
Parameters
sThe first string to compare.tThe second string to compare.
Returns
The number of single-character edits (insertions, deletions, or substitutions) required to change one string into the other.