Skip to content

DotNetCampus.CommandLine is probably the fastest command line parser in all .NET open-source projects. Parsing a classical command line only takes 34us.

License

Notifications You must be signed in to change notification settings

dotnet-campus/DotNetCampus.CommandLine

Repository files navigation

DotNetCampus.CommandLine

Build NuGet Package DotNetCampus.CommandLine dotnetCampus.CommandLine.Source

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.

Get Started

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:

Windows PowerShell Style

> demo.exe "C:\Users\lvyi\Desktop\demo.txt" -s -Mode Edit -StartupSessions A B C

Windows CMD Style

> demo.exe "C:\Users\lvyi\Desktop\demo.txt" /s /Mode Edit /StartupSessions A B C

Linux/GNU Style

$ demo.exe "C:/Users/lvyi/Desktop/demo.txt" -s --mode Edit --startup-sessions A --startup-sessions B --startup-sessions C

.NET CLI Style

> demo.exe "C:\Users\lvyi\Desktop\demo.txt" -s:true --mode:Edit --startup-sessions:A;B;C

Command Styles and Features

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

Engage, Contribute and Provide Feedback

Thank you very much for firing a new issue and providing new pull requests.

Issue

Click here to file a new issue:

Contributing Guide

Be kindly.

License

DotNetCampus.CommandLine is licensed under the MIT license.

About

DotNetCampus.CommandLine is probably the fastest command line parser in all .NET open-source projects. Parsing a classical command line only takes 34us.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors 3

  •  
  •  
  •  

Languages