1 // gen-shell - the generic REPL
2 // Copyright (c) 2021, Armaan Bhojwani <me@armaanb.net>
10 #include <readline/readline.h>
11 #include <readline/history.h>
13 ////////////////////////////////////////////////////////////////////////////////
18 getResponse(const std::string & prompt) {
19 std::string response {
23 // Display prompt, get input
24 char * line_read = readline(prompt.c_str());
30 add_history(line_read);
33 response = std::string(line_read);
40 ////////////////////////////////////////////////////////////////////////////////
43 main(int argc, char** argv)
45 // Command line arguments
48 sarge.setArgument("c", "command", "Command to convert to REPL", true);
49 sarge.setArgument("h", "help", "Show this message.", false);
50 sarge.setArgument("p", "prompt", "Define a custom prompt", true);
51 sarge.setArgument("q", "quotes", "Treat whole input as argv[1]", false);
52 sarge.setDescription("Make a REPL from any executable");
53 sarge.setUsage("gen-shell <options>");
55 if (!sarge.parseArguments(argc, argv)) {
56 std::cerr << "Could not parse command line arguments" << std::endl;
60 if (sarge.exists("help")) {
66 sarge.getFlag("command", arg_cmd);
69 sarge.getFlag("prompt", prompt);
76 auto command = getResponse(prompt);
78 if (command == "<EOF>" || command == "exit" || command == "quit" ) {
81 string whole_command = arg_cmd + " ";
82 if (sarge.exists("quotes")) {
83 whole_command = whole_command + "\"" + command + "\"";
85 whole_command += command;
87 system(whole_command.c_str());
92 ////////////////////////////////////////////////////////////////////////////////