MiniCommandLineParser 1.0.3.1
dotnet add package MiniCommandLineParser --version 1.0.3.1
NuGet\Install-Package MiniCommandLineParser -Version 1.0.3.1
<PackageReference Include="MiniCommandLineParser" Version="1.0.3.1" />
<PackageVersion Include="MiniCommandLineParser" Version="1.0.3.1" />
<PackageReference Include="MiniCommandLineParser" />
paket add MiniCommandLineParser --version 1.0.3.1
#r "nuget: MiniCommandLineParser, 1.0.3.1"
#:package MiniCommandLineParser@1.0.3.1
#addin nuget:?package=MiniCommandLineParser&version=1.0.3.1
#tool nuget:?package=MiniCommandLineParser&version=1.0.3.1
MiniCommandLineParser
A simple, lightweight, and dependency-free command-line parsing library for .NET.
âĻ Features
- ðŠķ Lightweight - Minimal footprint, no external dependencies
- ðŊ Simple API - Intuitive attribute-based configuration
- ðĶ Multi-target - Supports .NET 6/7/8/9 and .NET Standard 2.1
- ð Bidirectional - Parse arguments to objects AND format objects back to command-line strings
- ð Auto Help Text - Built-in help text generation with default value display
- ð§ Flexible - Supports short/long options, arrays, enums, flags, and more
- ð Positional Arguments - Support index-based positional parameters (e.g.,
app clone http://...) - âïļ Custom Separators - Split array values with custom separators (e.g.,
--tags=a;b;c) - ð Environment Variables - Fallback to environment variables when options not provided
- â ïļ Structured Errors - Typed error handling with
ParseErrorandParseErrorType - ⥠High Performance - Thread-safe TypeInfo caching for efficient parsing
ðĨ Installation
dotnet add package MiniCommandLineParser
ð Quick Start
1. Define Your Options Class
using MiniCommandLineParser;
public class Options
{
[Option('i', "input", Required = true, HelpText = "Input file path")]
public string InputFile { get; set; }
[Option('o', "output", HelpText = "Output file path")]
public string OutputFile { get; set; }
[Option('v', "verbose", HelpText = "Enable verbose output")]
public bool Verbose { get; set; }
[Option("count", HelpText = "Number of iterations")]
public int Count { get; set; } = 1;
[Option("tags", HelpText = "List of tags")]
public List<string> Tags { get; set; }
}
2. Parse Command-Line Arguments
var result = Parser.Default.Parse<Options>(args);
if (result.Result == ParserResultType.Parsed)
{
var options = result.Value;
Console.WriteLine($"Input: {options.InputFile}");
}
else
{
Console.WriteLine(result.ErrorMessage);
// Access structured errors
foreach (var error in result.Errors)
{
Console.WriteLine($"[{error.ErrorType}] {error.OptionName}: {error.Message}");
}
}
ð Environment Variable Support
Fallback to environment variables when command-line options are not provided:
public class Options
{
[Option('c', "config", EnvironmentVariable = "APP_CONFIG", HelpText = "Config file path")]
public string ConfigFile { get; set; }
[Option("api-key", EnvironmentVariable = "API_KEY", HelpText = "API key for authentication")]
public string ApiKey { get; set; }
[Option("timeout", EnvironmentVariable = "APP_TIMEOUT", HelpText = "Timeout in seconds")]
public int Timeout { get; set; } = 30;
}
// Command line takes precedence over environment variables
// If --config is not provided, APP_CONFIG environment variable is used
// If neither is set, property keeps its default value
Priority order: Command-line argument > Environment variable > Default value
ð Positional Arguments
Support CLI-style positional parameters without option names:
public class CloneOptions
{
[Option("command", Index = 0, HelpText = "Command name")]
public string Command { get; set; }
[Option("url", Index = 1, HelpText = "Repository URL")]
public string Url { get; set; }
[Option('v', "verbose", HelpText = "Verbose output")]
public bool Verbose { get; set; }
}
// Parse: myapp clone https://github.com/user/repo --verbose
// Result: Command="clone", Url="https://github.com/user/repo", Verbose=true
âïļ Custom Array Separators
Split array values using custom separators:
public class BuildOptions
{
// Default separator is ';'
[Option("tags", Separator = ';', HelpText = "Tags separated by semicolon")]
public List<string> Tags { get; set; }
// Custom separator
[Option("ids", Separator = ',', HelpText = "IDs separated by comma")]
public int[] Ids { get; set; }
}
// Parse: --tags=dev;test;prod --ids=1,2,3
// Result: Tags=["dev","test","prod"], Ids=[1,2,3]
â ïļ Structured Error Handling
Access detailed error information with typed errors:
var result = Parser.Default.Parse<Options>(args);
if (result.Result == ParserResultType.NotParsed)
{
foreach (var error in result.Errors)
{
switch (error.ErrorType)
{
case ParseErrorType.MissingRequired:
Console.WriteLine($"Missing required option: {error.OptionName}");
break;
case ParseErrorType.InvalidValue:
Console.WriteLine($"Invalid value for {error.OptionName}: {error.Message}");
break;
case ParseErrorType.UnknownOption:
Console.WriteLine($"Unknown option: {error.OptionName}");
break;
}
}
}
Error Types:
| Type | Description |
|------|-------------|
| MissingRequired | A required option was not provided |
| InvalidValue | The value could not be converted to the expected type |
| UnknownOption | An unrecognized option was provided |
ð Supported Argument Formats
# Short options
-v -i input.txt
# Long options
--verbose --input input.txt
# Equals syntax
--input=input.txt --config="key=value"
# Boolean options (all equivalent)
--verbose
--verbose=true
--verbose true
# Quoted values (with spaces)
--output "my output file.txt"
# Array values (space-separated)
--tags tag1 tag2 tag3
# Array values (with separator)
--tags=tag1;tag2;tag3
# Positional arguments
clone https://example.com --verbose
# Flags enum
--flags Flag1 Flag2 Flag3
ð Format Object to Command Line
Convert objects back to command-line strings with flexible formatting options:
var options = new Options { InputFile = "test.txt", Verbose = true };
// Full output (all options with space syntax)
string cmdLine = Parser.FormatCommandLine(options, CommandLineFormatMethod.Complete);
// Output: --input test.txt --verbose True --count 1
// Simplified output (only non-default values)
string simplified = Parser.FormatCommandLine(options, CommandLineFormatMethod.Simplify);
// Output: --input test.txt --verbose True
// Equal sign style (use = between option and value)
string equalStyle = Parser.FormatCommandLine(options, CommandLineFormatMethod.EqualSignStyle);
// Output: --input=test.txt --verbose=True --count=1
// Combine flags: Simplify + EqualSignStyle
string combined = Parser.FormatCommandLine(options,
CommandLineFormatMethod.Simplify | CommandLineFormatMethod.EqualSignStyle);
// Output: --input=test.txt --verbose=True
Format Method Flags
| Flag | Description |
|---|---|
None |
Default space-separated style |
Complete |
Output all options including defaults |
Simplify |
Only output non-default values |
EqualSignStyle |
Use --option=value syntax, arrays use separator |
ð Auto-Generate Help Text
var helpText = Parser.GetHelpText(new Options());
Console.WriteLine(helpText);
Help text automatically shows:
- Option names (short and long)
- Required/Optional status
- Default values
- Environment variable names
- Type information (Array, Enum, Flags)
âïļ Parser Settings
var parser = new Parser(new ParserSettings
{
CaseSensitive = false, // Case-insensitive matching (default)
IgnoreUnknownArguments = true // Ignore unknown arguments (default)
});
⥠Performance
- TypeInfo Caching: Type metadata is cached and reused across parse calls
- Thread-Safe: Safe for concurrent usage in multi-threaded applications
- Minimal Allocations: Optimized for low memory overhead
// TypeInfo is cached - subsequent calls are fast
var typeInfo = Parser.GetTypeInfo<Options>();
ð License
MIT License - see GitHub Repository for details.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. 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 is compatible. 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 is compatible. 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 is compatible. 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 | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- No dependencies.
-
net10.0
- No dependencies.
-
net6.0
- No dependencies.
-
net7.0
- No dependencies.
-
net8.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.