Add a command interpreter (eg., REPL) to any C++ program

Overview

Command Interpreter

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

#include "command_interpreter.hpp"

class Arithmetic : public CommandInterpreter {
  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 interpreter.

#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 interpreter.

>>> 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 Interpreter 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.

Issues
  • 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
Read-Compile-Run-Loop: tiny and powerful interactive C++ compiler (REPL)

Read-Compile-Run-Loop: tiny and powerful interactive C++ compiler (REPL) RCRL is a tiny engine for interactive C++ compilation and execution (implemen

Viktor Kirilov 370 Jun 20, 2022
Any one can add their programs here ,but program should not be repeated.

Hactoberfest2021 To All the Beginners Guys Don't Forget to give star to this repo. Any one can add their programs here ,but program should not be repe

Varun.py 22 Jun 12, 2022
Freeze OS is a cross-platform operating system emulator that runs on top of an interpreter called the Freeze interpreter.

Freeze OS is a cross-platform operating system emulator that runs on top of an interpreter called the Freeze interpreter. The operating system code is basically written in the Freeze programming language that is passed to the Freeze interpreter. The idea is to skip instances where the operating system needs to handle low level operators and focus on higher level stuff, like malware analysis, AI, and others.

null 24 May 2, 2022
Inject a DLL into any program using this C++ program

DLL-Injection-Cpp Inject a DLL into any process using this C++ program Installation Go into a folder and open up Command Prompt. In command prompt run

n0 4 Apr 25, 2022
Add virtual monitors to your windows 10 device! Works with Oculus software, obs, and any desktop sharing software

License MIT and CC0 or Public Domain, whichever is least restrictive -- Use it AS IS - NO IMPLICIT OR EXPLICIT warranty This may break your computer,

Rashi Abramson 162 Jun 21, 2022
A beginner friendly repo in the world of open source. Contribute here to add here project in any languages.

Hacktober Fest 2021 Heyy There (●'◡'●) Here you can contribute to opensource project in any valid language and project. Just follow the contribution g

Anonymous-inception 6 May 24, 2022
Add anything about data structure and algorithm in any language.

Hacktoberfest 2021 Follow the README below to get started! Note : This repo is excluded from the Hacktoberfest but you can still contribute and the re

null 29 Jun 15, 2022
Add a code in any language

Hii I AM SHAMIL THOMAS & I WELCOME YOU TO HacktoberFest 2021 Give a star to the repository if you like it ⭐ Add a code in any language ❓ What's Hackto

null 25 May 28, 2022
Trident provides an easy way to pass the output of one command to any number of targets.

Trident: The multiple-pipe system Trident provides an easy way to pipe the output of one command to not just one but many targets. These targets can b

Matthias Gessinger 36 Nov 23, 2021
Loads a signed kernel driver which allows you to map any driver to kernel mode without any traces of the signed / mapped driver.

CosMapper Loads a signed kernel driver (signed with leaked cert) which allows you to map any driver to kernel mode without any traces of the signed /

null 103 Jun 20, 2022
Automatically load dlls into any executables without replacing any files!

Automatically loaded dll using xinput9_1_0 proxy. Please put the modified xinput9_1_0.dll in the executable's directory.

null 11 Apr 17, 2022
Create a calculator of any kind in any language, create a pr.

calculators Create a calculator of any kind in any language, create a pr. Create a calculator of any type using the programming language of your choic

Akshay Gautam 2 Dec 1, 2021
Simple command-line program for sharing the display image on a local network.

XCast is a simple server/client command line program for sharing screen under X. With XCast you have the ability to either pull the display from a rem

Vito Čuček 3 Dec 12, 2021
Bypass UAC at any level by abusing the Program Compatibility Assistant with RPC, WDI, and more Windows components

ByeIntegrity 8.0 The eighth Windows privilege escalation attack in the ByeIntegrity family. ByeIntegrity 8.0 is the most complex one I've created so f

Arush Agarampur 212 Jun 10, 2022
Embed read-only filesystems into any C++11 program w. a single header, zero dependencies and zero modifications to your code

c-embed Embed read-only filesystems into any C++11 program w. a single header, zero dependencies and zero modifications to your code. Usage c-embed al

Nick McDonald 5 Jun 24, 2022
lambda calculus interpreter

lambda calculus interpreter what: A small lambda calculus interpreter written in C++. It supports α-conversion and β-reduction, as well as precise tra

zhiayang 23 Jun 15, 2022
INSTEAD interpreter for developers

instead-cli Trivial INSTEAD interpreter for developers. Build and run Dependencies: luajit (or lua), iconv. $ git clone https://github.com/instead-hub

INSTEAD 8 Apr 22, 2022
C64 Watch is a customized T-Watch 2020 that was inspired by the Commodore 64 computer. It features a C64 theme and a built-in BASIC interpreter.

C64 Watch C64 Watch is a customized T-Watch 2020 that was inspired by the Commodore 64 computer. It features a C64 theme and a built-in BASIC interpre

Nick Bild 24 Apr 7, 2022