blob: 3d29f8fee7fbb8de9e8c1fb691515ddb8bdccbdd [file] [log] [blame]
erikwright@chromium.org506c72b2012-02-28 03:38:12 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botf003cfe2008-08-24 09:55:55 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
deanm@chromium.orga638cc92008-10-06 19:25:35 +09004
evan@chromium.org4bbc6132009-01-21 10:00:22 +09005// This class works with command lines: building and parsing.
msw@chromium.org53fbac12011-05-14 10:10:24 +09006// 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.
evan@chromium.org4bbc6132009-01-21 10:00:22 +090011
msw@chromium.org7b2af612011-03-01 11:28:42 +090012// 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.commit3f4a7322008-07-27 06:49:38 +090014
deanm@chromium.orga638cc92008-10-06 19:25:35 +090015#ifndef BASE_COMMAND_LINE_H_
16#define BASE_COMMAND_LINE_H_
initial.commit3f4a7322008-07-27 06:49:38 +090017
jhawkins@chromium.org8e73d062011-04-05 03:04:37 +090018#include <stddef.h>
initial.commit3f4a7322008-07-27 06:49:38 +090019#include <map>
20#include <string>
21#include <vector>
22
darin@chromium.orge585bed2011-08-06 00:34:00 +090023#include "base/base_export.h"
mgiuca9959b0d2014-10-01 16:54:57 +090024#include "base/strings/string16.h"
jackhoua5995af2015-05-21 13:48:00 +090025#include "base/strings/string_piece.h"
brettw@chromium.org6a3802c2010-12-30 06:06:43 +090026#include "build/build_config.h"
initial.commit3f4a7322008-07-27 06:49:38 +090027
brettw@chromium.orga7086942013-02-02 14:12:33 +090028namespace base {
brettw@chromium.org37273892014-03-18 08:07:15 +090029
erg@google.com6e67c1d2010-07-29 02:25:28 +090030class FilePath;
sky@google.com87bdd752009-01-30 09:40:43 +090031
darin@chromium.orge585bed2011-08-06 00:34:00 +090032class BASE_EXPORT CommandLine {
initial.commit3f4a7322008-07-27 06:49:38 +090033 public:
erg@google.comd5fffd42011-01-08 03:06:45 +090034#if defined(OS_WIN)
msw@chromium.org7b2af612011-03-01 11:28:42 +090035 // The native command line string type.
thestigfb1da502016-07-15 11:50:16 +090036 using StringType = string16;
erg@google.comd5fffd42011-01-08 03:06:45 +090037#elif defined(OS_POSIX)
thestigfb1da502016-07-15 11:50:16 +090038 using StringType = std::string;
erg@google.comd5fffd42011-01-08 03:06:45 +090039#endif
40
thestigfb1da502016-07-15 11:50:16 +090041 using CharType = StringType::value_type;
42 using StringVector = std::vector<StringType>;
43 using SwitchMap = std::map<std::string, StringType>;
44 using StringPieceSwitchMap = std::map<StringPiece, const StringType*>;
erg@google.comd5fffd42011-01-08 03:06:45 +090045
msw@chromium.org7b2af612011-03-01 11:28:42 +090046 // A constructor for CommandLines that only carry switches and arguments.
mattm@chromium.orgaf4c7a02010-10-21 12:36:31 +090047 enum NoProgram { NO_PROGRAM };
48 explicit CommandLine(NoProgram no_program);
erg@google.comd5fffd42011-01-08 03:06:45 +090049
msw@chromium.org7b2af612011-03-01 11:28:42 +090050 // Construct a new command line with |program| as argv[0].
brettw@chromium.org37273892014-03-18 08:07:15 +090051 explicit CommandLine(const FilePath& program);
erg@google.comd5fffd42011-01-08 03:06:45 +090052
msw@chromium.org53fbac12011-05-14 10:10:24 +090053 // Construct a new command line from an argument list.
54 CommandLine(int argc, const CharType* const* argv);
msw@chromium.org7b2af612011-03-01 11:28:42 +090055 explicit CommandLine(const StringVector& argv);
erg@google.comd5fffd42011-01-08 03:06:45 +090056
jackhoua5995af2015-05-21 13:48:00 +090057 // Override copy and assign to ensure |switches_by_stringpiece_| is valid.
58 CommandLine(const CommandLine& other);
59 CommandLine& operator=(const CommandLine& other);
60
msw@chromium.org96b695b2011-03-02 05:47:58 +090061 ~CommandLine();
62
brettw@chromium.org40b2d582013-09-26 08:36:00 +090063#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();
anantad52b7112016-06-23 06:40:31 +090072
73 // Normally when the CommandLine singleton is initialized it gets the command
74 // line via the GetCommandLineW API and then uses the shell32 API
75 // CommandLineToArgvW to parse the command line and convert it back to
76 // argc and argv. Tests who don't want this dependency on shell32 and need
77 // to honor the arguments passed in should use this function.
78 static void InitUsingArgvForTesting(int argc, const char* const* argv);
brettw@chromium.org40b2d582013-09-26 08:36:00 +090079#endif
80
msw@chromium.org7b2af612011-03-01 11:28:42 +090081 // Initialize the current process CommandLine singleton. On Windows, ignores
82 // its arguments (we instead parse GetCommandLineW() directly) because we
83 // don't trust the CRT's parsing of the command line, but it still must be
erikwright@chromium.org506c72b2012-02-28 03:38:12 +090084 // called to set up the command line. Returns false if initialization has
85 // already occurred, and true otherwise. Only the caller receiving a 'true'
86 // return value should take responsibility for calling Reset.
87 static bool Init(int argc, const char* const* argv);
evan@chromium.org4bbc6132009-01-21 10:00:22 +090088
evan@chromium.orgfee6d862009-02-26 06:13:53 +090089 // Destroys the current process CommandLine singleton. This is necessary if
msw@chromium.org7b2af612011-03-01 11:28:42 +090090 // you want to reset the base library to its initial state (for example, in an
evan@chromium.orgfee6d862009-02-26 06:13:53 +090091 // outer library that needs to be able to terminate, and be re-initialized).
msw@chromium.org7b2af612011-03-01 11:28:42 +090092 // If Init is called only once, as in main(), Reset() is not necessary.
thestigfb1da502016-07-15 11:50:16 +090093 // Do not call this in tests. Use base::test::ScopedCommandLine instead.
evan@chromium.orgb6e7c2b2009-10-13 01:11:40 +090094 static void Reset();
evan@chromium.orgfee6d862009-02-26 06:13:53 +090095
evan@chromium.org4bbc6132009-01-21 10:00:22 +090096 // Get the singleton CommandLine representing the current process's
msw@chromium.org7b2af612011-03-01 11:28:42 +090097 // command line. Note: returned value is mutable, but not thread safe;
evan@chromium.org757621d2009-10-13 07:50:39 +090098 // only mutate if you know what you're doing!
erg@google.comcb8b2cb2010-08-31 05:15:25 +090099 static CommandLine* ForCurrentProcess();
evanm@google.com638e9fb2008-08-12 10:14:37 +0900100
vollick@chromium.org4ff9adc2013-07-12 08:10:40 +0900101 // Returns true if the CommandLine has been initialized for the given process.
102 static bool InitializedForCurrentProcess();
103
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900104#if defined(OS_WIN)
thestigfb1da502016-07-15 11:50:16 +0900105 static CommandLine FromString(const string16& command_line);
msw@chromium.org7b2af612011-03-01 11:28:42 +0900106#endif
107
msw@chromium.org7b2af612011-03-01 11:28:42 +0900108 // Initialize from an argv vector.
msw@chromium.org53fbac12011-05-14 10:10:24 +0900109 void InitFromArgv(int argc, const CharType* const* argv);
msw@chromium.org7b2af612011-03-01 11:28:42 +0900110 void InitFromArgv(const StringVector& argv);
msw@chromium.org7b2af612011-03-01 11:28:42 +0900111
msw@chromium.org53fbac12011-05-14 10:10:24 +0900112 // Constructs and returns the represented command line string.
gab@chromium.orge399ff42012-10-30 06:31:31 +0900113 // CAUTION! This should be avoided on POSIX because quoting behavior is
114 // unclear.
mgiuca90d74492014-10-01 18:24:51 +0900115 StringType GetCommandLineString() const {
116 return GetCommandLineStringInternal(false);
117 }
118
119#if defined(OS_WIN)
120 // Constructs and returns the represented command line string. Assumes the
121 // command line contains placeholders (eg, %1) and quotes any program or
122 // argument with a '%' in it. This should be avoided unless the placeholder is
123 // required by an external interface (eg, the Windows registry), because it is
124 // not generally safe to replace it with an arbitrary string. If possible,
125 // placeholders should be replaced *before* converting the command line to a
126 // string.
127 StringType GetCommandLineStringWithPlaceholders() const {
128 return GetCommandLineStringInternal(true);
129 }
130#endif
msw@chromium.org7b2af612011-03-01 11:28:42 +0900131
gab@chromium.orge399ff42012-10-30 06:31:31 +0900132 // Constructs and returns the represented arguments string.
133 // CAUTION! This should be avoided on POSIX because quoting behavior is
134 // unclear.
mgiuca90d74492014-10-01 18:24:51 +0900135 StringType GetArgumentsString() const {
136 return GetArgumentsStringInternal(false);
137 }
138
139#if defined(OS_WIN)
140 // Constructs and returns the represented arguments string. Assumes the
141 // command line contains placeholders (eg, %1) and quotes any argument with a
142 // '%' in it. This should be avoided unless the placeholder is required by an
143 // external interface (eg, the Windows registry), because it is not generally
144 // safe to replace it with an arbitrary string. If possible, placeholders
145 // should be replaced *before* converting the arguments to a string.
146 StringType GetArgumentsStringWithPlaceholders() const {
147 return GetArgumentsStringInternal(true);
148 }
149#endif
gab@chromium.orge399ff42012-10-30 06:31:31 +0900150
estade@chromium.org92c59f22008-10-16 06:59:08 +0900151 // Returns the original command line string as a vector of strings.
msw@chromium.org7b2af612011-03-01 11:28:42 +0900152 const StringVector& argv() const { return argv_; }
estade@chromium.org92c59f22008-10-16 06:59:08 +0900153
msw@chromium.org53fbac12011-05-14 10:10:24 +0900154 // Get and Set the program part of the command line string (the first item).
brettw@chromium.org37273892014-03-18 08:07:15 +0900155 FilePath GetProgram() const;
156 void SetProgram(const FilePath& program);
evan@chromium.org757621d2009-10-13 07:50:39 +0900157
msw@chromium.org7b2af612011-03-01 11:28:42 +0900158 // Returns true if this command line contains the given switch.
jackhoua5995af2015-05-21 13:48:00 +0900159 // Switch names must be lowercase.
160 // The second override provides an optimized version to avoid inlining codegen
161 // at every callsite to find the length of the constant and construct a
162 // StringPiece.
thestigfb1da502016-07-15 11:50:16 +0900163 bool HasSwitch(const StringPiece& switch_string) const;
tapted5762d502015-03-30 12:57:10 +0900164 bool HasSwitch(const char switch_constant[]) const;
initial.commit3f4a7322008-07-27 06:49:38 +0900165
msw@chromium.org7b2af612011-03-01 11:28:42 +0900166 // Returns the value associated with the given switch. If the switch has no
167 // value or isn't present, this method returns the empty string.
jackhoua5995af2015-05-21 13:48:00 +0900168 // Switch names must be lowercase.
thestigfb1da502016-07-15 11:50:16 +0900169 std::string GetSwitchValueASCII(const StringPiece& switch_string) const;
170 FilePath GetSwitchValuePath(const StringPiece& switch_string) const;
171 StringType GetSwitchValueNative(const StringPiece& switch_string) const;
msw@chromium.org7b2af612011-03-01 11:28:42 +0900172
msw@chromium.org7b2af612011-03-01 11:28:42 +0900173 // Get a copy of all switches, along with their values.
174 const SwitchMap& GetSwitches() const { return switches_; }
175
176 // Append a switch [with optional value] to the command line.
msw@chromium.org53fbac12011-05-14 10:10:24 +0900177 // Note: Switches will precede arguments regardless of appending order.
msw@chromium.org7b2af612011-03-01 11:28:42 +0900178 void AppendSwitch(const std::string& switch_string);
brettw@chromium.orga7086942013-02-02 14:12:33 +0900179 void AppendSwitchPath(const std::string& switch_string,
brettw@chromium.org37273892014-03-18 08:07:15 +0900180 const FilePath& path);
evan@chromium.org0b90a5c2010-07-30 08:02:34 +0900181 void AppendSwitchNative(const std::string& switch_string,
182 const StringType& value);
evan@chromium.org3f985142010-07-30 11:14:22 +0900183 void AppendSwitchASCII(const std::string& switch_string,
184 const std::string& value);
evan@chromium.org0b90a5c2010-07-30 08:02:34 +0900185
msw@chromium.org7b2af612011-03-01 11:28:42 +0900186 // Copy a set of switches (and any values) from another command line.
187 // Commonly used when launching a subprocess.
msw@chromium.org53fbac12011-05-14 10:10:24 +0900188 void CopySwitchesFrom(const CommandLine& source,
189 const char* const switches[],
msw@chromium.org7b2af612011-03-01 11:28:42 +0900190 size_t count);
191
192 // Get the remaining arguments to the command.
msw@chromium.orga25adf42011-07-14 08:41:22 +0900193 StringVector GetArgs() const;
msw@chromium.org7b2af612011-03-01 11:28:42 +0900194
195 // Append an argument to the command line. Note that the argument is quoted
196 // properly such that it is interpreted as one argument to the target command.
197 // AppendArg is primarily for ASCII; non-ASCII input is interpreted as UTF-8.
msw@chromium.org53fbac12011-05-14 10:10:24 +0900198 // Note: Switches will precede arguments regardless of appending order.
evan@chromium.org580ab7f2010-08-14 07:10:30 +0900199 void AppendArg(const std::string& value);
brettw@chromium.org37273892014-03-18 08:07:15 +0900200 void AppendArgPath(const FilePath& value);
evan@chromium.org580ab7f2010-08-14 07:10:30 +0900201 void AppendArgNative(const StringType& value);
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900202
msw@chromium.org53fbac12011-05-14 10:10:24 +0900203 // Append the switches and arguments from another command line to this one.
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900204 // If |include_program| is true, include |other|'s program as well.
msw@chromium.org53fbac12011-05-14 10:10:24 +0900205 void AppendArguments(const CommandLine& other, bool include_program);
initial.commit3f4a7322008-07-27 06:49:38 +0900206
msw@chromium.org7b2af612011-03-01 11:28:42 +0900207 // Insert a command before the current command.
208 // Common for debuggers, like "valgrind" or "gdb --args".
evan@chromium.org65dcdf92010-08-05 03:24:38 +0900209 void PrependWrapper(const StringType& wrapper);
agl@chromium.orgc60ca402009-02-10 09:52:14 +0900210
msw@chromium.org7b2af612011-03-01 11:28:42 +0900211#if defined(OS_WIN)
212 // Initialize by parsing the given command line string.
213 // The program name is assumed to be the first item in the string.
thestigfb1da502016-07-15 11:50:16 +0900214 void ParseFromString(const string16& command_line);
msw@chromium.org7b2af612011-03-01 11:28:42 +0900215#endif
evan@chromium.org0b90a5c2010-07-30 08:02:34 +0900216
initial.commit3f4a7322008-07-27 06:49:38 +0900217 private:
msw@chromium.org53fbac12011-05-14 10:10:24 +0900218 // Disallow default constructor; a program name must be explicitly specified.
msw@chromium.org96b695b2011-03-02 05:47:58 +0900219 CommandLine();
msw@chromium.org53fbac12011-05-14 10:10:24 +0900220 // Allow the copy constructor. A common pattern is to copy of the current
221 // process's command line and then add some flags to it. For example:
222 // CommandLine cl(*CommandLine::ForCurrentProcess());
223 // cl.AppendSwitch(...);
sky@google.com87bdd752009-01-30 09:40:43 +0900224
mgiuca90d74492014-10-01 18:24:51 +0900225 // Internal version of GetCommandLineString. If |quote_placeholders| is true,
226 // also quotes parts with '%' in them.
227 StringType GetCommandLineStringInternal(bool quote_placeholders) const;
228
229 // Internal version of GetArgumentsString. If |quote_placeholders| is true,
230 // also quotes parts with '%' in them.
231 StringType GetArgumentsStringInternal(bool quote_placeholders) const;
232
jackhoua5995af2015-05-21 13:48:00 +0900233 // Reconstruct |switches_by_stringpiece| to be a mirror of |switches|.
234 // |switches_by_stringpiece| only contains pointers to objects owned by
235 // |switches|.
236 void ResetStringPieces();
237
msw@chromium.org7b2af612011-03-01 11:28:42 +0900238 // The singleton CommandLine representing the current process's command line.
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900239 static CommandLine* current_process_commandline_;
initial.commit3f4a7322008-07-27 06:49:38 +0900240
msw@chromium.org53fbac12011-05-14 10:10:24 +0900241 // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* }
msw@chromium.org7b2af612011-03-01 11:28:42 +0900242 StringVector argv_;
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900243
msw@chromium.org7b2af612011-03-01 11:28:42 +0900244 // Parsed-out switch keys and values.
kinuko@chromium.org16013dc2010-05-24 19:39:59 +0900245 SwitchMap switches_;
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900246
jackhoua5995af2015-05-21 13:48:00 +0900247 // A mirror of |switches_| with only references to the actual strings.
248 // The StringPiece internally holds a pointer to a key in |switches_| while
249 // the mapped_type points to a value in |switches_|.
250 // Used for allocation-free lookups.
251 StringPieceSwitchMap switches_by_stringpiece_;
252
msw@chromium.org53fbac12011-05-14 10:10:24 +0900253 // The index after the program and switches, any arguments start here.
254 size_t begin_args_;
initial.commit3f4a7322008-07-27 06:49:38 +0900255};
256
brettw@chromium.org37273892014-03-18 08:07:15 +0900257} // namespace base
258
deanm@chromium.orga638cc92008-10-06 19:25:35 +0900259#endif // BASE_COMMAND_LINE_H_