-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexamples_util.hpp
42 lines (34 loc) · 1.51 KB
/
examples_util.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#pragma once
#include <chrono>
#include <iostream>
#include <string>
#include <thread>
#include <tuple>
#include <utility>
inline std::tuple<std::string, std::string, std::string, std::string> parse_input_or_fail(int argc, const char** argv) {
auto token = argc > 1 ? argv[1] : std::getenv("BANANA_BOT_TOKEN");
auto target = argc > 2 ? argv[2] : std::getenv("BANANA_EXAMPLE_TARGET");
if (token == nullptr && target == nullptr) {
std::cout << "usage: " << (argc > 0 ? argv[0] : "./self") << " token target [name] [context]\n";
std::exit(2);
}
auto extract_file_name = [](std::string_view path) {
if (path.ends_with(".exe")) path.remove_suffix(4);
const auto slash_pos = path.find_last_of("/\\");
if (slash_pos != std::string_view::npos) path = path.substr(slash_pos + 1);
return path;
};
const auto example_name = std::string(argc > 3 ? argv[3] : argc > 0 ? extract_file_name(argv[0]) : "<unknown>");
const auto context = argc > 4 ? argv[4] : std::getenv("BANANA_EXAMPLE_CONTEXT");
auto message_text = "Hello from " + example_name;
if (context) {
message_text += " at ";
message_text += context;
}
message_text += "!";
// Hack to make sure each example has delay before calling telegram API
if (const auto delay = std::getenv("BANANA_EXAMPLE_DELAY")) {
std::this_thread::sleep_for(std::chrono::milliseconds(std::stol(delay)));
}
return { token, target, std::move(example_name), std::move(message_text) };
}