70 lines
2.4 KiB
C++
70 lines
2.4 KiB
C++
//cli_parser.cpp
|
|
#include "cli_parser.h"
|
|
#include <getopt.h>
|
|
#include <iostream>
|
|
|
|
void config::parseCommandLine(int argc, char *argv[]) {
|
|
const struct option longOptions[] = {
|
|
{"host", required_argument, nullptr, 'h'},
|
|
{"port", required_argument, nullptr, 'p'},
|
|
{"vhost", required_argument, nullptr, 'v'},
|
|
{"vport", required_argument, nullptr, 'b'},
|
|
{"version", required_argument, nullptr, 'r'},
|
|
{"state", required_argument, nullptr, 's'},
|
|
{"help", no_argument, nullptr, 0},
|
|
{nullptr, 0, nullptr, 0}
|
|
};
|
|
|
|
int opt;
|
|
while ((opt = getopt_long(argc, argv, "h:p:v:b:r:s:", longOptions, nullptr)) != -1) {
|
|
switch (opt) {
|
|
case 'h':
|
|
hostname = optarg;
|
|
break;
|
|
case 'p':
|
|
port = std::stoi(optarg);
|
|
break;
|
|
case 'v':
|
|
virtualHostname = optarg;
|
|
break;
|
|
case 'b':
|
|
virtualPort = std::stoi(optarg);
|
|
break;
|
|
case 'r':
|
|
protocolVersion = std::stoi(optarg);
|
|
break;
|
|
case 's':
|
|
nextState = std::stoi(optarg);
|
|
break;
|
|
case 0: // --help
|
|
printHelp();
|
|
exit(0);
|
|
default: // '?'
|
|
printHelp();
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
if (hostname.empty()) {
|
|
std::cerr << "Error: Hostname is required.\n";
|
|
printHelp();
|
|
exit(1);
|
|
}
|
|
|
|
if (virtualHostname.empty()) {
|
|
virtualHostname = hostname; // Default to real hostname
|
|
}
|
|
}
|
|
|
|
void config::printHelp() {
|
|
std::cout << "Usage: mcpinger [options]\n"
|
|
<< "Options:\n"
|
|
<< " --host <hostname> Real server hostname (required)\n"
|
|
<< " --port <port> Real server port (default 25565)\n"
|
|
<< " --vhost <virtual host> Virtual hostname (default is same as host)\n"
|
|
<< " --vport <virtual port> Virtual port (default is same as port)\n"
|
|
<< " --version <protocol> Protocol version (default -1)\n"
|
|
<< " --state <next state> Next state (default 1, status request)\n"
|
|
<< " --help Show this help message\n";
|
|
}
|