aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/cpp/option_processor.cc
diff options
context:
space:
mode:
authorGravatar Luis Fernando Pino Duque <lpino@google.com>2016-11-21 13:20:50 +0000
committerGravatar Dmitry Lomov <dslomov@google.com>2016-11-21 19:41:18 +0000
commit0311fc5ed3a1e789dc2c09eb5c44849b5507f46a (patch)
tree32dad2c046492c29a1feaa6f73548efc4985aa0f /src/main/cpp/option_processor.cc
parent480a2cf4d3684ad9171077a279f1743192096d7b (diff)
Provide a SplitCommandLine method in the option processor
that takes a given command line args and splits it into the corresponding {binary, startup_args, command, command_args}. The purpose of this function is to help split the responsibilities of the ParseOptions function by processing the startup options independently (i.e. rc files detection and processing) from the command options. This will be combined with ParseOptions in a subsequent CL. -- MOS_MIGRATED_REVID=139773786
Diffstat (limited to 'src/main/cpp/option_processor.cc')
-rw-r--r--src/main/cpp/option_processor.cc84
1 files changed, 79 insertions, 5 deletions
diff --git a/src/main/cpp/option_processor.cc b/src/main/cpp/option_processor.cc
index 29f59cf8fe..6be88237d1 100644
--- a/src/main/cpp/option_processor.cc
+++ b/src/main/cpp/option_processor.cc
@@ -167,6 +167,85 @@ OptionProcessor::OptionProcessor(
parsed_startup_options_(std::move(default_startup_options)) {
}
+std::unique_ptr<CommandLine> OptionProcessor::SplitCommandLine(
+ const vector<string>& args,
+ string* error) {
+ const string lowercase_product_name =
+ parsed_startup_options_->GetLowercaseProductName();
+
+ if (args.empty()) {
+ blaze_util::StringPrintf(error,
+ "Unable to split command line, args is empty");
+ return nullptr;
+ }
+
+ const string path_to_binary(args[0]);
+
+ // Process the startup options.
+ vector<string> startup_args;
+ vector<string>::size_type i = 1;
+ while (i < args.size() && IsArg(args[i])) {
+ const string current_arg(args[i]);
+ // If the current argument is a valid nullary startup option such as
+ // --master_bazelrc or --nomaster_bazelrc proceed to examine the next
+ // argument.
+ if (parsed_startup_options_->IsNullary(current_arg)) {
+ startup_args.push_back(current_arg);
+ i++;
+ } else if (parsed_startup_options_->IsUnary(current_arg)) {
+ // If the current argument is a valid unary startup option such as
+ // --bazelrc there are two cases to consider.
+
+ // The option is of the form '--bazelrc=value', hence proceed to
+ // examine the next argument.
+ if (current_arg.find("=") != string::npos) {
+ startup_args.push_back(current_arg);
+ i++;
+ } else {
+ // Otherwise, the option is of the form '--bazelrc value', hence
+ // skip the next argument and proceed to examine the argument located
+ // two places after.
+ if (i + 1 >= args.size()) {
+ blaze_util::StringPrintf(
+ error,
+ "Startup option '%s' expects a value.\n"
+ "Usage: '%s=somevalue' or '%s somevalue'.\n"
+ " For more info, run '%s help startup_options'.",
+ current_arg.c_str(), current_arg.c_str(), current_arg.c_str(),
+ lowercase_product_name.c_str());
+ return nullptr;
+ }
+ // In this case we transform it to the form '--bazelrc=value'.
+ startup_args.push_back(current_arg + string("=") + args[i + 1]);
+ i += 2;
+ }
+ } else {
+ // If the current argument is not a valid unary or nullary startup option
+ // then fail.
+ blaze_util::StringPrintf(
+ error,
+ "Unknown %s startup option: '%s'.\n"
+ " For more info, run '%s help startup_options'.",
+ lowercase_product_name.c_str(), current_arg.c_str(),
+ lowercase_product_name.c_str());
+ return nullptr;
+ }
+ }
+
+ // The command is the arg right after the startup options.
+ if (i == args.size()) {
+ return std::unique_ptr<CommandLine>(
+ new CommandLine(path_to_binary, startup_args, "", {}));
+ }
+ const string command(args[i]);
+
+ // The rest are the command arguments.
+ const vector<string> command_args(args.begin() + i + 1, args.end());
+
+ return std::unique_ptr<CommandLine>(
+ new CommandLine(path_to_binary, startup_args, command, command_args));
+}
+
// Return the path to the user's rc file. If cmdLineRcFile != NULL,
// use it, dying if it is not readable. Otherwise, return the first
// readable file called rc_basename from [workspace, $HOME]
@@ -318,11 +397,6 @@ blaze_exit_code::ExitCode OptionProcessor::ParseOptions(
return ParseOptions(args, workspace, cwd, error);
}
-static bool IsArg(const string& arg) {
- return blaze_util::starts_with(arg, "-") && (arg != "--help")
- && (arg != "-help") && (arg != "-h");
-}
-
blaze_exit_code::ExitCode OptionProcessor::ParseStartupOptions(string *error) {
// Process rcfile startup options
map< string, vector<RcOption> >::const_iterator it =