Add sort option

This commit is contained in:
Bob Polis 2024-07-20 14:30:14 +02:00
parent 1e7ace00c4
commit 7689984870

View File

@ -4,12 +4,13 @@
#include <cstdlib>
#include <string>
#include <stdexcept>
#include <sys/types.h>
#include <vector>
#include <map>
#include <algorithm>
// POSIX
#include <getopt.h>
#include <sys/types.h>
#include <sys/xattr.h>
//libraries
@ -25,6 +26,7 @@ std::string change;
std::map<std::string, std::string> changes;
std::vector<std::string> deletions;
bool should_list = false;
bool sort = false;
static void handle_additions(std::vector<std::string>& tags) {
for (const std::string& tag : additions) {
@ -69,6 +71,9 @@ static void process(const char* path) {
handle_deletions(tags);
handle_changes(tags);
handle_additions(tags);
if (sort) {
std::sort(tags.begin(), tags.end());
}
if (should_list) {
std::cout << " after: " << sc::io::yellowf << sc::join(tags, ", ") << sc::io::reset << '\n';
}
@ -106,9 +111,10 @@ int main(int argc, char* argv[]) {
{"change", required_argument, nullptr, 'c'},
{"into", required_argument, nullptr, 'i'},
{"list", no_argument, nullptr, 'l'},
{"sort", no_argument, nullptr, 's'},
{nullptr, 0, nullptr, 0}
};
while ((opt_char = getopt_long(argc, argv, "a:c:d:hi:l", long_options, nullptr)) != -1) {
while ((opt_char = getopt_long(argc, argv, "a:c:d:hi:ls", long_options, nullptr)) != -1) {
std::string arg {optarg ? optarg : ""};
switch (opt_char) {
case 0: {
@ -138,6 +144,9 @@ int main(int argc, char* argv[]) {
case 'l':
should_list = true;
break;
case 's':
sort = true;
break;
case '?':
throw std::runtime_error("unrecognized option");
}