RazorDocs Search

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 ICommand from the entry point assembly and dependent modules.
  • Hosted Runner: Integrates with the .NET Generic Host to manage service lifecycles during command execution.

📂 Back to Console List | 🏠 Back to Root

Type

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.

Method

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.

Type

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.
Method

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.

Type

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.

Method

Configure

void Configure(CommandChainBuilder builder)

Configures the sequence of commands to execute.

Parameters

  • builderFluent builder to define the chain.
Type

CommandChainBuilder

Fluent builder used to configure the chain of commands.

Method

Add

CommandChainBuilder Add<TCommand>()

Adds a command to the execution chain.

Method

AddIf

CommandChainBuilder AddIf<TCommand>(Func<bool> condition)

Adds a command to the chain that will execute only when condition returns true.

Type

IOptionSuggester

Defines a contract for suggesting alternative options when an unknown option is provided.

Method

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.

Type

ConsoleStartup<TModule>

A base class for console application startup logic, extending the module-based initialization.

Type Parameters

  • TModuleThe type of the root module.
Method

ConfigureAdditionalServices

void ConfigureAdditionalServices(StartupContext context, IServiceCollection services)

Allows derived classes to register additional services.

Parameters

  • contextThe startup context.
  • servicesThe service collection.
Type

CommandService

Method

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:
  1. Resolves the target command type from the parsed arguments and registered commands.
  2. Extracts the set of valid option names for the resolved command.
  3. Compares the provided arguments against the valid options and uses IOptionSuggester to present suggestions for any unknown options.
Type

LevenshteinOptionSuggester

Suggests options using the Levenshtein distance algorithm.

Method

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.

Method

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.