Remove unused class CommandLineInterpreter.

This commit is contained in:
luocai 2023-12-21 17:40:32 +08:00
parent 60cf8a3cae
commit 43dab30597
3 changed files with 0 additions and 125 deletions

View File

@ -3,7 +3,6 @@ find_package(Boost REQUIRED COMPONENTS log log_setup program_options)
add_library(Universal
BoostLog.h BoostLog.inl BoostLog.cpp
BufferUtility.h BufferUtility.cpp
CommandLineInterpreter.h CommandLineInterpreter.cpp
DateTime.h DateTime.cpp
FunctionTraits.h
IoContext.h IoContext.cpp

View File

@ -1,101 +0,0 @@
#include "CommandLineInterpreter.h"
#include "BoostLog.h"
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/variables_map.hpp>
#include <iostream>
CommandLineInterpreter::CommandLineInterpreter(const DescriptionPointer &description, const std::string &prompt)
: m_description(description), m_prompt(prompt) {}
void CommandLineInterpreter::interpret(std::istream &inputStream) {
std::string command;
std::cout << m_prompt << std::flush;
while (std::getline(inputStream, command, '\n')) {
handleReadLine(command);
std::cout << m_prompt << std::flush;
}
}
void CommandLineInterpreter::handleReadLine(std::string line) {
using namespace boost::program_options;
if (m_description.expired()) {
LOG(error) << "description has expired.";
return;
}
auto description = m_description.lock();
std::vector<std::string> args;
// huu, ugly...
args = splitCommandLine(std::string("--") + line);
try {
variables_map vm;
store(command_line_parser(args).options(*description).run(), vm);
notify(vm);
} catch (boost::program_options::unknown_option &e) {
std::cerr << "error: " << e.what() << std::endl;
} catch (boost::program_options::invalid_command_line_syntax &e) {
std::cerr << "error: " << e.what() << std::endl;
} catch (boost::program_options::validation_error &e) {
std::cerr << "error: " << e.what() << std::endl;
}
}
std::vector<std::string> CommandLineInterpreter::splitCommandLine(const std::string &input) {
std::vector<std::string> result;
std::string::const_iterator i = input.begin(), e = input.end();
for (; i != e; ++i)
if (!isspace((unsigned char)*i)) break;
if (i != e) {
std::string current;
bool inside_quoted = false;
int backslash_count = 0;
for (; i != e; ++i) {
if (*i == '"') {
// '"' preceded by even number (n) of backslashes generates
// n/2 backslashes and is a quoted block delimiter
if (backslash_count % 2 == 0) {
current.append(backslash_count / 2, '\\');
inside_quoted = !inside_quoted;
// '"' preceded by odd number (n) of backslashes generates
// (n-1)/2 backslashes and is literal quote.
} else {
current.append(backslash_count / 2, '\\');
current += '"';
}
backslash_count = 0;
} else if (*i == '\\') {
++backslash_count;
} else {
// Not quote or backslash. All accumulated backslashes should be
// added
if (backslash_count) {
current.append(backslash_count, '\\');
backslash_count = 0;
}
if (isspace((unsigned char)*i) && !inside_quoted) {
// Space outside quoted section terminate the current argument
result.push_back(current);
current.resize(0);
for (; i != e && isspace((unsigned char)*i); ++i)
;
--i;
} else {
current += *i;
}
}
}
// If we have trailing backslashes, add them
if (backslash_count) current.append(backslash_count, '\\');
// If we have non-empty 'current' or we're still in quoted
// section (even if 'current' is empty), add the last token.
if (!current.empty() || inside_quoted) result.push_back(current);
}
return result;
}

View File

@ -1,23 +0,0 @@
#ifndef COMMANDLINEINTERPRETER_H
#define COMMANDLINEINTERPRETER_H
#include <boost/program_options/options_description.hpp>
#include <memory>
class CommandLineInterpreter {
public:
using DescriptionPointer = std::shared_ptr<boost::program_options::options_description>;
using DescriptionWeakPointer = std::weak_ptr<boost::program_options::options_description>;
CommandLineInterpreter(const DescriptionPointer &description, const std::string &prompt);
void interpret(std::istream &inputStream);
protected:
void handleReadLine(std::string line);
static std::vector<std::string> splitCommandLine(const std::string &input);
private:
const DescriptionWeakPointer m_description;
std::string m_prompt;
};
#endif // COMMANDLINEINTERPRETER_H