Inact 0.2.3

dotnet add package Inact --version 0.2.3                
NuGet\Install-Package Inact -Version 0.2.3                
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="Inact" Version="0.2.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Inact --version 0.2.3                
#r "nuget: Inact, 0.2.3"                
#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 Inact as a Cake Addin
#addin nuget:?package=Inact&version=0.2.3

// Install Inact as a Cake Tool
#tool nuget:?package=Inact&version=0.2.3                

Inact

Inact is a framework for creating interactive terminal environments. There are two classes, which are used to specify the environment, namely the Inact class and the MethodConstruct class. Both classes contain options to customize the environment.

The Inact class

This class represents the environment being created. It has the following options:

Option Type Use Default
Mark Func<string> Gives the string that will be printed, when the enviroment is ready for a command A method that returns "> "
Setup MethodConstruct Will be executed, on the commandline arguments, when the environment begins A method that does nothing and succeds
Methods List<MethodConstruct> Contains the methods that are available in the environment. Note that if a MethodConstruct with either a non-unique, name or shorthand, is added to this list, an ArgumentException is thrown. 'help' and 'quit'
Cascade bool Describs whether or not the environment should shut down, if a subexpression of a command say it should false
Variables Map<string,string> Contains the variables of the environment Empty

The MethodConstruct class

This class represtens methods that can be executed from the environment. It has the following options:

Option Type Use Default
Name string The full name of the method. Note that an ArgumentException is thrown if this is null, empty or contains spaces. Required in the constructor
Shorthand string A short name for the method. Note that an ArgumentException is thrown if this is the empty string or contains spaces. null
Description string A description of what the method does null
Passable bool Defines whether a call to the method can be given as an argument to another method false
Params_desc string A description of the parameters the method takes null
Params_check Predicate<string[]> A method that checks that the given parameters are valid A method that returns true
Method (string[]) ⇒ MethodResult The method body A method that always return Failure("No method defined")

How to specify an environment

There are two ways of specifying an Inact environment.

Method-based specification

For this, an Inact object must be instantiated. It is then possible to specify the environment, using method calls on the object.

An example:

var env = new Inact();

env.SetMarkFunc(() => "My environment >> ")

env.SetSetup(
    new MethodConstruct("SETUP") {
        Params_info = "Takes nothing",
        Params_check = ps => true,
        Method = ps => {
            return new Succes("Environment ready!");
        }
    }
);

env.AddMethod(
    new MethodConstruct("echo") {
        Shorthand = "e",
        Description = "Prints its parameter",
        Params_info = "Takes 1 parameter",
        Params_check = ps => ps.Length == 1,
        Method = ps => {
            return new Succes(ps[0]);
        }
    }
);

env.Run();

Object-based specification

For this, a class is created, and then, using attributes, the environment is specified. An instance of the class, is given to the InactFactory to construct the Inact object. Three imporant point with using this method of specification is:

  • The name of methods with the InactMethod attribute, will be used as the MethodConstruct Name
  • The Params_check can only check that there where given a valid number of arguments

An example:

public class EnvSpec
{
    [InactMark]
    public string mark()
    {
        return "My environment >> ";
    }

    public EnvSpec() 
    {
        Console.WriteLine("Environment ready!");
    }

    [InactMethod]
    [Shorthand("e")]
    [Description("Prints its parameter")]
    [Params_info("Takes 1 parameter")]
    [Params_check(1)]
    public MethodResult echo(string[] ps)
    {
        return new Succes(ps[0]);
    }
}
var env = InactFactory.CreateFrom(new EnvSpec());

env.Run();

How to use an environment

When an environment is running, you will interact with it by given it commands to execute. Commands build out of the following expressions:

Expression Examples
Value words, echo, "more words"
Lookup $varname
Call echo "more words", echo words → varname, (echo words)

Rules

  1. Commands must start with a Value expression, with a value that is either the Name or Shorthand of a MethodConstruct in the environment.
  2. All following expression, will be resolved, and the results used as arguments.
  3. If the result of resolving any expression in a command is Failure, it will immediatly be the result of the entire command.
  4. To use a call to a method as an argument to another, the call must be surronded by parentheses.
  5. To save the result of a method in a variable, an arrow followed by a word is used. This must be the final part of a call.

Examples of well formed commands

Command Result
echo hi Success("hi")
echo hi → greet1 Success("hi")
echo $greet1 Success("hi")
echo (echo hi) Success("hi")
echo (echo hi → greet2) Success("hi")
echo $greet2 Success("hi")
echo "hello there" Success("hello there")

Examples of ill formed commands

Command Result
$greet1 hi Failure("Command must start with a word")
echo → greet3 hi Failure("Saver (-> WORD) must be the last part of a method call")
echo echo hi) Failure("Closing parentheses without opening")
echo (echo hi Failure("Missing a closing Parentheses")

Example usage of an environment

C:\Program\Interactive> dotnet run
Environment ready!
My environment >> h
[help | h] Prints a definition of all Methods
[quit | q] Terminates this interactive environment
[echo | e] Takes 1 parameter and Prints its parameter

My environment >> echo hello world
Failure: [echo | e] Takes 1 parameter and Prints its parameter
My environment >> echo "hello world"
hello world
My environment >> q

C:\Program\Interactive>
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
0.2.3 458 4/8/2022
0.2.2 437 3/12/2022
0.2.1 428 3/11/2022
0.2.0 418 3/9/2022
0.1.0 424 3/8/2022