62 lines
1.3 KiB
C++
62 lines
1.3 KiB
C++
#include <getopt.h>
|
|
#include <iostream>
|
|
#include <string>
|
|
|
|
#include "converter.h"
|
|
|
|
struct CliArgs {
|
|
std::string input;
|
|
std::string output;
|
|
};
|
|
|
|
static void print_usage (const char *argv0) {
|
|
std::cerr << "Usage: " << argv0
|
|
<< " --input <file> --output <file>\n";
|
|
}
|
|
|
|
static bool parse_args (int argc, char **argv, CliArgs &out) {
|
|
static option long_opts[] = {
|
|
{"input", required_argument, nullptr, 'i'},
|
|
{"output", required_argument, nullptr, 'o'},
|
|
{"help", no_argument, nullptr, 'h'},
|
|
{nullptr, 0, nullptr, 0 }
|
|
};
|
|
|
|
int c = 0;
|
|
while ((c = ::getopt_long (argc, argv, "i:o:h", long_opts, nullptr)) != -1) {
|
|
switch (c) {
|
|
case 'i':
|
|
out.input = optarg;
|
|
break;
|
|
case 'o':
|
|
out.output = optarg;
|
|
break;
|
|
case 'h':
|
|
print_usage (argv[0]);
|
|
return false;
|
|
default:
|
|
print_usage (argv[0]);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (out.input.empty() || out.output.empty()) {
|
|
print_usage (argv[0]);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
int main (int argc, char **argv) {
|
|
CliArgs args;
|
|
if (!parse_args (argc, argv, args))
|
|
return 1;
|
|
|
|
Converter converter (args.input);
|
|
if (!converter.save (args.output))
|
|
return converter.error_code();
|
|
|
|
return 0;
|
|
}
|