Implemented a prototype for selecting time format

This commit is contained in:
theskywinds 2025-06-23 12:52:33 +02:00
parent 362915a2f3
commit 008d044f73

View File

@ -6,7 +6,7 @@ public:
MyFormatter() : Formatter() {}
std::string make_usage([[maybe_unused]] const CLI::App *app, [[maybe_unused]] std::string name) const override {
return "USAGE: " + name + " [OPTIONS]";
};
}
};
int main(int argc, char **argv) {
@ -14,15 +14,32 @@ int main(int argc, char **argv) {
auto fmt = std::make_shared<MyFormatter>();
app.formatter(fmt);
int p = 0;
app.add_option("-p", p, "Parameter");
char timeFormat {};
app.add_option("-f, --format", timeFormat, "Dictates which format to output as")
->multi_option_policy (CLI::MultiOptionPolicy::Throw)
->default_val('f');
CLI11_PARSE(app, argc, argv);
[[maybe_unused]] std::time_t result = std::time(nullptr);
std::tm time = *std::localtime(&result);
std::cout << std::asctime(&time);
try {
if (timeFormat != 'F' && timeFormat != 'f' &&
timeFormat != 'D' && timeFormat != 'd' &&
timeFormat != 'T' && timeFormat != 't' &&
timeFormat != 'R') {
throw std::invalid_argument("Invalid format selected");
}
} catch (const std::invalid_argument& e) {
std::cerr << "Error: " << e.what() << std::endl;
std::exit(EXIT_FAILURE);
}
std::cout << "Parameter value: " << p << std::endl;
std::time_t result = std::time(nullptr);
std::tm currentTime = *std::localtime(&result);
std::tm resultTime = currentTime;
std::cout << std::asctime(&resultTime);
std::cout << "Parameter value: " << timeFormat << std::endl;
return 0;
}