Skip to content

Repository files navigation

ApplicationBuilderHelpers

A .NET library for building command-line applications with a fluent API, dependency injection, and modular architecture.

  • Targets: net6.0net10.0 · AOT-compatible · Trimmable
  • Dependencies: Microsoft.Extensions.Hosting, Microsoft.Extensions.DependencyInjection.Abstractions, AbsolutePathHelpers

Features

  • 🎯 Command-based Architecture — Command patterns with automatic argument parsing
  • 🔧 Fluent Builder API — Intuitive setup via method chaining
  • 💉 Dependency Injection — Full Microsoft.Extensions.DependencyInjection support
  • 🏗️ Modular Application Structure — Reusable ApplicationDependency modules with lifecycle hooks
  • ⚙️ Configuration — .NET configuration integration with @ref: reference values
  • 🎨 Attributes[Command], [CommandOption], [CommandArgument] for declarative CLI definitions
  • 🎯 Sub-Commands — Hierarchical commands via space-separated names
  • 🖌️ Themable Help — 5 built-in console color themes, configurable help width
  • 🧩 Multiple Host TypesHostApplicationBuilder, WebApplicationBuilder, custom builders

Installation

dotnet add package ApplicationBuilderHelpers

Quick Start

// Program.cs
using ApplicationBuilderHelpers;

return await ApplicationBuilder.Create()
    .AddApplication<CoreApplication>()
    .AddCommand<GreetCommand>()
    .RunAsync(args);
[Command(description: "Greet someone")]
public class GreetCommand : Command
{
    [CommandArgument(Name = "name", Position = 0, Description = "Who to greet")]
    public string Name { get; set; } = "World";

    protected override ValueTask Run(ApplicationHost<HostApplicationBuilder> applicationHost, CancellationToken cancellationToken)
    {
        Console.WriteLine($"Hello, {Name}!");
        return ValueTask.CompletedTask;
    }
}
$ myapp Alice
Hello, Alice!

Core Concepts

Commands

Extend Command and override Run. Define options with [CommandOption] and positional arguments with [CommandArgument]. Commands can register their own services, middleware, and configuration — they inherit the full ApplicationDependency lifecycle.

[Command("build", description: "Build the project")]
public class BuildCommand : Command
{
    [CommandOption('v', "verbose", Description = "Enable verbose output")]
    public bool Verbose { get; set; }

    protected override async ValueTask Run(ApplicationHost<HostApplicationBuilder> applicationHost, CancellationToken cancellationToken)
    {
        // ...build logic...
    }
}

ApplicationDependency

Group shared services and configuration into reusable modules:

public class CoreApplication : ApplicationDependency
{
    public override void AddServices(ApplicationHostBuilder appBuilder, IServiceCollection services)
    {
        services.AddSingleton<IMyService, MyService>();
    }
}

See Application Dependencies for the full lifecycle reference.

Sub-Commands

Use space-separated names for hierarchical commands. Try myapp deploy prod or myapp deploy prod rollback:

[Command("deploy prod", description: "Deploy to production")]
public class DeployProductionCommand : Command { /* ... */ }

Exit Codes

RunAsync returns an exit code:

Outcome Exit code
Run returns normally (also --help / --version) 0
Usage / validation error (UnknownOption, MissingRequired, RequiresSubcommand, InvalidValue, UnknownCommand, DuplicateOption) 2
Unexpected fault (Fault, NoImplementation, or Run throwing CommandException with a custom code) 1 or ex.ExitCode (custom host-code passthrough preserved)
Cancellation (CancellationToken / Ctrl+C) 130 (128 + SIGINT)

Return normally on success. Throw CommandException for errors to return a non-zero exit code from RunAsync:

throw new CommandException("Operation failed", exitCode: 1);

Shell completion (complete / completions ...) resolves through the CompletionGateway pre-parse stage first — see Commands for the consolidated 0/1/2 exit matrix.

See Advanced Topics for more on sub-commands, custom host types, error handling, and error footers.

Architecture

┌─────────────────────┐
│  ApplicationBuilder │ ← Entry Point (fluent API)
└──────────┬──────────┘
           │
    ┌──────▼──────┐
    │  Commands   │ ← Command Registration (+ own lifecycle hooks)
    └──────┬──────┘
           │
    ┌──────▼──────────┐
    │  Applications   │ ← Application Modules (lifecycle hooks)
    └──────┬──────────┘
           │
    ┌──────▼───────────┐
    │  Host Builder    │ ← Host Configuration
    └──────┬───────────┘
           │
    ┌──────▼──────┐
    │  Services   │ ← Dependency Injection
    └──────┬──────┘
           │
    ┌──────▼──────────┐
    │  Middleware     │ ← Request Pipeline
    └──────┬──────────┘
           │
    ┌──────▼──────┐
    │  Execution  │ ← Command Execution
    └─────────────┘

RunAsync pipeline stages: hierarchy build → CompletionGateway (completion > help > parse > version) → help → parse → version check → execute.

Documentation

Guide
Getting Started Installation, first app, services
Commands Attributes, options, arguments, lifecycle
Application Dependencies Full lifecycle reference
Configuration & Themes Fluent config, themes, @ref: system, help formatting
Custom Type Parsers ICommandTypeParser / CommandTypeParser<T>
Advanced Topics Sub-commands, host types, exit codes, error handling
API Reference Complete public API surface

Contributing

Contributions are welcome! Please submit a Pull Request.

License

MIT — see the LICENSE file.

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages