Files
gitlabvers/src/main.cpp
Bob Polis c47e03c87c Reverse order of lines when using -a option
Now the most recent come at the end, which is more convenient when
output is done to a terminal.
2025-05-15 21:38:51 +02:00

82 lines
2.7 KiB
C++

#include <iostream>
#include <cstdlib>
#include <string>
#include <stdexcept>
#include <regex>
#include <sstream>
#include <vector>
#include <getopt.h>
#include <libscrequest.hpp>
#include "version.hpp"
void print_help() {
std::cout << "usage: gitlabvers [-h|--version]\n";
std::cout << " -h, --help show this help text and exit\n";
std::cout << " --version show version number and exit\n";
}
int main(int argc, char* argv[]) {
try {
bool all = false;
int opt_char, opt_val;
struct option long_options[] = {
{"help", no_argument, nullptr, 'h'},
{"all", no_argument, nullptr, 'a'},
{"version", no_argument, &opt_val, 1},
{nullptr, 0, nullptr, 0}
};
while ((opt_char = getopt_long(argc, argv, "ah", long_options, nullptr)) != -1) {
std::string arg {optarg ? optarg : ""};
switch (opt_char) {
case 0: {
// handle long-only options here
switch (opt_val) {
case 1:
std::cout << gitlabvers_version() << std::endl;
return EXIT_SUCCESS;
}
break;
}
case 'a':
all = true;
break;
case 'h':
print_help();
return EXIT_SUCCESS;
case '?':
throw std::runtime_error("unrecognized option");
}
}
if (optind == argc) {
// here when no file args
}
for (int i = optind; i < argc; ++i) {
try {
// process file argv[i]
} catch (const std::runtime_error& ex) {
std::cerr << "gitlabvers: " << ex.what() << '\n';
}
}
sc::requester req;
std::string text {req.get("https://gitlab.com/gitlab-org/gitlab-foss/-/raw/master/CHANGELOG.md")};
std::regex pat {R"(^##\s+(\d+\.\d+\.\d+)\s+\((\d+-\d+-\d+))", std::regex::multiline};
std::sregex_iterator beg {text.begin(), text.end(), pat};
std::sregex_iterator end {};
std::vector<std::string> lines;
for (std::sregex_iterator i = beg; i != end; ++i) {
std::smatch match {*i};
std::ostringstream oss;
oss << match[1] << " [" << match[2] << "]\n";
lines.insert(lines.begin(), oss.str());
if (!all) break;
}
for (const std::string& line : lines) {
std::cout << line;
}
} catch (const std::exception& ex) {
std::cerr << "gitlabvers: " << ex.what() << '\n';
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}