Trarizon.TextCommand
0.0.2
dotnet add package Trarizon.TextCommand --version 0.0.2
NuGet\Install-Package Trarizon.TextCommand -Version 0.0.2
<PackageReference Include="Trarizon.TextCommand" Version="0.0.2" />
paket add Trarizon.TextCommand --version 0.0.2
#r "nuget: Trarizon.TextCommand, 0.0.2"
// Install Trarizon.TextCommand as a Cake Addin
#addin nuget:?package=Trarizon.TextCommand&version=0.0.2
// Install Trarizon.TextCommand as a Cake Tool
#tool nuget:?package=Trarizon.TextCommand&version=0.0.2
Trarizon.TextCommand(CN)
For CLI-like input parsing, written with source generator.
The library is still under designing and developing, and writing test is such a hassle, so be careful
Use
(An example here) or here in another repo
For Type:
partial class Command
{
[Execution("/cmd")]
public partial bool Run(string input);
[Executor("foo", "bar")]
bool FooBar() => throw null!;
[Executor("ghoti")]
bool Ghoti(
[Flag("f")] bool flag,
[Option("op")] string? option,
[Value] int value,
[Values(5)] ReadOnlySpan<int> values)
=> throw null!;
}
The following code will be generated (simplified):
The generated code split input
into string array, match leading words with list pattern,
and the rest parts are using for argument parsing.
partial class Command
{
public partial bool Run(string input)
{
switch (input.SplitByWhiteSpaces()) {
case ["/cmd", "foo", "bar", ..]:
return this.FooBar();
case ["/cmd", "ghoti", .. var rest]:
var provider = ParameterSets.Ghoti.Parse(rest);
return Ghoti(
provider.GetFlag<bool, BooleanFlagParser>("--flag", parser: default),
provider.GetOption<string, ParsableParser<string>>("--option", parser: default, false),
provider.GetValue<int, ParsableParser<int>>(0, parser: default, null),
provider.GetValues<int, ParsableParser<int>>(0, stackalloc int[5], parser: default, "values", false));
}
return default;
}
}
file static class ParameterSets
{
public static readonly ParameterSet Ghoti = new();
}
You can set custom parser by set properties on attribute.
Rule
Current a type can only contains one
Execution
, multi-execution may be supported in the future.Execution
is a single-parameter method, the parameter type can bestring
,ReadOnlySpan<char>
,Span<string>
,ReadOnlySpan<string>
,string[]
,List<string>
.- When input is
string
orReadOnlySpan<char>
, the input will be split with white spaces.
- When input is
Use
""
to escape"
in raw string.You can mark multiple
[Executor]
on a method
API
Attribute | Comments |
---|---|
Execution |
Extrance of execution, a single-parameter method |
Executor |
Branches of execution |
Flag |
Flag parameter, pass a bool value to parser accoding to input |
Option |
Named option parameter,pass a ReadOnlySpan<char> value to parser |
Value |
Positional parameter, parse all unmatched arguments in order |
MultiValue |
Array of positional parameter, |
Attribute params | Comments |
---|---|
Parser |
Parser of parameter, See rules below |
Alias |
Constructor param. Alias of parameter, use with prefix - |
Name |
Name of parameer, default is the name of parameter |
Required |
Default is false . Exceptions threw when not existing if true |
MaxCount |
Constructor param. Max value count in MultiValue , all rest Value s if non-positive |
Parser Rules
Custom parsers should be field, Property or Method in current type.
For flag
, parser implements IArgFlagParser<T>
; for others, implements IArgParser<T>
.
Method parser should assignable to ArgParsingDelegate
or ArgFlagParsingDelegate
Build-in parser:
ParsableParser<T>
: ParseISpanParsable<T>
EnumParser<T>
: Parse enum typeBooleanFlagParser
: ParseFlag
tobool
BinaryFlagParser<T>
: ParseFlag
to one of specified values.DelegateParser<T>
: Wrap a parser delegateDelegateFlagParser<T>
: Wrap a parser delegateNullableParser<TParser, T>
: Wrap parser forNullable<T>
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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. |
-
net8.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 |
---|