CLI 0.9.1

dotnet add package CLI --version 0.9.1
                    
NuGet\Install-Package CLI -Version 0.9.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="CLI" Version="0.9.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CLI" Version="0.9.1" />
                    
Directory.Packages.props
<PackageReference Include="CLI" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CLI --version 0.9.1
                    
#r "nuget: CLI, 0.9.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.
#addin nuget:?package=CLI&version=0.9.1
                    
Install CLI as a Cake Addin
#tool nuget:?package=CLI&version=0.9.1
                    
Install CLI as a Cake Tool

CLI

Command line interface builder.

Usage

Program.cs

using CLI;

class Program
{
    static void Main(string[] args)
    {
        Commands commands = new Commands()
        {
            Prompt = "$ "
        };

        // add your commands here
        new Command
        {
            Name = "echo",
            Handler = args =>
            {
                var num = args.Get<int>("a");
                var str = args.Get("b");

                Console.WriteLine("a=" + num);
                Console.WriteLine("b=" + str);
            }
        };

        new Command
        {
            Name = "add",
            Handler = args =>
            {
                Console.WriteLine(args.Get<int>("#1") + args.Get<int>("#2"));
            }
        };

        // start command line loop
        commands.Initialize(args);
    }
}

Command line

$ echo -a 10 --b="hello friend"
$ a=10
$ b=hello friend
$
$ add 50 "-10"
$ 40

Async Commands

If you want async command to be awaited by the CLI then use AsyncHandler:

new Command
{
    Name = "example task2",
    Description = "This command will be awaited for",
    AsyncHandler = async args => {
        Console.WriteLine("getting back to you in a sec...");
        await System.Threading.Tasks.Task.Delay(1000);
        Console.WriteLine("done");
    }
};

new Command
{
    Name = "example task1",
    Description = "This command will not be awaited for",
    Handler = async args => {
        Console.WriteLine("getting back to you in a sec...");
        await System.Threading.Tasks.Task.Delay(1000);
        Console.WriteLine("done");
    }
};

Utilities

Prompting input

using static CLI.ConsoleUtil;

string count = PromptInput("How many? ");

// prompts for input, does not show typed characters in the console
string password = PromptInput("Password: ", hideInput: true);

Printing lists

IEnumarable<T> objects can be selectively printed in a table structure.

using static CLI.ConsoleUtil;

new Command
{
    Name = "ls",
    Handler = cnx =>
    {
        string path = GetPath(cnx.Get("#1"));

        var dirInfo = Directory.EnumerateFileSystemEntries(path).Select(p => new FileInfo(p)).ToList();

        PrintList(dirInfo,
            "CreationTime->Created(-20:yyyy-MM-dd HH:mm:ss)",
            "Name(-50)",
            "Attributes->Attrs");
    }
};
$ ls
Created              | Name                                               | Attrs
2023-02-13 20:35:43    CLI.dll                                              Archive
2023-02-13 20:35:43    CLI.pdb                                              Archive
2023-02-13 20:35:43    CLISample.deps.json                                  Archive
2023-02-13 20:35:43    CLISample.dll                                        Archive
2023-02-13 20:35:43    CLISample.exe                                        Archive
2023-02-13 20:35:43    CLISample.pdb                                        Archive
2023-02-13 20:35:43    CLISample.runtimeconfig.dev.json                     Archive
2023-02-13 20:35:43    CLISample.runtimeconfig.json                         Archive
2023-02-13 20:35:43    Scripts                                              Directory

Scripting

Create startup scripts and runtime scripts. Runtime scripts act as CLI commands. Scripts can be placed in the working directory and refereced with ./myscript.

Do not rely too much on the scripting syntax. It is subject to change in later development process.

Comprehensive Example

Program.cs

using CLI;

class Program
{
    static void Main(string[] args)
    {
        Commands commands = new Commands(true)
        {
            Prompt = "$ "
        };

        new ServerCommands();
        new FsCommands();

        new Command
        {
            Name = "echo",
            Handler = a =>
            {
                Console.WriteLine(a.Get("message", "m"));
            }
        };

        // startup scripts are executed on startup and are not available as commands on runtime.
        commands.UseStartupScript("./Scripts/startup1.txt");

        // runtime scripts are available just like predefined commands
        commands.UseScript("./Scripts/server.dump.txt");

        commands.Initialize(args);
    }
}

ServerCommands.cs

using CLI;

class ServerCommands
{
    Server server = null;

    public ServerCommands() {
        new Command
        {
            Name = "server",
            Handler => context => {
                if (server == null) {
                    return;
                }

                context.SetResult("status", string.Format("running on port {0}, health: {1}", server.Port, server.Health));
                context.SetResult("logStream", server.logStream);
                context.SetResult("eventStream", server.eventStream);
            }
        };

        new Command
        {
            Name = "server run",
            Handler = context => {
                var port = context.Get<int>("port", "p");
                var host = context.Get("host", "h");

                // run server somehow
                server = new Server(port, host);
                server.Start();

                context.SetResult("logStream", server.logStream);
                context.SetResult("eventStream", server.eventStream);
            }
        };
    }
}

FsCommands.cs

using System.IO;
using CLI;

class FsCommands
{
    public FsCommands()
    {
        new Command
        {
            Name = "cp",
            Handler = context => {
                var src = context.Get("src", "s");
                var dst = context.Get("dst", "dest", "d");
                // recursive copy entire directories or single file
            }
        };

        new Command
        {
            Name = "savestream",
            Handler = context => {
                var src = context.Get<Stream>("src", "s");
                var dst = context.Get("dst", "dest", "d");
                // save the stream to destination file
            }
        };
    }
}

startup1.txt

# This is a comment
# Assigning an output (illustrative) and using it
server.run(port: 5000, host: "example.com") -> server1
# Start a stream piping task
pipestream(stream: $server1.logStream, file: "./server.logs")

echo(m: "Server started on port 5000. Logs are available in ./server.logs")

server.dump.txt

# $destpath is script input parameter
server() -> server

savestream(src: $server.logStream, dst: $destpath)
savestream(src: $server.eventStream, dst: $destpath)

echo(m: "server info saved")

Command line

$ server dump --destpath=./dump1
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • .NETStandard 2.1

    • 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.9.1 114 2/24/2025
0.9.0 943 4/28/2024
0.8.0 626 2/13/2023
0.7.0 600 4/2/2022
0.6.1 2,212 8/23/2020
0.6.0 436 8/19/2020
0.5.4 453 8/8/2020
0.5.3 843 3/14/2020