Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/xenoatom/xenoatom.commandline
A lightweight, powerful and NativeAOT friendly command line parser .NET library.
https://github.com/xenoatom/xenoatom.commandline
command-line command-line-parser dotnet dotnet-core
Last synced: 14 days ago
JSON representation
A lightweight, powerful and NativeAOT friendly command line parser .NET library.
- Host: GitHub
- URL: https://github.com/xenoatom/xenoatom.commandline
- Owner: XenoAtom
- License: other
- Created: 2024-05-04T17:01:18.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2024-06-22T07:17:52.000Z (7 months ago)
- Last Synced: 2024-12-17T11:40:26.626Z (24 days ago)
- Topics: command-line, command-line-parser, dotnet, dotnet-core
- Language: C#
- Homepage:
- Size: 321 KB
- Stars: 64
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- Funding: .github/FUNDING.yml
- License: license.txt
Awesome Lists containing this project
README
# XenoAtom.CommandLine [![ci](https://github.com/XenoAtom/XenoAtom.CommandLine/actions/workflows/ci.yml/badge.svg)](https://github.com/XenoAtom/XenoAtom.CommandLine/actions/workflows/ci.yml) ![coverage](https://gist.githubusercontent.com/xoofx/4b1dc8d0fa14dd6a3846e78e5f0eafae/raw/dotnet-releaser-coverage-badge-XenoAtom-XenoAtom.CommandLine.svg) [![NuGet](https://img.shields.io/nuget/v/XenoAtom.CommandLine.svg)](https://www.nuget.org/packages/XenoAtom.CommandLine/)
**XenoAtom.CommandLine** is a lightweight, powerful and NativeAOT friendly command line parser.
It is a fork of the excellent [NDesk.Options](http://www.ndesk.org/Options)/[Mono.Options](https://tirania.org/blog/archive/2008/Oct-14.html) with several small improvements and new features.
## โจ Features
- Lightweight library with no dependencies
- `net8.0`+ ready and NativeAOT friendly, no `System.Reflection` used
- Provides a simple API to parse command line arguments
- Generates a help message from the command line definition
- What you declare is what you get!
- Supports
- Commands and sub-command parsing (e.g. `git commit -m "message"`)
- Tar and POSIX style options (e.g. `-abc` is equivalent to `-a -b -c`)
- `-`, `/` and `--` option prefixes (e.g. `-v`, `/v`, `--verbose`))
- Multiple option values (e.g. `-i foo -i bar`)
- Optional and required option values `:` (e.g. `-o[BAR] -oBAR`)
- Key/value pairs (e.g. `-DMACRO=VALUE1)
- Option aliases (e.g. `-v`, `-verbose`)
- `--` to stop option parsing
- `--help` and `--version` built-in options
- Parsing of values to specific target types (e.g. `int`, `bool`, `enum`, etc.))
- Response files e.g `@file.txt`
- Grouping of command/options that can be activated together when a specific condition is met.## ๐งช Example
```csharp
using System;
using XenoAtom.CommandLine;const string _ = "";
string? name = null;
int age = 0;
List<(string, string?)> keyValues = new List<(string, string?)>();
List messages = new List();var commandApp = new CommandApp("myexe")
{
_,
{"D:", "Defines a {0:name} and optional {1:value}", (key, value) =>
{
if (key is null) throw new OptionException("The key is mandatory for a define", "D");
keyValues.Add((key, value));
}},
{"n|name=", "Your {NAME}", v => name = v},
{"a|age=", "Your {AGE}", (int v) => age = v},
new HelpOption(),
_,
"Available commands:",
new Command("commit")
{
_,
{"m|message=", "Add a {MESSAGE} to this commit", messages},
new HelpOption(),// Action for the commit command
(ctx, arguments) =>
{
ctx.Out.WriteLine($"Committing with name={name}, age={age}");
foreach (var message in messages)
{
ctx.Out.WriteLine($"Commit message: {message}");
}
return ValueTask.FromResult(0);
}
},
// Default action if no command is specified
(ctx, _) =>
{
ctx.Out.WriteLine($"Hello {name}! You are {age} years old.");
if (keyValues.Count > 0)
{
foreach (var keyValue in keyValues)
{
ctx.Out.WriteLine($"Define: {keyValue.Item1} => {keyValue.Item2}");
}
}return ValueTask.FromResult(0);
}
};await commandApp.RunAsync(args);
```Running `myexe --help` will output:
```
Usage: myexe [Options] COMMAND-D[=name:value] Defines a name and optional value
-n, --name=NAME Your NAME
-a, --age=AGE Your AGE
-h, -?, --help Show this message and exitAvailable commands:
commit
```Running `myexe --name John -a50` will output:
```
Hello John! You are 50 years old.
```Running `myexe --name John -a50 -DHello -DWorld=121` will output:
```
Hello John! You are 50 years old.
Define: Hello =>
Define: World => 121
```Running `myexe commit --help` will output:
```
Usage: myexe commit [Options]-m, --message=MESSAGE Add a MESSAGE to this commit
-h, -?, --help Show this message and exit
```Running `myexe --name John -a50 commit --message "Hello!" --message "World!"` will output:
```
Committing with name=John, age=50
Commit message: Hello!
Commit message: World!
```## ๐ User Guide
For more details on how to use XenoAtom.CommandLine, please visit the [user guide](https://github.com/XenoAtom/XenoAtom.CommandLine/blob/main/doc/readme.md).
## ๐๏ธ Build
You need to install the [.NET 8 SDK](https://dotnet.microsoft.com/download/dotnet/8.0). Then from the root folder:
```console
$ dotnet build src -c Release
```## ๐ชช License
This software is released under the [BSD-2-Clause license](https://opensource.org/licenses/BSD-2-Clause).
The license also integrate the original MIT license from [Mono.Options](https://github.com/mono/mono/blob/main/mcs/class/Mono.Options/Mono.Options/Options.cs).
## ๐ค Author
Alexandre Mutel aka [xoofx](https://xoofx.github.io).