CodeOfChaos.CliArgsParser 3.0.3

dotnet add package CodeOfChaos.CliArgsParser --version 3.0.3                
NuGet\Install-Package CodeOfChaos.CliArgsParser -Version 3.0.3                
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" Version="3.0.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CodeOfChaos.CliArgsParser --version 3.0.3                
#r "nuget: CodeOfChaos.CliArgsParser, 3.0.3"                
#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 as a Cake Addin
#addin nuget:?package=CodeOfChaos.CliArgsParser&version=3.0.3

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

CliArgsParser

NuGet Downloads NuGet Version

CliArgsParser is a library built around Dependency Injection to allow you to create Cli tools with ease. This is particularly useful in scenarios where your application requires a large amount of commands, with specific arguments.

dotnet add package CliArgsParser --version 2.2.7

Features

Here are the key features of Cli Args Parser:

  • Command Declaration and Registration: Commands are declaratively defined within a class using the ICommandAtlas interface. This arrangement provides a well-structured, easy-to-maintain way of defining and locating command handling logic.
  • Asynchronous Command Execution: CliArgsParser allows for asynchronous execution of commands. This can provide major performance benefits, especially when commands involve IO-bound operations. The library also exposes the ArgsParser.ParseAsyncParallel method for parallel async execution of cli tools.
  • Argument Mapping: CliArgsParser allows property-based argument mapping. Properties of a class implementing the ICommandParameters interface can be used as the target of argument mapping from command line input. Argument mapping is further simplified through the use of the ArgValue and ArgFlag attributes which takes the argument name as a parameter.
  • Dependency Injection approach: The library is built around using DI in your workflow. ICommandAtlas classes allow constructors to fully utilize dependency injection. Yet you are not required to have a IServiceCollection, by using the CliParser.CreateStandalone() or ArgsParser.CreateStandalone() methods.

F.A.Q:

"I tried to create a atlas with async commands, but they did not execute when called"

This is due to the parser trying to execute the commands as non-async, because the parser was most likely called without using the async methods. To resolve this, always run the parsers through their async methods.

Quick Start

The following section is a basic example on how to get started with CliArgsParser. For in-code examples, they can be found in the repo under examples : https://github.com/code-of-chaos/CliArgsParser-cs/tree/core/examples

Dependency Injected approach

using CliArgsParser;

// Define a class for mapping arguments
public class ArgsTest : ICommandParameters  {
    [ArgValue("name"), Description("Defines the name of the user")] 
    public string Name { get; set; } = "Default Name";
}

public class MyAppCommands : ICommandAtlas {
    [Command("hello"), Description("Greets everyone")]
    public void CommandHello() {
        Console.Writeline("Hello there!");    
        /* Or your command' implementation, without arguments, goes here... */
    }

    [Command<ArgsTest>("greet"), Description("Greets you specificly")]
    public void CommandGreet(ArgsTest argsTest)  {
        Console.Writeline($"Hello there and welcome, {argsTest.Name}");  
        /* Or Your command implementation goes here... */
    }
}

internal static class Program {
    public async static Task Main(string[] args) {
        IServiceCollection serviceCollection = new ServiceCollection();

        serviceCollection.AddCliArgsParser(configuration =>
            configuration
                .SetConfig(new CliArgsParserConfig {
                    Overridable = true,
                    GenerateShortNames = true
                })
                .AddFromType<HelloAtlas>()
        );
        
        ServiceProvider provider = serviceCollection.BuildServiceProvider();
        
        var cliParser =  provider.GetRequiredService<ICliParser>();
        await cliParser.StartParsingAsync();
    }
}

Standalone Approach

using CliArgsParser;

// Define a class for mapping arguments
public class ArgsTest : ICommandParameters  {
    [ArgValue("name"), Description("Defines the name of the user")] 
    public string Name { get; set; } = "Default Name";
}

public class MyAppCommands : ICommandAtlas {
    [Command("hello"), Description("Greets everyone")]
    public void CommandHello() {
        Console.Writeline("Hello there!");    
        /* Or your command' implementation, without arguments, goes here... */
    }

    [Command<ArgsTest>("greet"), Description("Greets you specificly")]
    public void CommandGreet(ArgsTest argsTest)  {
        Console.Writeline($"Hello there and welcome, {argsTest.Name}");  
        /* Or Your command implementation goes here... */
    }
}

internal static class Program {
    public async static Task Main(string[] args) {
        ICliParser parser = CliParser.CreateStandalone(
            configuration =>
                configuration
                    .SetConfig(new CliArgsParserConfig {
                        Overridable = true,
                        GenerateShortNames = true
                    })
                    .AddFromType<HelloAtlas>()
        );
        await cliParser.StartParsingAsync();
    }
}
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
3.0.3 105 12/12/2024
3.0.3-preview.6 0 12/18/2024
3.0.3-preview.5 34 12/17/2024
2.2.7 64 12/12/2024