EasyArguments 1.0.1
See the version list below for details.
dotnet add package EasyArguments --version 1.0.1
NuGet\Install-Package EasyArguments -Version 1.0.1
<PackageReference Include="EasyArguments" Version="1.0.1" />
<PackageVersion Include="EasyArguments" Version="1.0.1" />
<PackageReference Include="EasyArguments" />
paket add EasyArguments --version 1.0.1
#r "nuget: EasyArguments, 1.0.1"
#:package EasyArguments@1.0.1
#addin nuget:?package=EasyArguments&version=1.0.1
#tool nuget:?package=EasyArguments&version=1.0.1
<div align="center">
EasyArguments
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 | Versions 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. |
-
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.