Thinkmine.CommandLine 1.0.6

There is a newer version of this package available.
See the version list below for details.
dotnet add package Thinkmine.CommandLine --version 1.0.6
NuGet\Install-Package Thinkmine.CommandLine -Version 1.0.6
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="Thinkmine.CommandLine" Version="1.0.6" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Thinkmine.CommandLine --version 1.0.6
#r "nuget: Thinkmine.CommandLine, 1.0.6"
#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 Thinkmine.CommandLine as a Cake Addin
#addin nuget:?package=Thinkmine.CommandLine&version=1.0.6

// Install Thinkmine.CommandLine as a Cake Tool
#tool nuget:?package=Thinkmine.CommandLine&version=1.0.6

Thinkmine Command line processor

Provides a simple attribute-driven low-code API for command-line processing, using an approach similar to Web API routing.

Usage

Creating patterns

For any given class -

  1. Add static methods that return a bool for each method you want to handle a given command pattern.
  2. Decorate the method with CommandPatternAttribute
  3. As a catch-all for any input that does not match you can optionally provide another method (with a similar signature as is described above) and decorate it with CommandNotFoundAttribute instead

CommandPatternAttribute accepts a string as an argument. This string can contain any sequence of characters. To add placeholders to the pattern use the open and close brakets. In the following example a command pattern handler is created

[CommandPattern("say -what {something} -to {someone}")]
static bool SaySomething(string something, string someone, int i)
{
    Console.WriteLine($"You said {something} to {someone}");
    return true;
}

This method will be invoked whenever the following pattern is passed to the interpreter say -what [...] -to [...] . The placeholders in the pattern must match the parameter names of the method that it decorates (similar to Web API).
If placeholders are provided but the method does not have paramters (or they dont match) then the parameters will hold their default value when the method is run. This is also true in the reverse scenario; if no placeholders are included the method will be called with the default values of any parameters that exist. The return value is not used by the processor; rather, it is meant to be used in REPL scenarios to indicate whether the loop should continue. THe convension is to return true to continue the program and false to exit it.

In the following example we pass the following:

say hello world to the man next door

The result of this would be:

You said hello world to the man next door

A single method may be decorated with more than one command pattern (thus allowing it to handle multiple scenarios). In the following example the same SayHello method is used to handle three different patterns.

[CommandPattern("test {arg}")]
[CommandPattern("test")]
[CommandPattern("hello there")]
static bool SayHello(string arg)
{
    Console.WriteLine($"Calling hello there command with {arg}");
    return true;
}

This method will be invoked if any of the following patterns is passed into the processor.

  1. test
  2. test 123
  3. hello there

Running the Processor

To initialize the processor use the following code:

CommandLineProcessor.Initialize();

This will traverse the current assembly and any referenced assemblies and identify all the command patterns that have been specified. A command pattern is declared by decorating a method of a certain signature: static with a boolean return type with the CommandPatternAttribute. Initialize must always be called first, and must be called again each time a new assembly is added to the AppDomain it was initially called in.

The processor can be used in two ways: For 1-time command line argument processing you can call CommandLineProcessor.Execute(string[]);

This version of the execute method accepts an array of strings. This can be the arguments passed into the console itself (but it really could be anything). No special processing is performed on the array other than turning it into a space-delimited string. So if the array: [hello,world,i,like,to,code] it would be converted to hello world i like to code. This version of the method returns void.

For creating something like a REPL (Read-evaluate-print loop) the other version of the method is more ideal. CommandLineProcessor.Execute(string);

This version of the method takes the entire string as input and processes it with no additional manipulation. This version returns a bool that can be used to determine whether it should be called again.

The following shows how a simple console REPL might look.

static void Main(string[] args)
{
    Console.WriteLine("Pidgin REPL Test");
    //call initialization
    CommandLineProcessor.Initialize();

    while (true)
    {
        Console.Write("> ");
        var command_line = Console.ReadLine();
        var continue_to_run = CommandLineProcessor.Execute(command_line);

        if (!continue_to_run)
            break;
    }

}

As can be seen from the sample, the Execute method returns a boolean value that is then used to determine whether the loop continues to run. The alternate method does not have a return type.

static void Main(string[] args)
{
    CommandLineProcessor.Initialize();
    CommandLineProcessor.Execute(args);
}
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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.
  • net6.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
1.2.1 324 1/9/2023
1.2.0-beta 110 1/8/2023
1.1.2-beta 122 1/7/2023
1.1.1-beta 106 1/7/2023
1.1.0-alpha 115 1/7/2023
1.0.7 257 1/5/2023
1.0.6 246 1/5/2023
1.0.5 257 1/5/2023
1.0.0 312 11/15/2022