TinyCLI · A tiny command-line interface for embedded systems

TinyCLI

A tiny command-line interface for embedded systems

TinyCLI is a command-line interface library for embedded systems. It allows you to register a set of commands (and optionally users), and then by directing input and output you automatically get a command-line interface with tab-completion, history, shortcut support, pattern matching, etc. It can also coexist with log or debug-output.

It is written in C, performs no dynamic allocations, and is intended to run on microcontrollers with or without an RTOS. Typical applications include providing a CLI over a serial line or a Telnet connection.

TinyCLI in action

The library is configured using compiler flags, and features can be deactivated to reduce memory usage to fit on memory-constrained systems.

The library is provided in two layers. Unless you have very specific requirements it is recommended to only use the high-level tclie API:

Prompt rendering, history, tab-completion, and prompt-preserving log output are common to both layers.

Quick-Start

Below is a minimal program that shows how to use the library:

#include <stdio.h>
#include "tclie.h"

/* Output callback. The library routes every emitted byte through this;
   on an MCU this would typically write to a UART. */
void out(void *arg, const char *str)
{
    printf("%s", str);
}

/* Command callback for "echo ...". */
int cmd_echo(void *arg, int argc, const char **argv)
{
    for (int i = 1; i < argc; i++)
        printf("%s%s", argv[i], i + 1 < argc ? " " : "\n");
    return 0;
}

/* Command table. The descriptions are printed by the built-in help command. */
static const tclie_cmd_t cmds[] = {
    { .name = "echo", .fn = cmd_echo, .desc = "Print arguments back" },
};

int main(void)
{
    /* Initialize and register command table. */
    tclie_t t;
    tclie_init(&t, out, NULL);
    tclie_reg_cmds(&t, cmds, sizeof(cmds) / sizeof(*cmds));

    /* Feed input characters. */
    int c;
    while ((c = getchar()) != EOF)
        tclie_in_char(&t, (char) c);
}

Documentation

Examples

The examples directory contains: