NestedArgs 1.0.2

dotnet add package NestedArgs --version 1.0.2
                    
NuGet\Install-Package NestedArgs -Version 1.0.2
                    
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="NestedArgs" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NestedArgs" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="NestedArgs" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add NestedArgs --version 1.0.2
                    
#r "nuget: NestedArgs, 1.0.2"
                    
#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.
#:package NestedArgs@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=NestedArgs&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=NestedArgs&version=1.0.2
                    
Install as a Cake Tool

NestedArgs

NestedArgs is a .NET library designed to simplify the creation and management of complex command-line arguments in .NET applications. By supporting nested commands and options, it enables developers to craft intuitive, powerful, and well-structured command-line interfaces with ease.

Key Advantages of NestedArgs

NestedArgs distinguishes itself from other command-line parsing libraries through its robust feature set and support for Ahead-of-Time (AOT) compilation. Here are the primary benefits:

  1. Hierarchical Command Structure
    NestedArgs enables you to define commands and subcommands in a nested, hierarchical way. This is ideal for applications with multiple command levels, providing an organized and user-friendly interface. For instance, think of a git-like structure with commands such as git commit or git push, each with their own options�NestedArgs makes this simple and intuitive.

  2. Flexible Option Handling
    The library offers versatile option configuration, including:

    • Long and short names (e.g., --verbose or -v)
    • Default values for optional inputs
    • Required options that enforce user input
    • Flags (options without values)
    • Multi-value options (e.g., --file file1 --file file2)
      This flexibility allows you to design a command-line interface tailored to your application�s unique needs.
  3. Intuitive API
    With a fluent and readable API, NestedArgs simplifies the process of defining commands and options. The chained method calls (e.g., .Option().SubCommand()) produce clean, maintainable code that�s easy to understand and extend.

  4. AOT Compilation Support
    NestedArgs fully supports Ahead-of-Time (AOT) compilation, which pre-compiles your application into native code for faster startup times and better performance. This is especially valuable in scenarios like startup scripts or performance-critical environments where eliminating Just-In-Time (JIT) compilation overhead is a priority.

  5. Seamless .NET Integration
    Built with .NET developers in mind, NestedArgs aligns with familiar .NET patterns and practices, ensuring a smooth adoption process. It integrates effortlessly into your existing .NET projects, reducing the learning curve.

  6. Automatic Help Generation
    NestedArgs generates help text automatically for all commands and options, saving you time and ensuring users have clear, consistent documentation. Just define your structure, and NestedArgs handles the rest.

  7. Customization and Extensibility
    The library strikes a balance between ready-to-use functionality and adaptability. You can customize option types, add validation logic, or extend behavior to meet specific requirements�all while leveraging a solid foundation.

  8. Lightweight and Efficient
    Designed to minimize overhead, NestedArgs keeps your application responsive and efficient, even in resource-constrained environments. It�s a lean solution that delivers power without bloat.

In short, NestedArgs is a robust, user-friendly, and high-performance tool for managing command-line arguments in .NET, enhanced by AOT compilation support. It�s an excellent choice for everything from simple utilities to complex, multi-tiered applications.

Getting Started

Installation

Install NestedArgs via NuGet Package Manager:

Install-Package NestedArgs

Or use the .NET CLI:

dotnet add package NestedArgs

Compatibility: NestedArgs supports .NET 6.0 and later versions.

Basic Usage

Here�s an example to demonstrate how to set up a basic command-line interface with NestedArgs, including a subcommand to showcase its hierarchical capabilities:

using NestedArgs;

internal class Program
{
    private static void Main(string[] args)
    {
        // Define the root command
        Command rootCommand = new CommandBuilder("myapp", "A sample application with nested commands.")
            .Option(new Option
            {
                LongName = "option1",
                ShortName = 'o',
                Description = "A required root-level option",
                IsRequired = true
            })
            .SubCommand(new CommandBuilder("subcmd", "A subcommand for specific tasks.")
                .Option(new Option
                {
                    LongName = "suboption",
                    ShortName = 's',
                    Description = "An optional subcommand option",
                    IsRequired = false
                })
                .Build())
            .Build();

        // Parse the arguments
        var result = rootCommand.Parse(args);

        // Handle the parsed results
        if (result.Status == ParseStatus.Success)
        {
            var matches = result.Matches;
            Console.WriteLine($"Root option value: {matches.Value("option1")}");
            if (matches.SubCommandMatches("subcmd") is CommandMatches subMatches)
            {
                Console.WriteLine($"Subcommand option value: {subMatches.Value("suboption") ?? "Not provided"}");
            }
        }
        else
        {
            Console.WriteLine("Error parsing arguments. Use --help for usage info.");
        }
    }
}

Running the Example:

  • myapp --option1 value triggers the root command with option1.
  • myapp subcmd --option1 value --suboption subvalue includes the subcommand and its option.

Handling Parsed Arguments

After parsing, the CommandMatches object provides access to option values:

  • matches.Value("option1") retrieves the value of --option1.
  • matches.SubCommandMatches("subcmd") accesses the subcommand�s matches, allowing you to retrieve --suboption or other nested values.

This structure simplifies handling multi-level command-line inputs in a clear and logical way.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.0

    • No dependencies.

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
1.0.2 300 3/4/2025
1.0.1 275 1/6/2024
1.0.0 225 1/6/2024