Skip to content

OrgEleCho/EleCho.CommandLine

Repository files navigation

EleCho.CommandLine nuget

Simple commandline framework.

Usage

Create a commandline app.

class MyCommandLineApp : CommandLineApp
{
    [Command]
    public void Echo(string text, bool toUpper)
    {
        if (toUpper)
            text = text.ToUpper();

        Console.WriteLine(text);
    }
}

Execute commands from user input.

MyCommandLineApp app = new MyCommandLineApp();
while (true)
{
    Console.Write("> ");
    var input = Console.ReadLine();
    if (input == null)
        return;

    try
    {
        object? rst =
            app.Execute(input);

        if (rst != null)
            Console.WriteLine(rst);
    }
    catch(Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

Preview:

/echo "hello world"
hello world
/echo --to-upper "hello world"
HELLO WORLD

Use custom Option:

[Command]
public void Echo(string text, bool toUpper, [Option("name")] string? myName = null)
{
    if (toUpper)
        text = text.ToUpper();

    if (myName != null)
        Console.Write($"{myName}: ");

    Console.WriteLine(text);
}

Use short name for Option:

[Command]
public void Echo(string text, bool toUpper, [Option("name", 'n')] string? myName = null)
{
    if (toUpper)
        text = text.ToUpper();

    if (myName != null)
        Console.Write($"{myName}: ");

    Console.WriteLine(text);
}

Variable number of arguments are also supported:

[Command]
public int Sum(params int[] nums)
{
    return nums.Sum();
}

All basic types like numbers, string, char, enum are supported.

About

Simple commandline framework. 简易命令行框架.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages