Trarizon.TextCommand 0.0.1

The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package Trarizon.TextCommand --version 0.0.1
NuGet\Install-Package Trarizon.TextCommand -Version 0.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Trarizon.TextCommand" Version="0.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Trarizon.TextCommand --version 0.0.1
#r "nuget: Trarizon.TextCommand, 0.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Trarizon.TextCommand as a Cake Addin
#addin nuget:?package=Trarizon.TextCommand&version=0.0.1

// Install Trarizon.TextCommand as a Cake Tool
#tool nuget:?package=Trarizon.TextCommand&version=0.0.1

Trarizon.TextCommand(CN)

For CLI-like input parsing, written with source generator.

Core is done, patching diagnostic analyzer...

Use

(An example here)

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, null));
        }
        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 be string, ReadOnlySpan<char>, Span<string>, ReadOnlySpan<string>, string[], List<string>.
    • You can use other types supports list pattern for string, but generator doesn't guarantee correctness. Error diagnostic may be added in the future.
    • When input is string or ReadOnlySpan<char>, the input will be split with white spaces.
  • Use "" to escape " in raw string.

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 Values 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

Buid-in parser:

  • ParsableParser<T> : Parse ISpanParsable<T>
  • EnumParser<T> : Parse enum type
  • BooleanFlagParser : Parse Flag to bool
  • BinaryFlagParser<T> : Parse Flag to one of specified values.
  • DelegateParser<T> : Wrap a parser delegate
  • DelegateFlagParser<T> : Wrap a parser delegate
  • NullableParser<TParser, T> : Wrap parser for Nullable<T>
Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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