#include "args.hpp"
#include "common/message_parser.hpp"
#include <getopt.h>
#include <algorithm>
#include <iostream>
bool parseArgs(int argc, char* argv[], Arguments& args)
{
static const option longopts[] = {
{ "help", no_argument, NULL, 'h' },
{ "local", required_argument, NULL, 'l' },
{ "remote", required_argument, NULL, 'r' },
{ "msg", required_argument, NULL, 'm' },
{"count", required_argument, NULL, 'c'},
};
int opt = -1;
while ((opt = getopt_long_only(argc, argv, "", longopts, NULL)) != -1)
{
switch (opt)
{
case 'l':
args.localAddr = optarg;
break;
case 'r':
args.remoteAddr = optarg;
break;
case 'm':
{
int errorPos = 0;
std::tie(args.message, errorPos) = parseString(optarg);
if (errorPos >= 0) {
std::cout << "Error parsing message at char " << errorPos << '\n';
return false;
}
break;
}
case 'c':
{
args.count = std::stoi(optarg);
break;
}
case 'h':
default:
std::cout
<< "Usage: echo-async -local LOCAL -remote REMOTE -msg MESSAGE\n"
<< " LOCAL Local IP address and port (required for servers)\n"
<< " REMOTE Scion address of the remote server (only for clients)\n"
<< " MESSAGE The message clients will send to the server\n"
<< " COUNT (for clients) number of times clients will repeat the message\n";
return false;
}
}
if (args.localAddr.empty() && args.remoteAddr.empty()) {
std::cout << "At least one of local (for servers) and remote (for clients) is required\n";
return false;
}
return true;
}