X-ToolZ.SimpleParser 2.0.1

dotnet add package X-ToolZ.SimpleParser --version 2.0.1
                    
NuGet\Install-Package X-ToolZ.SimpleParser -Version 2.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="X-ToolZ.SimpleParser" Version="2.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="X-ToolZ.SimpleParser" Version="2.0.1" />
                    
Directory.Packages.props
<PackageReference Include="X-ToolZ.SimpleParser" />
                    
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 X-ToolZ.SimpleParser --version 2.0.1
                    
#r "nuget: X-ToolZ.SimpleParser, 2.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 X-ToolZ.SimpleParser@2.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=X-ToolZ.SimpleParser&version=2.0.1
                    
Install as a Cake Addin
#tool nuget:?package=X-ToolZ.SimpleParser&version=2.0.1
                    
Install as a Cake Tool

SimpleParser

A simple, attribute-based command line argument parser for .NET.

Define options using attributes and let SimpleParser handle the rest.

Installation

dotnet add package X-ToolZ.SimpleParser

Quick Start

using X_ToolZ;

class Options
{
    [Option("n", "name", "Your name")]
    public string Name { get; set; }

    [BoolOption("v", "verbose", "Enable verbose output")]
    public bool Verbose { get; set; }
}

var options = new Options();
var result = SimpleParser.Parse(args, options);

if (result.IsSuccessful)
{
    Console.WriteLine($"Hello, {options.Name}!");
}
else
{
    Console.WriteLine($"Error: {result.Message}");
}

Option Attributes

Option

Standard option for string, int, and other value types.

[Option("o", "output", "Output file path")]
public string OutputPath { get; set; }

[Option("c", "count", "Number of iterations")]
public int Count { get; set; }

Supports multiple syntaxes:

  • -o value
  • --output value
  • --output=value

BoolOption

Flag option that sets a boolean to true when present.

[BoolOption("v", "verbose", "Enable verbose mode")]
public bool Verbose { get; set; }

[BoolOption("q", "quiet")]
public bool Quiet { get; set; }

Usage: -v or --verbose

ListOption

Parse comma-separated (or custom separator) values into a typed list.

[ListOption("f", "files", "Input files")]
public List<string> Files { get; set; }

[ListOption("n", "numbers", "List of numbers")]
public List<int> Numbers { get; set; }

[ListOption("v", "values", ';', "Semicolon-separated values")]
public List<double> Values { get; set; }

Usage: -f file1.txt,file2.txt,file3.txt

IndexOption

Get argument by position (zero-based index).

[IndexOption(0, "Command to execute")]
public string Command { get; set; }

[IndexOption(1, "Target file")]
public string Target { get; set; }

Usage: myapp build project.csproj → Command="build", Target="project.csproj"

EnumOption

Parse string values into enum types (case-insensitive).

public enum LogLevel { None, Info, Warning, Error }

[EnumOption("l", "level", "Log level")]
public LogLevel Level { get; set; }

[EnumOption("m", "mode")]
public LogLevel? OptionalMode { get; set; }  // Nullable enum supported

Usage: -l warning or --level=Error

Constructor Overloads

When using 2-argument attribute constructors, C# matches (shortName, description) not (shortName, longName). To specify a long name with only 2 arguments, use named parameters:

// These are equivalent:
[Option("n", "name", "")]           // 3 args: shortName, longName, description
[Option("n", longName: "name")]     // Named parameter for longName

Error Handling

SimpleParser returns a ParseResult with detailed error messages:

var result = SimpleParser.Parse(args, options);

if (!result.IsSuccessful)
{
    Console.WriteLine(result.Message);
    // Examples:
    // "Option '--output' for property 'OutputPath' requires a value."
    // "Cannot convert 'abc' to type 'Int32' for property 'Count'."
    // "Value 'invalid' is not valid for enum 'LogLevel'. Valid values: None, Info, Warning, Error."
    return 1;
}

Usage Help

Generate usage information automatically:

Console.WriteLine(SimpleParser.CreateUsage(options));

Output:

Command line options for MyApp:
Option [string] Name -n or --name (Your name)
BoolOption [bool] Verbose -v or --verbose (Enable verbose output)
ListOption [list`1] Files -f or --files (Input files)
EnumOption [LogLevel] Level -l or --level (Log level)

Complete Example

using X_ToolZ;

class Options
{
    [IndexOption(0, "Command")]
    public string Command { get; set; }

    [Option("o", "output", "Output directory")]
    public string OutputDir { get; set; }

    [BoolOption("v", "verbose", "Verbose output")]
    public bool Verbose { get; set; }

    [BoolOption("f", "force", "Force overwrite")]
    public bool Force { get; set; }

    [ListOption("i", "input", "Input files")]
    public List<string> InputFiles { get; set; }

    [EnumOption("l", "level", "Log level")]
    public LogLevel LogLevel { get; set; }
}

public enum LogLevel { None, Info, Warning, Error }

class Program
{
    static int Main(string[] args)
    {
        var options = new Options();
        var result = SimpleParser.Parse(args, options);

        if (!result.IsSuccessful)
        {
            Console.WriteLine($"Error: {result.Message}");
            Console.WriteLine(SimpleParser.CreateUsage(options));
            return 1;
        }

        Console.WriteLine($"Command: {options.Command}");
        Console.WriteLine($"Output: {options.OutputDir}");
        Console.WriteLine($"Verbose: {options.Verbose}");
        Console.WriteLine($"Files: {string.Join(", ", options.InputFiles ?? new())}");
        Console.WriteLine($"Log Level: {options.LogLevel}");

        return 0;
    }
}

Example usage:

myapp build -o ./dist -v --input=src/a.cs,src/b.cs --level=Info

License

MIT

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • 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
2.0.1 160 12/20/2025