Add a command-line interface to any C++ program

Overview

Command Interface

This header-only library makes it easy to add command evaluation to a C++ program.

#include "command_interface.hpp"

class Arithmetic : public CommandInterface {
  static int add(int x, int y) {
    return x + y;
  }

  int inc(int x) {
    return x + 1;
  }

  void register_commands() override {
    register_command(add, "add", "Add two numbers");
    register_command(&Arithmetic::inc, "inc", "Increment a number");
  }
};

A command is simply a function. Just register any function with a helper string; type safety occurs automatically.

We can create a simple REPL with the above interface.

>> "; while (std::getline(std::cin, text)) { std::cout << arithmetic.eval(text) << std::endl << std::endl; std::cout << ">>> "; } return 0; } ">
#include <iostream>

int main() {
  Arithmetic arithmetic;
  std::string text;
  std::cout << ">>> ";
  while (std::getline(std::cin, text)) {
    std::cout << arithmetic.eval(text) << std::endl << std::endl;
    std::cout << ">>> ";
  }
  return 0;
}

And then we have a command-line interface.

>>> help
add   Add two numbers
inc   Increment a number
help  Show this help

>>> add 3 4
7

>>> inc 21
22

>>> add 3 4 5
Error: expected 2 arguments; got 3

>>> add 3 four
Error: invalid argument type at position 1; expected type i

Command Interface was created for adding a way to query state in microservices, like getting the list of outstanding orders in a trading system.

This library requires Boost's lexical cast and preprocessor macros.

You might also like...
pbr2gltf2 is a command line tool for converting PBR images to a glTF 2.0 material.
pbr2gltf2 is a command line tool for converting PBR images to a glTF 2.0 material.

pbr2gltf2 is a command line tool for converting PBR images to a glTF 2.0 material. The tool is detecting depending on the filename, which PBR information is stored. It swizzles the images and does reassign the channels to a glTF 2.0 image. The tool stores the images plus a minimal, valid glTF 2.0 file containing the required material, textures and images.

cmdlime - is a C++17 header-only library for command line parsing with minimum of code and pain things to remember A command-line tool to display colorful distro information.
A command-line tool to display colorful distro information.

sjfetch A command-line tool to display colorful distro information.

LwSHELL is lightweight, platform independent, command line shell for embedded systems.

LwSHELL is lightweight, platform independent, command line shell for embedded systems. It targets communication with embedded systems from remote terminal to quickly send commands and the retrieve data from the device.

Toybox: all-in-one Linux command line.

Toybox: all-in-one Linux command line.

Simple command line tool that processes image files using the FidelityFX Super Resolution (FSR) or Contrast Adaptive Sharpening (CAS) shader systems.

Simple command line tool that processes image files using the FidelityFX Super Resolution (FSR) or Contrast Adaptive Sharpening (CAS) shader systems.

Command-line flag parsing in C

flag.h Inspired by Go's flag module: https://pkg.go.dev/flag WARNING! The design of the library is not finished and may be a subject to change. Quick

A command line tool with no external dependencies to print information about an X server instance.

xinfo A command line tool with no external dependencies to print information about an X server instance. Building and running To build the code in thi

easy to use, powerful & expressive command line argument parsing for modern C++ / single header / usage & doc generation

clipp - command line interfaces for modern C++ Easy to use, powerful and expressive command line argument handling for C++11/14/17 contained in a sing

Comments
  • compile failed

    compile failed

    the failure message is like below

    command_interface.hpp:153:35: error: 'x6' was not declared in this scope
           return err_type_args(typeid(x##n).name(), n); \
                                       ^
    /opt/include/boost/preprocessor/repetition/limits/repeat_256.hpp:282:62: note: in expansion of macro 'CAST'
     # define BOOST_PP_REPEAT_2_7(m, d) BOOST_PP_REPEAT_2_6(m, d) m(3, 6,
    
    opened by sequoiar 5
  • Project cannot compile sussessfully in VS2019

    Project cannot compile sussessfully in VS2019

    It's a nice project of the command line argument parse! I meet one problem while compling the demo.

    fatal error C1083: 无法打开包括文件: “boost/lexical_cast.hpp”: No such file or directory

    It seems need some lib about "boost". I didn't find the specification about the "boost" lib at the first time, it is hidding in the last line. I recommand highlight the wiki about the dependency of other libraries which can make the user know what to install obviously.

    opened by Sudouble 1
Owner
Empirical Software Solutions, LLC
Empirical Software Solutions, LLC
CLI11 is a command line parser for C++11 and beyond that provides a rich feature set with a simple and intuitive interface.

CLI11: Command line parser for C++11 What's new • Documentation • API Reference CLI11 is a command line parser for C++11 and beyond that provides a ri

null 2.4k Dec 30, 2022
CLIp is a clipboard emulator for a command line interface written in 100% standard C only. Pipe to it to copy, pipe from it to paste.

CLIp v2 About CLIp is a powerful yet easy to use and minimal clipboard manager for a command line environment, with no dependencies or bloat. Usage Sy

A.P. Jo. 12 Sep 18, 2021
A Command-Line-Interface Debugger for 64-bit Windows written in C.

Debugger-For-Windows A command-line-interface debugger for 64-bit Windows. [email protected]:/mnt/c/Projects/C/Debugger$ ./Debugger.exe ./Tests/test.ex

Tomer Gibor 1 Nov 3, 2021
Windows command line program for Spleeter, written in pure C, no need of Python.

SpleeterMsvcExe is a Windows command line program for Spleeter, which can be used directly. It is written in pure C language, using ffmpeg to read and write audio files, and using Tensorflow C API to make use of Spleeter models. No need to install Python environment, and it does not contain anything related to Python.

Wudi 181 Dec 5, 2022
Shpp - Call c++ functions from a shell with any arguments of any types parsed automatically

shpp Call c++ functions from a shell with any arguments of any types parsed automatically Declare a variable or define a function and register it in s

Pedro Moreira 96 Jun 8, 2022
A simple to use, composable, command line parser for C++ 11 and beyond

Clara v1.1.5 !! This repository is unmaintained. Go here for a fork that is somewhat maintained. !! A simple to use, composable, command line parser f

Catch Org 648 Dec 27, 2022
A library for interactive command line interfaces in modern C++

cli A cross-platform header only C++14 library for interactive command line interfaces (Cisco style) Features Header only Cross-platform (linux and wi

Daniele Pallastrelli 888 Dec 31, 2022
Lightweight C++ command line option parser

Release versions Note that master is generally a work in progress, and you probably want to use a tagged release version. Version 3 breaking changes I

null 3.3k Dec 30, 2022
A simple to use, composable, command line parser for C++ 11 and beyond

Lyra A simple to use, composing, header only, command line arguments parser for C++ 11 and beyond. Obtain License Standards Stats Tests License Distri

Build Frameworks Group 388 Dec 22, 2022
A single header C++ library for parsing command line arguments and options with minimal amount of code

Quick Arg Parser Tired of unwieldy tools like getopt or argp? Quick Arg Parser is a single header C++ library for parsing command line arguments

null 47 Dec 21, 2022