Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(320)

Side by Side Diff: base/command_line.h

Issue 595803002: base::CommandLine: Added optional quoting of placeholder arguments. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Split into two separate methods. Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | base/command_line.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // This class works with command lines: building and parsing. 5 // This class works with command lines: building and parsing.
6 // Arguments with prefixes ('--', '-', and on Windows, '/') are switches. 6 // Arguments with prefixes ('--', '-', and on Windows, '/') are switches.
7 // Switches will precede all other arguments without switch prefixes. 7 // Switches will precede all other arguments without switch prefixes.
8 // Switches can optionally have values, delimited by '=', e.g., "-switch=value". 8 // Switches can optionally have values, delimited by '=', e.g., "-switch=value".
9 // An argument of "--" will terminate switch parsing during initialization, 9 // An argument of "--" will terminate switch parsing during initialization,
10 // interpreting subsequent tokens as non-switch arguments, regardless of prefix. 10 // interpreting subsequent tokens as non-switch arguments, regardless of prefix.
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 static CommandLine FromString(const std::wstring& command_line); 90 static CommandLine FromString(const std::wstring& command_line);
91 #endif 91 #endif
92 92
93 // Initialize from an argv vector. 93 // Initialize from an argv vector.
94 void InitFromArgv(int argc, const CharType* const* argv); 94 void InitFromArgv(int argc, const CharType* const* argv);
95 void InitFromArgv(const StringVector& argv); 95 void InitFromArgv(const StringVector& argv);
96 96
97 // Constructs and returns the represented command line string. 97 // Constructs and returns the represented command line string.
98 // CAUTION! This should be avoided on POSIX because quoting behavior is 98 // CAUTION! This should be avoided on POSIX because quoting behavior is
99 // unclear. 99 // unclear.
100 StringType GetCommandLineString() const; 100 StringType GetCommandLineString() const {
101 return GetCommandLineStringInternal(false);
102 }
103
104 #if defined(OS_WIN)
105 // Constructs and returns the represented command line string. Assumes the
106 // command line contains placeholders (eg, %1) and quotes any program or
107 // argument with a '%' in it.
Mark Mentovai 2014/09/25 13:14:15 The comment here and on the other method should sa
Matt Giuca 2014/09/26 03:49:05 Done.
108 StringType GetCommandLineStringWithPlaceholders() const {
109 return GetCommandLineStringInternal(true);
110 }
111 #endif
101 112
102 // Constructs and returns the represented arguments string. 113 // Constructs and returns the represented arguments string.
103 // CAUTION! This should be avoided on POSIX because quoting behavior is 114 // CAUTION! This should be avoided on POSIX because quoting behavior is
104 // unclear. 115 // unclear.
105 StringType GetArgumentsString() const; 116 StringType GetArgumentsString() const {
117 return GetArgumentsStringInternal(false);
118 }
119
120 #if defined(OS_WIN)
121 // Constructs and returns the represented arguments string. Assumes the
122 // command line contains placeholders (eg, %1) and quotes any argument with a
123 // '%' in it.
124 StringType GetArgumentsStringWithPlaceholders() const {
125 return GetArgumentsStringInternal(true);
126 }
127 #endif
106 128
107 // Returns the original command line string as a vector of strings. 129 // Returns the original command line string as a vector of strings.
108 const StringVector& argv() const { return argv_; } 130 const StringVector& argv() const { return argv_; }
109 131
110 // Get and Set the program part of the command line string (the first item). 132 // Get and Set the program part of the command line string (the first item).
111 FilePath GetProgram() const; 133 FilePath GetProgram() const;
112 void SetProgram(const FilePath& program); 134 void SetProgram(const FilePath& program);
113 135
114 // Returns true if this command line contains the given switch. 136 // Returns true if this command line contains the given switch.
115 // (Switch names are case-insensitive). 137 // (Switch names are case-insensitive).
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 #endif 188 #endif
167 189
168 private: 190 private:
169 // Disallow default constructor; a program name must be explicitly specified. 191 // Disallow default constructor; a program name must be explicitly specified.
170 CommandLine(); 192 CommandLine();
171 // Allow the copy constructor. A common pattern is to copy of the current 193 // Allow the copy constructor. A common pattern is to copy of the current
172 // process's command line and then add some flags to it. For example: 194 // process's command line and then add some flags to it. For example:
173 // CommandLine cl(*CommandLine::ForCurrentProcess()); 195 // CommandLine cl(*CommandLine::ForCurrentProcess());
174 // cl.AppendSwitch(...); 196 // cl.AppendSwitch(...);
175 197
198 // Internal version of GetCommandLineString. If |quote_placeholders| is true,
199 // also quotes parts with '%' in them.
200 StringType GetCommandLineStringInternal(bool quote_placeholders) const;
201
202 // Internal version of GetArgumentsString. If |quote_placeholders| is true,
203 // also quotes parts with '%' in them.
204 StringType GetArgumentsStringInternal(bool quote_placeholders) const;
205
176 // The singleton CommandLine representing the current process's command line. 206 // The singleton CommandLine representing the current process's command line.
177 static CommandLine* current_process_commandline_; 207 static CommandLine* current_process_commandline_;
178 208
179 // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* } 209 // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* }
180 StringVector argv_; 210 StringVector argv_;
181 211
182 // Parsed-out switch keys and values. 212 // Parsed-out switch keys and values.
183 SwitchMap switches_; 213 SwitchMap switches_;
184 214
185 // The index after the program and switches, any arguments start here. 215 // The index after the program and switches, any arguments start here.
186 size_t begin_args_; 216 size_t begin_args_;
187 }; 217 };
188 218
189 } // namespace base 219 } // namespace base
190 220
191 // TODO(brettw) remove once all callers specify the namespace properly. 221 // TODO(brettw) remove once all callers specify the namespace properly.
192 using base::CommandLine; 222 using base::CommandLine;
193 223
194 #endif // BASE_COMMAND_LINE_H_ 224 #endif // BASE_COMMAND_LINE_H_
OLDNEW
« no previous file with comments | « no previous file | base/command_line.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698