EasyArguments 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package EasyArguments --version 1.0.1
                    
NuGet\Install-Package EasyArguments -Version 1.0.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="EasyArguments" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EasyArguments" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="EasyArguments" />
                    
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 EasyArguments --version 1.0.1
                    
#r "nuget: EasyArguments, 1.0.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.
#:package EasyArguments@1.0.1
                    
#: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=EasyArguments&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=EasyArguments&version=1.0.1
                    
Install as a Cake Tool

<div align="center">

EasyArguments

NuGet

EasyArguments is a lightweight .NET library that simplifies the process of parsing command-line arguments into strongly-typed objects. It provides attributes to define metadata for arguments and a controller to handle the parsing logic.

</div>

Table of Contents

Installation

Nuget package coming...

Usage

Configuring the Controller

Use the ArgumentsControllerAttribute to configure the behavior of the class that defines the arguments. You can specify whether the order of arguments should be respected, whether an automatic help argument should be included, and the character used as a separator.

using EasyArguments.Attributes;

[ArgumentsController(RespectOrder = true, AutoHelpArgument = true, Separator = '=')]
public class MyArguments
{
    // Argument definitions
}

Defining Arguments

To define command-line arguments, use the ArgumentAttribute on properties within a class. You can specify short and long names, help messages, and whether the argument is required.

using EasyArguments.Attributes;

[ArgumentsController(RespectOrder = true, AutoHelpArgument = true, Separator = '=')]
public class MyArguments
{
    [Argument("-n", "--name", "Specifies the name.", Required = true)]
    public string Name { get; set; }

    [Argument("-a", "--age", "Specifies the age.", Required = true)]
    public int Age { get; set; }

    [Argument(null, "--verbose", "Enables verbose mode.")]
    public bool Verbose { get; set; }

    [Argument("-v", "--version", "Displays application version.")]
    public bool Version { get; set; }
}

Parsing Arguments

Use the ArgumentsController class to parse the command-line arguments into an instance of your class.

using EasyArguments;

class Program
{
    static void Main(string[] args)
    {
        try
        {
            MyArguments parsedArgs = ArgumentsController.Parse<MyArguments>(args);
            // Use parsedArgs
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error: {ex.Message}");
        }
    }
}

Handling Errors

The ArgumentsController provides a mechanism to handle errors. By default, errors are thrown as exceptions. You can redirect errors to the console by setting ArgumentsController.RedirectErrorToConsole to true.

using EasyArguments;

class Program
{
    static void Main(string[] args)
    {
        try
        {
            
            ArgumentsController.RedirectErrorToConsole = true; // Errors will be shown in the console
            MyArguments parsedArgs = ArgumentsController.Parse<MyArguments>(args);
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error: {ex.Message}");
        }
    }
}

Full Example

Here is a complete example:

using EasyArguments;
using EasyArguments.Attributes;
using System;

[ArgumentsController(RespectOrder = true, AutoHelpArgument = true, Separator = '=')]
public class MyArguments
{    
    [Argument("-n", "--name", "Specifies the name.", Required = true)]
    public string Name { get; set; }

    [Argument("-a", "--age", "Specifies the age.", Required = true)]
    public int Age { get; set; }

    [Argument(null, "--verbose", "Enables verbose mode.")]
    public bool Verbose { get; set; }

    [Argument("-v", "--version", "Displays application version.")]
    public bool Version { get; set; }
}

class Program
{
    // args = ["-n=EasyArguments", "-age=1", "--verbose"]
    static void Main(string[] args)
    {
        ArgumentsController.RedirectErrorToConsole = true;

        try
        {
            var parsedArgs = ArgumentsController.Parse<MyArguments>(args);
            Console.WriteLine($"Name: {parsedArgs.Name}");
            Console.WriteLine($"Age: {parsedArgs.Age}");
            Console.WriteLine($"Verbose: {parsedArgs.Verbose}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Unexpected Error: {ex.Message}");
        }
    }
}

Contribution

Contributions are welcome! If you have ideas to improve EasyArguments feel free to open an issue.

How to Contribute

  • Fork the repository.
  • Create a issue
  • Create a new branch for your issue.
  • Submit a pull request with a detailed explanation of your changes.
  • 😃

License

This project is licensed under the GPLv3 License. See the LICENSE file for details.

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.1.1 123 2/4/2025
1.0.8 110 2/2/2025
1.0.6 109 1/31/2025
1.0.4 100 1/28/2025
1.0.3 105 1/25/2025
1.0.2 105 1/25/2025
1.0.1 107 1/25/2025
1.0.0 108 1/25/2025