Add OptionParser::add_check.

GitOrigin-RevId: dbf50403ba264ddf04b50197f0b54500c8f761ce
This commit is contained in:
levlam 2020-06-19 05:50:18 +03:00
parent 5aa609535e
commit 251318d2da
3 changed files with 16 additions and 7 deletions

View File

@ -4356,6 +4356,12 @@ void main(int argc, char **argv) {
[&](Slice parameter) { api_id = to_integer<int32>(parameter); }); [&](Slice parameter) { api_id = to_integer<int32>(parameter); });
options.add_option('\0', "api-hash", "Set Telegram API hash", [&](Slice parameter) { api_hash = parameter.str(); }); options.add_option('\0', "api-hash", "Set Telegram API hash", [&](Slice parameter) { api_hash = parameter.str(); });
options.add_option('\0', "api_hash", "Set Telegram API hash", [&](Slice parameter) { api_hash = parameter.str(); }); options.add_option('\0', "api_hash", "Set Telegram API hash", [&](Slice parameter) { api_hash = parameter.str(); });
options.add_check([&] {
if (api_id == 0 || api_hash.empty()) {
return Status::Error("You must provide valid api-id and api-hash obtained at https://my.telegram.org");
}
return Status::OK();
});
auto res = options.run(argc, argv); auto res = options.run(argc, argv);
if (res.is_error()) { if (res.is_error()) {
LOG(PLAIN) << "tg_cli: " << res.error().message(); LOG(PLAIN) << "tg_cli: " << res.error().message();
@ -4369,13 +4375,6 @@ void main(int argc, char **argv) {
return; return;
} }
if (api_id == 0 || api_hash.empty()) {
LOG(PLAIN) << "tg_cli: "
<< "You should provide some valid api_id and api_hash";
LOG(PLAIN) << options;
return;
}
SET_VERBOSITY_LEVEL(new_verbosity_level); SET_VERBOSITY_LEVEL(new_verbosity_level);
{ {

View File

@ -47,6 +47,10 @@ void OptionParser::add_option(char short_key, Slice long_key, Slice description,
}); });
} }
void OptionParser::add_check(std::function<Status()> check) {
checks_.push_back(std::move(check));
}
Result<vector<char *>> OptionParser::run(int argc, char *argv[]) { Result<vector<char *>> OptionParser::run(int argc, char *argv[]) {
std::unordered_map<char, const Option *> short_options; std::unordered_map<char, const Option *> short_options;
std::unordered_map<string, const Option *> long_options; std::unordered_map<string, const Option *> long_options;
@ -143,6 +147,9 @@ Result<vector<char *>> OptionParser::run(int argc, char *argv[]) {
TRY_STATUS(option->arg_callback(parameter)); TRY_STATUS(option->arg_callback(parameter));
} }
} }
for (auto &check : checks_) {
TRY_STATUS(check());
}
return std::move(non_options); return std::move(non_options);
} }

View File

@ -40,6 +40,8 @@ class OptionParser {
void add_option(char short_key, Slice long_key, Slice description, std::function<void(void)> callback); void add_option(char short_key, Slice long_key, Slice description, std::function<void(void)> callback);
void add_check(std::function<Status()> check);
// returns found non-option parameters // returns found non-option parameters
Result<vector<char *>> run(int argc, char *argv[]) TD_WARN_UNUSED_RESULT; Result<vector<char *>> run(int argc, char *argv[]) TD_WARN_UNUSED_RESULT;
@ -47,6 +49,7 @@ class OptionParser {
private: private:
vector<Option> options_; vector<Option> options_;
vector<std::function<Status()>> checks_;
string description_; string description_;
}; };