blob: 3de8873e26aa608352415065ac4885ae238044be [file] [log] [blame]
[email protected]72e2e2422012-02-27 18:38:121// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
[email protected]02c87962008-10-06 10:25:354
[email protected]bb975362009-01-21 01:00:225// This class works with command lines: building and parsing.
[email protected]a40ca4302011-05-14 01:10:246// Arguments with prefixes ('--', '-', and on Windows, '/') are switches.
7// Switches will precede all other arguments without switch prefixes.
8// Switches can optionally have values, delimited by '=', e.g., "-switch=value".
9// An argument of "--" will terminate switch parsing during initialization,
10// interpreting subsequent tokens as non-switch arguments, regardless of prefix.
[email protected]bb975362009-01-21 01:00:2211
[email protected]06cc083a2011-03-01 02:28:4212// There is a singleton read-only CommandLine that represents the command line
13// that the current process was started with. It must be initialized in main().
initial.commitd7cae122008-07-26 21:49:3814
[email protected]02c87962008-10-06 10:25:3515#ifndef BASE_COMMAND_LINE_H_
16#define BASE_COMMAND_LINE_H_
initial.commitd7cae122008-07-26 21:49:3817
[email protected]2edc2862011-04-04 18:04:3718#include <stddef.h>
initial.commitd7cae122008-07-26 21:49:3819#include <map>
20#include <string>
21#include <vector>
22
[email protected]0bea7252011-08-05 15:34:0023#include "base/base_export.h"
mgiucac9e499e2014-10-01 07:54:5724#include "base/strings/string16.h"
jackhou1bd9da92015-05-21 04:48:0025#include "base/strings/string_piece.h"
[email protected]74e9fa22010-12-29 21:06:4326#include "build/build_config.h"
initial.commitd7cae122008-07-26 21:49:3827
[email protected]a3ef4832013-02-02 05:12:3328namespace base {
[email protected]2f3b1cc2014-03-17 23:07:1529
[email protected]5d91c9e2010-07-28 17:25:2830class FilePath;
[email protected]d4515eb2009-01-30 00:40:4331
[email protected]0bea7252011-08-05 15:34:0032class BASE_EXPORT CommandLine {
initial.commitd7cae122008-07-26 21:49:3833 public:
[email protected]a502bbe72011-01-07 18:06:4534#if defined(OS_WIN)
[email protected]06cc083a2011-03-01 02:28:4235 // The native command line string type.
mgiucac9e499e2014-10-01 07:54:5736 typedef base::string16 StringType;
[email protected]a502bbe72011-01-07 18:06:4537#elif defined(OS_POSIX)
[email protected]a502bbe72011-01-07 18:06:4538 typedef std::string StringType;
39#endif
40
[email protected]a40ca4302011-05-14 01:10:2441 typedef StringType::value_type CharType;
[email protected]06cc083a2011-03-01 02:28:4242 typedef std::vector<StringType> StringVector;
[email protected]a502bbe72011-01-07 18:06:4543 typedef std::map<std::string, StringType> SwitchMap;
jackhou1bd9da92015-05-21 04:48:0044 typedef std::map<base::StringPiece, const StringType*> StringPieceSwitchMap;
[email protected]a502bbe72011-01-07 18:06:4545
[email protected]06cc083a2011-03-01 02:28:4246 // A constructor for CommandLines that only carry switches and arguments.
[email protected]947446b2010-10-21 03:36:3147 enum NoProgram { NO_PROGRAM };
48 explicit CommandLine(NoProgram no_program);
[email protected]a502bbe72011-01-07 18:06:4549
[email protected]06cc083a2011-03-01 02:28:4250 // Construct a new command line with |program| as argv[0].
[email protected]2f3b1cc2014-03-17 23:07:1551 explicit CommandLine(const FilePath& program);
[email protected]a502bbe72011-01-07 18:06:4552
[email protected]a40ca4302011-05-14 01:10:2453 // Construct a new command line from an argument list.
54 CommandLine(int argc, const CharType* const* argv);
[email protected]06cc083a2011-03-01 02:28:4255 explicit CommandLine(const StringVector& argv);
[email protected]a502bbe72011-01-07 18:06:4556
jackhou1bd9da92015-05-21 04:48:0057 // Override copy and assign to ensure |switches_by_stringpiece_| is valid.
58 CommandLine(const CommandLine& other);
59 CommandLine& operator=(const CommandLine& other);
60
[email protected]acbeb3d2011-03-01 20:47:5861 ~CommandLine();
62
[email protected]bf98a0e12013-09-25 23:36:0063#if defined(OS_WIN)
64 // By default this class will treat command-line arguments beginning with
65 // slashes as switches on Windows, but not other platforms.
66 //
67 // If this behavior is inappropriate for your application, you can call this
68 // function BEFORE initializing the current process' global command line
69 // object and the behavior will be the same as Posix systems (only hyphens
70 // begin switches, everything else will be an arg).
71 static void set_slash_is_not_a_switch();
72#endif
73
[email protected]06cc083a2011-03-01 02:28:4274 // Initialize the current process CommandLine singleton. On Windows, ignores
75 // its arguments (we instead parse GetCommandLineW() directly) because we
76 // don't trust the CRT's parsing of the command line, but it still must be
[email protected]72e2e2422012-02-27 18:38:1277 // called to set up the command line. Returns false if initialization has
78 // already occurred, and true otherwise. Only the caller receiving a 'true'
79 // return value should take responsibility for calling Reset.
80 static bool Init(int argc, const char* const* argv);
[email protected]bb975362009-01-21 01:00:2281
[email protected]a2318cda2009-02-25 21:13:5382 // Destroys the current process CommandLine singleton. This is necessary if
[email protected]06cc083a2011-03-01 02:28:4283 // you want to reset the base library to its initial state (for example, in an
[email protected]a2318cda2009-02-25 21:13:5384 // outer library that needs to be able to terminate, and be re-initialized).
[email protected]06cc083a2011-03-01 02:28:4285 // If Init is called only once, as in main(), Reset() is not necessary.
[email protected]02fb75ab2009-10-12 16:11:4086 static void Reset();
[email protected]a2318cda2009-02-25 21:13:5387
[email protected]bb975362009-01-21 01:00:2288 // Get the singleton CommandLine representing the current process's
[email protected]06cc083a2011-03-01 02:28:4289 // command line. Note: returned value is mutable, but not thread safe;
[email protected]0189bbd2009-10-12 22:50:3990 // only mutate if you know what you're doing!
[email protected]dcd869c2010-08-30 20:15:2591 static CommandLine* ForCurrentProcess();
[email protected]1a48f312008-08-12 01:14:3792
[email protected]2bf64a92013-07-11 23:10:4093 // Returns true if the CommandLine has been initialized for the given process.
94 static bool InitializedForCurrentProcess();
95
[email protected]bb975362009-01-21 01:00:2296#if defined(OS_WIN)
mgiucac9e499e2014-10-01 07:54:5797 static CommandLine FromString(const base::string16& command_line);
[email protected]06cc083a2011-03-01 02:28:4298#endif
99
[email protected]06cc083a2011-03-01 02:28:42100 // Initialize from an argv vector.
[email protected]a40ca4302011-05-14 01:10:24101 void InitFromArgv(int argc, const CharType* const* argv);
[email protected]06cc083a2011-03-01 02:28:42102 void InitFromArgv(const StringVector& argv);
[email protected]06cc083a2011-03-01 02:28:42103
[email protected]a40ca4302011-05-14 01:10:24104 // Constructs and returns the represented command line string.
[email protected]45f982e2012-10-29 21:31:31105 // CAUTION! This should be avoided on POSIX because quoting behavior is
106 // unclear.
mgiucac974d5102014-10-01 09:24:51107 StringType GetCommandLineString() const {
108 return GetCommandLineStringInternal(false);
109 }
110
111#if defined(OS_WIN)
112 // Constructs and returns the represented command line string. Assumes the
113 // command line contains placeholders (eg, %1) and quotes any program or
114 // argument with a '%' in it. This should be avoided unless the placeholder is
115 // required by an external interface (eg, the Windows registry), because it is
116 // not generally safe to replace it with an arbitrary string. If possible,
117 // placeholders should be replaced *before* converting the command line to a
118 // string.
119 StringType GetCommandLineStringWithPlaceholders() const {
120 return GetCommandLineStringInternal(true);
121 }
122#endif
[email protected]06cc083a2011-03-01 02:28:42123
[email protected]45f982e2012-10-29 21:31:31124 // Constructs and returns the represented arguments string.
125 // CAUTION! This should be avoided on POSIX because quoting behavior is
126 // unclear.
mgiucac974d5102014-10-01 09:24:51127 StringType GetArgumentsString() const {
128 return GetArgumentsStringInternal(false);
129 }
130
131#if defined(OS_WIN)
132 // Constructs and returns the represented arguments string. Assumes the
133 // command line contains placeholders (eg, %1) and quotes any argument with a
134 // '%' in it. This should be avoided unless the placeholder is required by an
135 // external interface (eg, the Windows registry), because it is not generally
136 // safe to replace it with an arbitrary string. If possible, placeholders
137 // should be replaced *before* converting the arguments to a string.
138 StringType GetArgumentsStringWithPlaceholders() const {
139 return GetArgumentsStringInternal(true);
140 }
141#endif
[email protected]45f982e2012-10-29 21:31:31142
[email protected]10e42bf2008-10-15 21:59:08143 // Returns the original command line string as a vector of strings.
[email protected]06cc083a2011-03-01 02:28:42144 const StringVector& argv() const { return argv_; }
[email protected]10e42bf2008-10-15 21:59:08145
[email protected]a40ca4302011-05-14 01:10:24146 // Get and Set the program part of the command line string (the first item).
[email protected]2f3b1cc2014-03-17 23:07:15147 FilePath GetProgram() const;
148 void SetProgram(const FilePath& program);
[email protected]0189bbd2009-10-12 22:50:39149
[email protected]06cc083a2011-03-01 02:28:42150 // Returns true if this command line contains the given switch.
jackhou1bd9da92015-05-21 04:48:00151 // Switch names must be lowercase.
152 // The second override provides an optimized version to avoid inlining codegen
153 // at every callsite to find the length of the constant and construct a
154 // StringPiece.
155 bool HasSwitch(const base::StringPiece& switch_string) const;
tapted009a1dc82015-03-30 03:57:10156 bool HasSwitch(const char switch_constant[]) const;
initial.commitd7cae122008-07-26 21:49:38157
[email protected]06cc083a2011-03-01 02:28:42158 // Returns the value associated with the given switch. If the switch has no
159 // value or isn't present, this method returns the empty string.
jackhou1bd9da92015-05-21 04:48:00160 // Switch names must be lowercase.
161 std::string GetSwitchValueASCII(const base::StringPiece& switch_string) const;
162 FilePath GetSwitchValuePath(const base::StringPiece& switch_string) const;
163 StringType GetSwitchValueNative(const base::StringPiece& switch_string) const;
[email protected]06cc083a2011-03-01 02:28:42164
[email protected]06cc083a2011-03-01 02:28:42165 // Get a copy of all switches, along with their values.
166 const SwitchMap& GetSwitches() const { return switches_; }
167
168 // Append a switch [with optional value] to the command line.
[email protected]a40ca4302011-05-14 01:10:24169 // Note: Switches will precede arguments regardless of appending order.
[email protected]06cc083a2011-03-01 02:28:42170 void AppendSwitch(const std::string& switch_string);
[email protected]a3ef4832013-02-02 05:12:33171 void AppendSwitchPath(const std::string& switch_string,
[email protected]2f3b1cc2014-03-17 23:07:15172 const FilePath& path);
[email protected]4f08c83f2010-07-29 23:02:34173 void AppendSwitchNative(const std::string& switch_string,
174 const StringType& value);
[email protected]d420c31e2010-07-30 02:14:22175 void AppendSwitchASCII(const std::string& switch_string,
176 const std::string& value);
[email protected]4f08c83f2010-07-29 23:02:34177
[email protected]06cc083a2011-03-01 02:28:42178 // Copy a set of switches (and any values) from another command line.
179 // Commonly used when launching a subprocess.
[email protected]a40ca4302011-05-14 01:10:24180 void CopySwitchesFrom(const CommandLine& source,
181 const char* const switches[],
[email protected]06cc083a2011-03-01 02:28:42182 size_t count);
183
184 // Get the remaining arguments to the command.
[email protected]75f1c782011-07-13 23:41:22185 StringVector GetArgs() const;
[email protected]06cc083a2011-03-01 02:28:42186
187 // Append an argument to the command line. Note that the argument is quoted
188 // properly such that it is interpreted as one argument to the target command.
189 // AppendArg is primarily for ASCII; non-ASCII input is interpreted as UTF-8.
[email protected]a40ca4302011-05-14 01:10:24190 // Note: Switches will precede arguments regardless of appending order.
[email protected]0445eb42010-08-13 22:10:30191 void AppendArg(const std::string& value);
[email protected]2f3b1cc2014-03-17 23:07:15192 void AppendArgPath(const FilePath& value);
[email protected]0445eb42010-08-13 22:10:30193 void AppendArgNative(const StringType& value);
[email protected]bb975362009-01-21 01:00:22194
[email protected]a40ca4302011-05-14 01:10:24195 // Append the switches and arguments from another command line to this one.
[email protected]bb975362009-01-21 01:00:22196 // If |include_program| is true, include |other|'s program as well.
[email protected]a40ca4302011-05-14 01:10:24197 void AppendArguments(const CommandLine& other, bool include_program);
initial.commitd7cae122008-07-26 21:49:38198
[email protected]06cc083a2011-03-01 02:28:42199 // Insert a command before the current command.
200 // Common for debuggers, like "valgrind" or "gdb --args".
[email protected]13081fc2010-08-04 18:24:38201 void PrependWrapper(const StringType& wrapper);
[email protected]052f1d482009-02-10 00:52:14202
[email protected]06cc083a2011-03-01 02:28:42203#if defined(OS_WIN)
204 // Initialize by parsing the given command line string.
205 // The program name is assumed to be the first item in the string.
mgiucac9e499e2014-10-01 07:54:57206 void ParseFromString(const base::string16& command_line);
[email protected]06cc083a2011-03-01 02:28:42207#endif
[email protected]4f08c83f2010-07-29 23:02:34208
initial.commitd7cae122008-07-26 21:49:38209 private:
[email protected]a40ca4302011-05-14 01:10:24210 // Disallow default constructor; a program name must be explicitly specified.
[email protected]acbeb3d2011-03-01 20:47:58211 CommandLine();
[email protected]a40ca4302011-05-14 01:10:24212 // Allow the copy constructor. A common pattern is to copy of the current
213 // process's command line and then add some flags to it. For example:
214 // CommandLine cl(*CommandLine::ForCurrentProcess());
215 // cl.AppendSwitch(...);
[email protected]d4515eb2009-01-30 00:40:43216
mgiucac974d5102014-10-01 09:24:51217 // Internal version of GetCommandLineString. If |quote_placeholders| is true,
218 // also quotes parts with '%' in them.
219 StringType GetCommandLineStringInternal(bool quote_placeholders) const;
220
221 // Internal version of GetArgumentsString. If |quote_placeholders| is true,
222 // also quotes parts with '%' in them.
223 StringType GetArgumentsStringInternal(bool quote_placeholders) const;
224
jackhou1bd9da92015-05-21 04:48:00225 // Reconstruct |switches_by_stringpiece| to be a mirror of |switches|.
226 // |switches_by_stringpiece| only contains pointers to objects owned by
227 // |switches|.
228 void ResetStringPieces();
229
[email protected]06cc083a2011-03-01 02:28:42230 // The singleton CommandLine representing the current process's command line.
[email protected]bb975362009-01-21 01:00:22231 static CommandLine* current_process_commandline_;
initial.commitd7cae122008-07-26 21:49:38232
[email protected]a40ca4302011-05-14 01:10:24233 // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* }
[email protected]06cc083a2011-03-01 02:28:42234 StringVector argv_;
[email protected]bb975362009-01-21 01:00:22235
[email protected]06cc083a2011-03-01 02:28:42236 // Parsed-out switch keys and values.
[email protected]f6c34832010-05-24 10:39:59237 SwitchMap switches_;
[email protected]bb975362009-01-21 01:00:22238
jackhou1bd9da92015-05-21 04:48:00239 // A mirror of |switches_| with only references to the actual strings.
240 // The StringPiece internally holds a pointer to a key in |switches_| while
241 // the mapped_type points to a value in |switches_|.
242 // Used for allocation-free lookups.
243 StringPieceSwitchMap switches_by_stringpiece_;
244
[email protected]a40ca4302011-05-14 01:10:24245 // The index after the program and switches, any arguments start here.
246 size_t begin_args_;
initial.commitd7cae122008-07-26 21:49:38247};
248
[email protected]2f3b1cc2014-03-17 23:07:15249} // namespace base
250
[email protected]02c87962008-10-06 10:25:35251#endif // BASE_COMMAND_LINE_H_