Implemented a prototype for selecting time format
This commit is contained in:
parent
362915a2f3
commit
008d044f73
31
src/main.cpp
31
src/main.cpp
|
|
@ -6,7 +6,7 @@ public:
|
||||||
MyFormatter() : Formatter() {}
|
MyFormatter() : Formatter() {}
|
||||||
std::string make_usage([[maybe_unused]] const CLI::App *app, [[maybe_unused]] std::string name) const override {
|
std::string make_usage([[maybe_unused]] const CLI::App *app, [[maybe_unused]] std::string name) const override {
|
||||||
return "USAGE: " + name + " [OPTIONS]";
|
return "USAGE: " + name + " [OPTIONS]";
|
||||||
};
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
|
|
@ -14,15 +14,32 @@ int main(int argc, char **argv) {
|
||||||
auto fmt = std::make_shared<MyFormatter>();
|
auto fmt = std::make_shared<MyFormatter>();
|
||||||
app.formatter(fmt);
|
app.formatter(fmt);
|
||||||
|
|
||||||
int p = 0;
|
char timeFormat {};
|
||||||
app.add_option("-p", p, "Parameter");
|
|
||||||
|
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);
|
CLI11_PARSE(app, argc, argv);
|
||||||
|
|
||||||
[[maybe_unused]] std::time_t result = std::time(nullptr);
|
try {
|
||||||
std::tm time = *std::localtime(&result);
|
if (timeFormat != 'F' && timeFormat != 'f' &&
|
||||||
std::cout << std::asctime(&time);
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user