Skip to content

Commit

Permalink
Don't use -- in exec_script
Browse files Browse the repository at this point in the history
Instead of passing -- in CommandLine construction for exec_script,
which relies on the script running handling --, add a flag to
CommandLine to control its behavior.

This allow script languages that don't support -- as a special
flag.

Change-Id: I2e6d0c2233e4f3cfae8718a7ad98552663216396
Reviewed-on: https://gn-review.googlesource.com/2660
Reviewed-by: Brett Wilson <[email protected]>
Commit-Queue: Brett Wilson <[email protected]>
  • Loading branch information
stuartmorgan authored and Commit Bot committed Aug 13, 2018
1 parent 27ef6b4 commit c218b83
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
25 changes: 15 additions & 10 deletions base/command_line.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,12 @@ bool IsSwitch(const CommandLine::StringType& string,
return true;
}

// Append switches and arguments, keeping switches before arguments.
// Append switches and arguments, keeping switches before arguments
// if handle_switches is true.
void AppendSwitchesAndArguments(CommandLine* command_line,
const CommandLine::StringVector& argv) {
bool parse_switches = true;
const CommandLine::StringVector& argv,
bool handle_switches) {
bool parse_switches = handle_switches;
for (size_t i = 1; i < argv.size(); ++i) {
CommandLine::StringType arg = argv[i];
#if defined(OS_WIN)
Expand Down Expand Up @@ -155,18 +157,21 @@ string16 QuoteForCommandLineToArgvW(const string16& arg,

} // namespace

CommandLine::CommandLine(NoProgram no_program) : argv_(1), begin_args_(1) {}
CommandLine::CommandLine(NoProgram no_program)
: argv_(1), begin_args_(1), parse_switches_(true) {}

CommandLine::CommandLine(const FilePath& program) : argv_(1), begin_args_(1) {
CommandLine::CommandLine(const FilePath& program)
: argv_(1), begin_args_(1), parse_switches_(true) {
SetProgram(program);
}

CommandLine::CommandLine(int argc, const CommandLine::CharType* const* argv)
: argv_(1), begin_args_(1) {
: argv_(1), begin_args_(1), parse_switches_(true) {
InitFromArgv(argc, argv);
}

CommandLine::CommandLine(const StringVector& argv) : argv_(1), begin_args_(1) {
CommandLine::CommandLine(const StringVector& argv)
: argv_(1), begin_args_(1), parse_switches_(true) {
InitFromArgv(argv);
}

Expand Down Expand Up @@ -257,7 +262,7 @@ void CommandLine::InitFromArgv(const StringVector& argv) {
switches_.clear();
begin_args_ = 1;
SetProgram(argv.empty() ? FilePath() : FilePath(argv[0]));
AppendSwitchesAndArguments(this, argv);
AppendSwitchesAndArguments(this, argv, parse_switches_);
}

FilePath CommandLine::GetProgram() const {
Expand Down Expand Up @@ -395,7 +400,7 @@ void CommandLine::AppendArguments(const CommandLine& other,
bool include_program) {
if (include_program)
SetProgram(other.GetProgram());
AppendSwitchesAndArguments(this, other.argv());
AppendSwitchesAndArguments(this, other.argv(), parse_switches_);
}

void CommandLine::PrependWrapper(const CommandLine::StringType& wrapper) {
Expand Down Expand Up @@ -451,7 +456,7 @@ CommandLine::StringType CommandLine::GetArgumentsStringInternal(
bool quote_placeholders) const {
StringType params;
// Append switches and arguments.
bool parse_switches = true;
bool parse_switches = parse_switches_;
for (size_t i = 1; i < argv_.size(); ++i) {
StringType arg = argv_[i];
StringType switch_string;
Expand Down
10 changes: 10 additions & 0 deletions base/command_line.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ class CommandLine {
FilePath GetProgram() const;
void SetProgram(const FilePath& program);

// Enables/disables the parsing of switches for future argument appending.
// True by default, but can be set to false to ensure that no re-ordering
// is done.
void SetParseSwitches(bool parse_switches) {
parse_switches_ = parse_switches;
}

// Returns true if this command line contains the given switch.
// Switch names must be lowercase.
// The second override provides an optimized version to avoid inlining codegen
Expand Down Expand Up @@ -238,6 +245,9 @@ class CommandLine {

// The index after the program and switches, any arguments start here.
size_t begin_args_;

// Whether or not to parse arguments that look like switches as switches.
bool parse_switches_;
};

} // namespace base
Expand Down
8 changes: 3 additions & 5 deletions tools/gn/function_exec_script.cc
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,9 @@ Value RunExecScript(Scope* scope,
const base::FilePath& python_path = build_settings->python_path();
base::CommandLine cmdline(python_path);

// CommandLine tries to interpret arguments by default. Passing "--" disables
// this for everything following the "--", so pass this as the very first
// thing to python. Python ignores a -- before the .py file, and this makes
// CommandLine let through arguments without modifying them.
cmdline.AppendArg("--");
// CommandLine tries to interpret arguments by default. Disable that so
// that the arguments will be passed through exactly as specified.
cmdline.SetParseSwitches(false);

cmdline.AppendArgPath(script_path);

Expand Down

0 comments on commit c218b83

Please sign in to comment.