CodeOfChaos.CliArgsParser.Library 4.2.1

dotnet add package CodeOfChaos.CliArgsParser.Library --version 4.2.1                
NuGet\Install-Package CodeOfChaos.CliArgsParser.Library -Version 4.2.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CodeOfChaos.CliArgsParser.Library" Version="4.2.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CodeOfChaos.CliArgsParser.Library --version 4.2.1                
#r "nuget: CodeOfChaos.CliArgsParser.Library, 4.2.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install CodeOfChaos.CliArgsParser.Library as a Cake Addin
#addin nuget:?package=CodeOfChaos.CliArgsParser.Library&version=4.2.1

// Install CodeOfChaos.CliArgsParser.Library as a Cake Tool
#tool nuget:?package=CodeOfChaos.CliArgsParser.Library&version=4.2.1                

CodeOfChaos.CliArgsParser

Overview

CodeOfChaos.CliArgsParser is a library designed to simplify the process of creating command-line applications in .NET projects. It provides developers with attributes, structures, and tools to quickly define, parse, and execute CLI commands with minimal boilerplate code. The library is focused on flexibility, clarity, and ease of use, utilizing C#'s powerful features like attributes and partial classes.


Features

  • Easy Command Definition: Define commands using attributes like [CliArgsCommand] and [CliArgsParameter] directly in your application.
  • Automatic Parsing: Command-line arguments are parsed and mapped to parameters automatically.
  • Description Metadata: Add meaningful descriptions to commands and parameters using [CliArgsDescription] for better documentation.
  • Support for Flags and Complex Types: Easily handle flags, strings, arrays, and even optional/default values.
  • Asynchronous Execution: Built-in support for asynchronous methods to handle complex operations.

Getting Started

To use CodeOfChaos.CliArgsParser in your project, follow these steps:

1. Install the Library

Add the library to your project via NuGet:

dotnet add package CodeOfChaos.CliArgsParser

2. Define a Command

Commands are defined as partial classes and annotated with metadata. For example:

[CliArgsCommand("example")]
[CliArgsDescription("This is a test command")]
public partial class ExampleCommand : ICommand<ExampleCommandParameters> {
    public async Task ExecuteAsync(ExampleCommandParameters parameters) {
        Console.WriteLine($"Running example command with TestValue: {parameters.TestValue}");
        
        if (parameters.Verbose) {
            Console.WriteLine("Verbose mode enabled.");
        }
        
        await Task.Delay(100); // Simulating async work
    }
}

3. Define Parameters

Parameters for a command are defined using partial structs with annotated attributes:

public readonly partial struct ExampleCommandParameters : IParameters
{
    [CliArgsParameter("test", "t")]
    [CliArgsDescription("A required test parameter")]
    public required string TestValue { get; init; }

    [CliArgsParameter("verbose", "v", ParameterType.Flag)]
    [CliArgsDescription("Enable verbose logging")]
    public bool Verbose { get; init; }
    
    [CliArgsParameter("optional-parameter", "o")]
    [CliArgsDescription("An optional parameter")]
    public string? OptionalValue { get; init; } = "default";
}

4. Register and Run Commands

Typically, this is performed in your Program.cs or entry point:

using CodeOfChaos.CliArgsParser;

CliArgsParser parser = CliArgsBuilder.CreateFromConfig(
    config => {
        config.AddCommand<ExampleCommand>();
    }
).Build();

await parser.ParseAsync(args);
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
4.2.1 55 12/23/2024
4.2.0 78 12/22/2024
4.1.0 71 12/22/2024
4.0.0 72 12/22/2024
4.0.0-preview.8 36 12/22/2024
3.0.3 109 12/12/2024
3.0.3-preview.8 56 12/21/2024
3.0.3-preview.7 41 12/21/2024
3.0.3-preview.6 35 12/18/2024
3.0.3-preview.5 37 12/17/2024
2.2.7 77 12/12/2024