From 720266e97a24d9ad59a66b284320750a9b3e1ae4 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Fri, 29 Dec 2017 14:00:02 -0800 Subject: [PATCH] Support equal signs in options, e.g. --log-file=a.log --- src/options.cc | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/options.cc b/src/options.cc index 4d603f19..b0962f5f 100644 --- a/src/options.cc +++ b/src/options.cc @@ -8,22 +8,16 @@ std::unordered_map ParseOptions(int argc, char** argv) { std::unordered_map output; - std::string previous_arg; - for (int i = 1; i < argc; ++i) { std::string arg = argv[i]; - - if (arg[0] != '-') { - if (previous_arg.size() == 0) { - LOG_S(FATAL) << "Invalid arguments; switches must start with -"; - exit(1); - } - - output[previous_arg] = arg; - previous_arg = ""; - } else { - output[arg] = ""; - previous_arg = arg; + if (arg[0] == '-') { + auto equal = arg.find('='); + if (equal != std::string::npos) { + output[arg.substr(0, equal)] = arg.substr(equal + 1); + } else if (i + 1 < argc && argv[i + 1][0] != '-') + output[arg] = argv[++i]; + else + output[arg] = ""; } } @@ -33,4 +27,4 @@ std::unordered_map ParseOptions(int argc, bool HasOption(const std::unordered_map& options, const std::string& option) { return options.find(option) != options.end(); -} \ No newline at end of file +}