diff options
author | ajmichael <ajmichael@google.com> | 2018-01-24 07:43:32 -0800 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-01-24 07:45:06 -0800 |
commit | 1b3569aec8c823cd88fb9eb03a6b6864951976fe (patch) | |
tree | d7eb528e31b1ed8bbf09339b08fbfb4af7960ca4 /src/main | |
parent | f48a935e72ba9497623a5cc2c914d521feb34fcf (diff) |
Support negated flags in SearchNullaryOption with last-wins semantics.
`blaze --nomaster_bazelrc --master_bazelrc` now uses the master bazelrc.
RELNOTES: None
PiperOrigin-RevId: 183083839
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/cpp/blaze_util.cc | 26 | ||||
-rw-r--r-- | src/main/cpp/blaze_util.h | 11 | ||||
-rw-r--r-- | src/main/cpp/option_processor.cc | 4 |
3 files changed, 21 insertions, 20 deletions
diff --git a/src/main/cpp/blaze_util.cc b/src/main/cpp/blaze_util.cc index 3b5d5dd322..f0a6f21e6d 100644 --- a/src/main/cpp/blaze_util.cc +++ b/src/main/cpp/blaze_util.cc @@ -103,25 +103,23 @@ const char* SearchUnaryOption(const vector<string>& args, return GetUnaryOption(args[i].c_str(), NULL, key); } -static bool SearchNullaryOption(const vector<string>& args, - const char *key, - const bool include_positional_params) { +bool SearchNullaryOption(const vector<string>& args, + const string& flag_name, + const bool default_value) { + const string positive_flag = "--" + flag_name; + const string negative_flag = "--no" + flag_name; + bool result = default_value; for (vector<string>::size_type i = 0; i < args.size(); i++) { if (args[i] == "--") { - if (!include_positional_params) { - return false; - } - continue; + break; } - if (GetNullaryOption(args[i].c_str(), key)) { - return true; + if (GetNullaryOption(args[i].c_str(), positive_flag.c_str())) { + result = true; + } else if (GetNullaryOption(args[i].c_str(), negative_flag.c_str())) { + result = false; } } - return false; -} - -bool SearchNullaryOption(const vector<string>& args, const char *key) { - return SearchNullaryOption(args, key, false); + return result; } bool VerboseLogging() { return !GetEnv("VERBOSE_BLAZE_CLIENT").empty(); } diff --git a/src/main/cpp/blaze_util.h b/src/main/cpp/blaze_util.h index 0fc1a22b23..fa056b5ff0 100644 --- a/src/main/cpp/blaze_util.h +++ b/src/main/cpp/blaze_util.h @@ -57,11 +57,14 @@ bool GetNullaryOption(const char *arg, const char *key); const char* SearchUnaryOption(const std::vector<std::string>& args, const char* key); -// Searches for 'key' in 'args' using GetNullaryOption. -// Arguments found after '--' are omitted from the search. -// Returns true iff key is a flag in args. +// Searches for '--flag_name' and '--noflag_name' in 'args' using +// GetNullaryOption. Arguments found after '--' are omitted from the search. +// Returns true if '--flag_name' is a flag in args and '--noflag_name' does not +// appear after its last occurrence. If neither '--flag_name' nor +// '--noflag_name' appear, returns 'default_value'. Otherwise, returns false. bool SearchNullaryOption(const std::vector<std::string>& args, - const char* key); + const std::string& flag_name, + const bool default_value); // Enable messages mostly of interest to developers. bool VerboseLogging(); diff --git a/src/main/cpp/option_processor.cc b/src/main/cpp/option_processor.cc index 07ea0665c6..4a6e57980b 100644 --- a/src/main/cpp/option_processor.cc +++ b/src/main/cpp/option_processor.cc @@ -327,8 +327,8 @@ blaze_exit_code::ExitCode OptionProcessor::ParseOptions( } bool use_master_blazerc = true; - if (SearchNullaryOption(cmd_line_->startup_args, "--nomaster_blazerc") || - SearchNullaryOption(cmd_line_->startup_args, "--nomaster_bazelrc")) { + if (!SearchNullaryOption(cmd_line_->startup_args, "master_blazerc", true) || + !SearchNullaryOption(cmd_line_->startup_args, "master_bazelrc", true)) { use_master_blazerc = false; } |