erius
8f18f9ef8f
Moved ftsort sources to bin/ftsort Moved tape_config.h into bin/ftsort Implemented command line parsing for ftsort in tape_config.cpp ftsort binary is in working state
89 lines
2.6 KiB
C++
89 lines
2.6 KiB
C++
#include "tape_config.h"
|
|
#include <bits/getopt_core.h>
|
|
#include <getopt.h>
|
|
|
|
const static struct std::vector<option> CMD_OPTS{
|
|
{.name = "write-delay",
|
|
.has_arg = no_argument,
|
|
.flag = nullptr,
|
|
.val = 'w'},
|
|
{.name = "read-delay", .has_arg = no_argument, .flag = nullptr, .val = 'r'},
|
|
{.name = "seek-forward-delay",
|
|
.has_arg = no_argument,
|
|
.flag = nullptr,
|
|
.val = 'f'},
|
|
{.name = "seek-backwards-delay",
|
|
.has_arg = no_argument,
|
|
.flag = nullptr,
|
|
.val = 'b'},
|
|
{.name = "memory-limit",
|
|
.has_arg = no_argument,
|
|
.flag = nullptr,
|
|
.val = 'm'},
|
|
{.name = "config-file",
|
|
.has_arg = no_argument,
|
|
.flag = nullptr,
|
|
.val = 'c'},
|
|
{.name = "version", .has_arg = no_argument, .flag = nullptr, .val = 'v'},
|
|
{.name = "help", .has_arg = no_argument, .flag = nullptr, .val = 'h'},
|
|
{.name = nullptr, .has_arg = no_argument, .flag = nullptr, .val = 0}};
|
|
|
|
std::chrono::milliseconds parse_delay(const std::string &input) {
|
|
int delay_ms = std::stoi(input);
|
|
return std::chrono::milliseconds(delay_ms);
|
|
}
|
|
|
|
CmdArgs parse_command_line(int argc, char **argv) {
|
|
CmdArgs cmd{
|
|
.config_file_path = "",
|
|
.input_file_name = "",
|
|
.output_file_name = "",
|
|
.memory_limit = SIZE_MAX,
|
|
.version = false,
|
|
.help = false,
|
|
.settings = tape::FT_DEFAULT_SETTINGS,
|
|
};
|
|
int opt_index = 0;
|
|
int opt = 0;
|
|
// parse [OPTIONS...] using getopt_long
|
|
while ((opt = getopt_long(argc, argv, "r:w:f:b:m:c:vh", CMD_OPTS.data(),
|
|
&opt_index)) != -1) {
|
|
switch (opt) {
|
|
case 'r':
|
|
cmd.settings.read_delay = parse_delay(optarg);
|
|
break;
|
|
case 'w':
|
|
cmd.settings.write_delay = parse_delay(optarg);
|
|
break;
|
|
case 'f':
|
|
cmd.settings.seek_forward_delay = parse_delay(optarg);
|
|
break;
|
|
case 'b':
|
|
cmd.settings.seek_backwards_delay = parse_delay(optarg);
|
|
break;
|
|
case 'm':
|
|
cmd.memory_limit = std::stoi(optarg);
|
|
break;
|
|
case 'c':
|
|
cmd.config_file_path = optarg;
|
|
break;
|
|
case 'v':
|
|
cmd.version = true;
|
|
break;
|
|
case 'h':
|
|
case '?':
|
|
default:
|
|
cmd.help = true;
|
|
break;
|
|
}
|
|
}
|
|
// parse positional args INPUT_FILE OUTPUT_FILE
|
|
if (argc - optind < 2) {
|
|
cmd.help = true;
|
|
return cmd;
|
|
}
|
|
// no linting to disable pointer arithmetic warning
|
|
cmd.input_file_name = argv[optind]; // NOLINT
|
|
cmd.output_file_name = argv[optind + 1]; // NOLINT
|
|
return cmd;
|
|
}
|