English | 简体中文 | 繁體中文 |
---|
DotNetCampus.CommandLine is a simple yet high-performance command line parsing library for .NET. Thanks to the power of source code generators, it provides efficient parsing capabilities with a developer-friendly experience.
Parsing a typical command line takes only about 0.8μs (microseconds), making it one of the fastest command line parsers available in .NET.
For your program Main
method, write this code:
class Program
{
static void Main(string[] args)
{
// Create a new instance of CommandLine type from command line arguments
var commandLine = CommandLine.Parse(args);
// Parse the command line into an instance of Options type
// The source generator will automatically handle the parsing for you
var options = commandLine.As<Options>();
// Now use your options object to implement your functionality
}
}
Define a class that maps command line arguments:
class Options
{
[Value(0)]
public required string FilePath { get; init; }
[Option('s', "silence")]
public bool IsSilence { get; init; }
[Option('m', "mode")]
public string? StartMode { get; init; }
[Option("startup-sessions")]
public IReadOnlyList<string> StartupSessions { get; init; } = [];
}
Then use different command line styles to populate instances of this type:
> demo.exe "C:\Users\lvyi\Desktop\demo.txt" -s -Mode Edit -StartupSessions A B C
> demo.exe "C:\Users\lvyi\Desktop\demo.txt" /s /Mode Edit /StartupSessions A B C
$ demo.exe "C:/Users/lvyi/Desktop/demo.txt" -s --mode Edit --startup-sessions A --startup-sessions B --startup-sessions C
> demo.exe "C:\Users\lvyi\Desktop\demo.txt" -s:true --mode:Edit --startup-sessions:A;B;C
The library supports multiple command line styles through CommandLineStyle
enum:
- Flexible (default): Intelligently recognizes multiple styles
- GNU: GNU standard compliant
- POSIX: POSIX standard compliant
- DotNet: .NET CLI style
- PowerShell: PowerShell style
Advanced features include:
- Support for various data types including collections and dictionaries
- Positional arguments with
ValueAttribute
- Required properties with C#
required
modifier - Command handling with verb support
- URL protocol parsing
- High performance thanks to source generators
Thank you very much for firing a new issue and providing new pull requests.
Click here to file a new issue:
Be kindly.
DotNetCampus.CommandLine is licensed under the MIT license.