tg_cli: use OptionParser.

GitOrigin-RevId: c2d73fcc79028d1dce9b951ad02ae880c114c1e0
This commit is contained in:
levlam 2020-06-17 05:30:14 +03:00
parent c1688ca081
commit a7fa10718f

View File

@ -23,6 +23,7 @@
#include "td/utils/JsonBuilder.h" #include "td/utils/JsonBuilder.h"
#include "td/utils/logging.h" #include "td/utils/logging.h"
#include "td/utils/misc.h" #include "td/utils/misc.h"
#include "td/utils/OptionParser.h"
#include "td/utils/port/FileFd.h" #include "td/utils/port/FileFd.h"
#include "td/utils/port/PollFlags.h" #include "td/utils/port/PollFlags.h"
#include "td/utils/port/signals.h" #include "td/utils/port/signals.h"
@ -4285,10 +4286,6 @@ static void fail_signal(int sig) {
} }
} }
static void usage() {
//TODO:
}
static void on_fatal_error(const char *error) { static void on_fatal_error(const char *error) {
std::cerr << "Fatal error: " << error << std::endl; std::cerr << "Fatal error: " << error << std::endl;
} }
@ -4331,52 +4328,72 @@ void main(int argc, char **argv) {
} }
return std::string(); return std::string();
}(std::getenv("TD_API_HASH")); }(std::getenv("TD_API_HASH"));
// TODO use OptionParser
for (int i = 1; i < argc; i++) { td::OptionParser options;
if (!std::strcmp(argv[i], "--test")) { options.set_description("TDLib test client");
use_test_dc = true; options.add_option('\0', "test", "Use test DC", [&] {
} else if (!std::strncmp(argv[i], "-v", 2)) { use_test_dc = true;
const char *arg = argv[i] + 2; return Status::OK();
if (*arg == '\0' && i + 1 < argc) { });
arg = argv[++i]; options.add_option('v', "verbosity", "Verbosity level", [&](Slice level) {
} int new_verbosity = 1;
int new_verbosity = 1; while (begins_with(level, "v")) {
while (*arg == 'v') { new_verbosity++;
new_verbosity++; level.remove_prefix(1);
arg++;
}
if (*arg) {
new_verbosity += to_integer<int>(Slice(arg)) - (new_verbosity == 1);
}
new_verbosity_level = VERBOSITY_NAME(FATAL) + new_verbosity;
} else if (!std::strncmp(argv[i], "-l", 2)) {
const char *arg = argv[i] + 2;
if (*arg == '\0' && i + 1 < argc) {
arg = argv[++i];
}
if (file_log.init(arg).is_ok() && file_log.init(arg).is_ok() && file_log.init(arg, 1000 << 20).is_ok()) {
log_interface = &ts_log;
}
} else if (!std::strcmp(argv[i], "-W")) {
get_chat_list = true;
} else if (!std::strcmp(argv[i], "--disable-network") || !std::strcmp(argv[i], "-n")) {
disable_network = true;
} else if (!std::strcmp(argv[i], "--api_id") || !std::strcmp(argv[i], "--api-id")) {
if (i + 1 >= argc) {
return usage();
}
api_id = to_integer<int32>(Slice(argv[++i]));
} else if (!std::strcmp(argv[i], "--api_hash") || !std::strcmp(argv[i], "--api-hash")) {
if (i + 1 >= argc) {
return usage();
}
api_hash = argv[++i];
} }
if (!level.empty()) {
new_verbosity += to_integer<int>(level) - (new_verbosity == 1);
}
new_verbosity_level = VERBOSITY_NAME(FATAL) + new_verbosity;
return Status::OK();
});
options.add_option('l', "log", "Log file", [&](Slice file_name) {
if (file_log.init(file_name.str()).is_ok() && file_log.init(file_name.str()).is_ok() &&
file_log.init(file_name.str(), 1000 << 20).is_ok()) {
log_interface = &ts_log;
}
return Status::OK();
});
options.add_option('W', "", "Preload chat list", [&] {
get_chat_list = true;
return Status::OK();
});
options.add_option('n', "disable-network", "Disable network", [&] {
disable_network = true;
return Status::OK();
});
options.add_option('\0', "api-id", "Telegram API ID", [&](Slice parameter) {
api_id = to_integer<int32>(parameter);
return Status::OK();
});
options.add_option('\0', "api_id", "Telegram API ID", [&](Slice parameter) {
api_id = to_integer<int32>(parameter);
return Status::OK();
});
options.add_option('\0', "api-hash", "Telegram API hash", [&](Slice parameter) {
api_hash = parameter.str();
return Status::OK();
});
options.add_option('\0', "api_hash", "Telegram API hash", [&](Slice parameter) {
api_hash = parameter.str();
return Status::OK();
});
auto res = options.run(argc, argv);
if (res.is_error()) {
LOG(ERROR) << res.error();
LOG(PLAIN) << options;
return;
}
if (!res.ok().empty()) {
LOG(ERROR) << "Have unexpected non-option parameters";
LOG(PLAIN) << options;
return;
} }
if (api_id == 0 || api_hash.empty()) { if (api_id == 0 || api_hash.empty()) {
LOG(ERROR) << "You should provide some valid api_id and api_hash"; LOG(ERROR) << "You should provide some valid api_id and api_hash";
return usage(); LOG(PLAIN) << options;
return;
} }
SET_VERBOSITY_LEVEL(new_verbosity_level); SET_VERBOSITY_LEVEL(new_verbosity_level);