From 0897a140c7212020609432bfdaa607c14dbbe9f6 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 23 Sep 2019 16:55:07 +0300 Subject: [PATCH] Check_proxy: get_next_arg. GitOrigin-RevId: 9079c8f344a3bd2a8d6585c255e752abef66d3f1 --- benchmark/check_proxy.cpp | 42 +++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/benchmark/check_proxy.cpp b/benchmark/check_proxy.cpp index 990d0a886..9ce5221bb 100644 --- a/benchmark/check_proxy.cpp +++ b/benchmark/check_proxy.cpp @@ -16,7 +16,8 @@ #include static void usage() { - td::TsCerr() << "Tests specified MTProto-proxies, outputs working proxies to stdout; exits with code 0 if a working proxy was found.\n"; + td::TsCerr() << "Tests specified MTProto-proxies, outputs working proxies to stdout; exits with code 0 if a working " + "proxy was found.\n"; td::TsCerr() << "Usage: check_proxy [options] server:port:secret [server2:port2:secret2 ...]\n"; td::TsCerr() << "Options:\n"; td::TsCerr() << " -v\tSet verbosity level to N\n"; @@ -66,12 +67,27 @@ int main(int argc, char **argv) { for (int i = 1; i < argc; i++) { td::string arg(argv[i]); - if (arg.substr(0, 2) == "-v") { - if (arg.size() == 2 && i + 1 < argc && argv[i + 1][0] != '-') { - arg = argv[++i]; + + auto get_next_arg = [&i, &arg, argc, argv](bool is_optional = false) { + CHECK(arg.size() >= 2); + if (arg.size() == 2 || arg[1] == '-') { + if (i + 1 < argc && argv[i + 1][0] != '-') { + return td::string(argv[++i]); + } } else { - arg = arg.substr(2); + if (arg.size() > 2) { + return arg.substr(2); + } } + if (!is_optional) { + td::TsCerr() << "Value is required after " << arg << "\n"; + usage(); + } + return td::string(); + }; + + if (td::begins_with(arg, "-v")) { + arg = get_next_arg(true); int new_verbosity = 1; while (arg[0] == 'v') { new_verbosity++; @@ -81,18 +97,10 @@ int main(int argc, char **argv) { new_verbosity += td::to_integer(arg) - (new_verbosity == 1); } new_verbosity_level = VERBOSITY_NAME(FATAL) + new_verbosity; - } else if (arg == "-t" || arg == "--timeout") { - if (i + 1 == argc) { - td::TsCerr() << "Value is required after " << arg; - usage(); - } - timeout = td::to_double(td::string(argv[++i])); - } else if (arg == "-d" || arg == "--dc_id") { - if (i + 1 == argc) { - td::TsCerr() << "Value is required after " << arg; - usage(); - } - dc_id = td::to_integer(td::string(argv[++i])); + } else if (td::begins_with(arg, "-t") || arg == "--timeout") { + timeout = td::to_double(get_next_arg()); + } else if (td::begins_with(arg, "-d") || arg == "--dc_id") { + dc_id = td::to_integer(get_next_arg()); } else if (arg[0] == '-') { usage(); } else {