74 lines
2.2 KiB
C++
74 lines
2.2 KiB
C++
|
#include <iostream>
|
||
|
#include <cstdlib>
|
||
|
#include <cstring>
|
||
|
#include <string>
|
||
|
#include <stdexcept>
|
||
|
#include <fstream>
|
||
|
#include <getopt.h>
|
||
|
#include "version.hpp"
|
||
|
|
||
|
void print_help() {
|
||
|
std::cout << "usage: {PROJECT} [-h]\n";
|
||
|
std::cout << " -h, --help show this help text and exit\n";
|
||
|
std::cout << " --version show version number and exit\n";
|
||
|
}
|
||
|
|
||
|
std::string filter(const std::string& text) {
|
||
|
std::string result;
|
||
|
// your filter code here
|
||
|
result = text; // null-filter
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
int main(int argc, char * argv[]) {
|
||
|
try {
|
||
|
int opt_char, opt_val;
|
||
|
struct option long_options[] = {
|
||
|
{"help", no_argument, nullptr, 'h'},
|
||
|
{"version", no_argument, &opt_val, 1},
|
||
|
{nullptr, 0, nullptr, 0}
|
||
|
};
|
||
|
while ((opt_char = getopt_long(argc, argv, "h", long_options, nullptr)) != -1) {
|
||
|
switch (opt_char) {
|
||
|
case 0: {
|
||
|
// handle long-only options here
|
||
|
switch (opt_val) {
|
||
|
case 1:
|
||
|
std::cout << {PROJECT}_version() << std::endl;
|
||
|
return EXIT_SUCCESS;
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
case 'h':
|
||
|
print_help();
|
||
|
return EXIT_SUCCESS;
|
||
|
case '?':
|
||
|
throw std::runtime_error("unrecognized option");
|
||
|
}
|
||
|
}
|
||
|
if (optind == argc) {
|
||
|
// no file args => read lines from stdin
|
||
|
std::string line;
|
||
|
while (getline(std::cin, line)) {
|
||
|
std::cout << filter(line) << '\n';
|
||
|
}
|
||
|
}
|
||
|
for (int i = optind; i < argc; ++i) {
|
||
|
try {
|
||
|
// process file argv[i]
|
||
|
std::ifstream file {argv[i]};
|
||
|
std::string line;
|
||
|
while (getline(file, line)) {
|
||
|
std::cout << filter(line) << '\n';
|
||
|
}
|
||
|
} catch (const std::runtime_error& ex) {
|
||
|
std::cerr << "{PROJECT}: " << ex.what() << '\n';
|
||
|
}
|
||
|
}
|
||
|
} catch (const std::exception& ex) {
|
||
|
std::cerr << "{PROJECT}: " << ex.what() << '\n';
|
||
|
return EXIT_FAILURE;
|
||
|
}
|
||
|
return EXIT_SUCCESS;
|
||
|
}
|