Trarizon.TextCommand
0.1.0.2
dotnet add package Trarizon.TextCommand --version 0.1.0.2
NuGet\Install-Package Trarizon.TextCommand -Version 0.1.0.2
<PackageReference Include="Trarizon.TextCommand" Version="0.1.0.2" />
paket add Trarizon.TextCommand --version 0.1.0.2
#r "nuget: Trarizon.TextCommand, 0.1.0.2"
// Install Trarizon.TextCommand as a Cake Addin
#addin nuget:?package=Trarizon.TextCommand&version=0.1.0.2
// Install Trarizon.TextCommand as a Cake Tool
#tool nuget:?package=Trarizon.TextCommand&version=0.1.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
(My test examples 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,
[MultiValue(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 = ParsingContextProvider.Ghoti.Parse(rest);
var errorBuilder = new();
var arg0 = provider.GetFlag<bool, FlagParser>("--flag", parser: default);
errorBuilder.AddWhenError(arg0);
var arg1 = provider.GetOption<string, Parser>("--option", parser: default);
errorBuilder.AddWhenError(arg1);
var arg2 = provider.GetValue<int, Parser>(0, parser: default);
errorBuilder.AddWhenError(arg2);
var arg3 = provider.GetValues<int, Parser>(0, parser: default, stackalloc[5]));
errorBuilder.AddWhenError(arg3);
if (errorBuilder.HasError)
return errorBuilder.DefaultErrorHandler();
else
return Ghoti(arg0, arg1, arg2, arg3);
}
return default;
}
}
file static class ParsingContextProvider
{
public static readonly ParsingContext Ghoti = new();
}
You can set custom parser by set properties on attribute.
Rule
- Only single
Execution
method in a type, which accept single parameter instring
,ReadOnlySpan<char>
- If input is
string
orReadOnlySpan<char>
, the input will be split with white spaces.
- If input is
- Use
""
escape"
- You can mark multiple
[Executor]
on a method - Custom parser is allowed. Default parser are provided for common types, see Parsing
- Custom error handler is allowes. See ErrorHandler
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 |
---|---|
ErrorHandler |
Custom error handling. See ErrorHandler |
Parser <br/>ParserType |
Use one of these. Parser of parameter. See Parsing |
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 |
Parsing
Default Parsers
bool
isflag
by default, usingBooleanFlagParser
ISpanParsable<T>
useParsableParser<T>
defaultenum
types useEnumParser<TEnum>
default- For
Nullable<T>
, if there's default or provided parser forT
, useNullableParser<T, TParser>
to wrap - For any type
T
, if there's provided parser forT2
, andT2
can implicit convert intoT
, useConversion<T2, T, TParser>
to wrap - For custom method parsers, use
DelegateParser<T>
orDelagateFlagParser<T>
to wrap - For custom type parser
TParser
, usedefault(TParser)
Custom parser
Custom parsers should be field, property or method in current type. Or any value type implements required interfaces.
- For
flag
, parser type implementsIArgFlagParser<T>
, method parser should matchT Parse(bool)
- For others, parser type implements
IArgParser<T>
, method parser should matchbool TryParse(InputArg, out T)
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 delegateWrapped.NullableParser<T, TParser>
: Wrap parser forNullable<T>
Wrapped.ConversionParser<T, TResult, TParser>
: Convert result of parser into another type
ErrorHandler
By default, we use ArgResultErrors.Builder.DefaultErrorHandler()
Custom error handler should be method in current type, and match following rules:
- Return type of current type should be void or implicit converted to return type of execution method.
- First parameter is
ArgResultErrors
, could be marked within
- Second parameter is optional, with type
string
, means the executor method name where the error comes.
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 |
---|