blob: 61f91f2a878170c9308a4c9bc8ea3587fa7ef50a [file] [log] [blame]
erikwright@chromium.org72e2e2422012-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.
deanm@chromium.org02c87962008-10-06 10:25:354
evan@chromium.orgbb975362009-01-21 01:00:225// This class works with command lines: building and parsing.
msw@chromium.orga40ca4302011-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.
evan@chromium.orgbb975362009-01-21 01:00:2211
msw@chromium.org06cc083a2011-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
deanm@chromium.org02c87962008-10-06 10:25:3515#ifndef BASE_COMMAND_LINE_H_
16#define BASE_COMMAND_LINE_H_
initial.commitd7cae122008-07-26 21:49:3817
jhawkins@chromium.org2edc2862011-04-04 18:04:3718#include <stddef.h>
initial.commitd7cae122008-07-26 21:49:3819#include <map>
20#include <string>
21#include <vector>
22
darin@chromium.org0bea7252011-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"
brettw@chromium.org74e9fa22010-12-29 21:06:4326#include "build/build_config.h"
initial.commitd7cae122008-07-26 21:49:3827
brettw@chromium.orga3ef4832013-02-02 05:12:3328namespace base {
brettw@chromium.org2f3b1cc2014-03-17 23:07:1529
erg@google.com5d91c9e2010-07-28 17:25:2830class FilePath;
sky@google.comd4515eb2009-01-30 00:40:4331
darin@chromium.org0bea7252011-08-05 15:34:0032class BASE_EXPORT CommandLine {
initial.commitd7cae122008-07-26 21:49:3833 public:
erg@google.coma502bbe72011-01-07 18:06:4534#if defined(OS_WIN)
msw@chromium.org06cc083a2011-03-01 02:28:4235 // The native command line string type.
thestigfd085682016-07-15 02:50:1636 using StringType = string16;
Fabrice de Gans-Riberi306871de2018-05-16 19:38:3937#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
thestigfd085682016-07-15 02:50:1638 using StringType = std::string;
erg@google.coma502bbe72011-01-07 18:06:4539#endif
40
thestigfd085682016-07-15 02:50:1641 using CharType = StringType::value_type;
42 using StringVector = std::vector<StringType>;
Jeremy Roman863386d2017-10-31 19:25:3843 using SwitchMap = std::map<std::string, StringType, std::less<>>;
erg@google.coma502bbe72011-01-07 18:06:4544
msw@chromium.org06cc083a2011-03-01 02:28:4245 // A constructor for CommandLines that only carry switches and arguments.
mattm@chromium.org947446b2010-10-21 03:36:3146 enum NoProgram { NO_PROGRAM };
47 explicit CommandLine(NoProgram no_program);
erg@google.coma502bbe72011-01-07 18:06:4548
msw@chromium.org06cc083a2011-03-01 02:28:4249 // Construct a new command line with |program| as argv[0].
brettw@chromium.org2f3b1cc2014-03-17 23:07:1550 explicit CommandLine(const FilePath& program);
erg@google.coma502bbe72011-01-07 18:06:4551
msw@chromium.orga40ca4302011-05-14 01:10:2452 // Construct a new command line from an argument list.
53 CommandLine(int argc, const CharType* const* argv);
msw@chromium.org06cc083a2011-03-01 02:28:4254 explicit CommandLine(const StringVector& argv);
erg@google.coma502bbe72011-01-07 18:06:4555
jackhou1bd9da92015-05-21 04:48:0056 // Override copy and assign to ensure |switches_by_stringpiece_| is valid.
57 CommandLine(const CommandLine& other);
58 CommandLine& operator=(const CommandLine& other);
59
msw@chromium.orgacbeb3d2011-03-01 20:47:5860 ~CommandLine();
61
brettw@chromium.orgbf98a0e12013-09-25 23:36:0062#if defined(OS_WIN)
63 // By default this class will treat command-line arguments beginning with
64 // slashes as switches on Windows, but not other platforms.
65 //
66 // If this behavior is inappropriate for your application, you can call this
67 // function BEFORE initializing the current process' global command line
68 // object and the behavior will be the same as Posix systems (only hyphens
69 // begin switches, everything else will be an arg).
70 static void set_slash_is_not_a_switch();
anantad936bc12016-06-22 21:40:3171
72 // Normally when the CommandLine singleton is initialized it gets the command
73 // line via the GetCommandLineW API and then uses the shell32 API
74 // CommandLineToArgvW to parse the command line and convert it back to
75 // argc and argv. Tests who don't want this dependency on shell32 and need
76 // to honor the arguments passed in should use this function.
77 static void InitUsingArgvForTesting(int argc, const char* const* argv);
brettw@chromium.orgbf98a0e12013-09-25 23:36:0078#endif
79
msw@chromium.org06cc083a2011-03-01 02:28:4280 // Initialize the current process CommandLine singleton. On Windows, ignores
81 // its arguments (we instead parse GetCommandLineW() directly) because we
82 // don't trust the CRT's parsing of the command line, but it still must be
erikwright@chromium.org72e2e2422012-02-27 18:38:1283 // called to set up the command line. Returns false if initialization has
84 // already occurred, and true otherwise. Only the caller receiving a 'true'
85 // return value should take responsibility for calling Reset.
86 static bool Init(int argc, const char* const* argv);
evan@chromium.orgbb975362009-01-21 01:00:2287
evan@chromium.orga2318cda2009-02-25 21:13:5388 // Destroys the current process CommandLine singleton. This is necessary if
msw@chromium.org06cc083a2011-03-01 02:28:4289 // you want to reset the base library to its initial state (for example, in an
evan@chromium.orga2318cda2009-02-25 21:13:5390 // outer library that needs to be able to terminate, and be re-initialized).
msw@chromium.org06cc083a2011-03-01 02:28:4291 // If Init is called only once, as in main(), Reset() is not necessary.
thestigfd085682016-07-15 02:50:1692 // Do not call this in tests. Use base::test::ScopedCommandLine instead.
evan@chromium.org02fb75ab2009-10-12 16:11:4093 static void Reset();
evan@chromium.orga2318cda2009-02-25 21:13:5394
evan@chromium.orgbb975362009-01-21 01:00:2295 // Get the singleton CommandLine representing the current process's
msw@chromium.org06cc083a2011-03-01 02:28:4296 // command line. Note: returned value is mutable, but not thread safe;
evan@chromium.org0189bbd2009-10-12 22:50:3997 // only mutate if you know what you're doing!
erg@google.comdcd869c2010-08-30 20:15:2598 static CommandLine* ForCurrentProcess();
evanm@google.com1a48f312008-08-12 01:14:3799
vollick@chromium.org2bf64a92013-07-11 23:10:40100 // Returns true if the CommandLine has been initialized for the given process.
101 static bool InitializedForCurrentProcess();
102
evan@chromium.orgbb975362009-01-21 01:00:22103#if defined(OS_WIN)
thestigfd085682016-07-15 02:50:16104 static CommandLine FromString(const string16& command_line);
msw@chromium.org06cc083a2011-03-01 02:28:42105#endif
106
msw@chromium.org06cc083a2011-03-01 02:28:42107 // Initialize from an argv vector.
msw@chromium.orga40ca4302011-05-14 01:10:24108 void InitFromArgv(int argc, const CharType* const* argv);
msw@chromium.org06cc083a2011-03-01 02:28:42109 void InitFromArgv(const StringVector& argv);
msw@chromium.org06cc083a2011-03-01 02:28:42110
msw@chromium.orga40ca4302011-05-14 01:10:24111 // Constructs and returns the represented command line string.
gab@chromium.org45f982e2012-10-29 21:31:31112 // CAUTION! This should be avoided on POSIX because quoting behavior is
113 // unclear.
mgiucac974d5102014-10-01 09:24:51114 StringType GetCommandLineString() const {
115 return GetCommandLineStringInternal(false);
116 }
117
118#if defined(OS_WIN)
119 // Constructs and returns the represented command line string. Assumes the
120 // command line contains placeholders (eg, %1) and quotes any program or
121 // argument with a '%' in it. This should be avoided unless the placeholder is
122 // required by an external interface (eg, the Windows registry), because it is
123 // not generally safe to replace it with an arbitrary string. If possible,
124 // placeholders should be replaced *before* converting the command line to a
125 // string.
126 StringType GetCommandLineStringWithPlaceholders() const {
127 return GetCommandLineStringInternal(true);
128 }
129#endif
msw@chromium.org06cc083a2011-03-01 02:28:42130
gab@chromium.org45f982e2012-10-29 21:31:31131 // Constructs and returns the represented arguments string.
132 // CAUTION! This should be avoided on POSIX because quoting behavior is
133 // unclear.
mgiucac974d5102014-10-01 09:24:51134 StringType GetArgumentsString() const {
135 return GetArgumentsStringInternal(false);
136 }
137
138#if defined(OS_WIN)
139 // Constructs and returns the represented arguments string. Assumes the
140 // command line contains placeholders (eg, %1) and quotes any argument with a
141 // '%' in it. This should be avoided unless the placeholder is required by an
142 // external interface (eg, the Windows registry), because it is not generally
143 // safe to replace it with an arbitrary string. If possible, placeholders
144 // should be replaced *before* converting the arguments to a string.
145 StringType GetArgumentsStringWithPlaceholders() const {
146 return GetArgumentsStringInternal(true);
147 }
148#endif
gab@chromium.org45f982e2012-10-29 21:31:31149
estade@chromium.org10e42bf2008-10-15 21:59:08150 // Returns the original command line string as a vector of strings.
msw@chromium.org06cc083a2011-03-01 02:28:42151 const StringVector& argv() const { return argv_; }
estade@chromium.org10e42bf2008-10-15 21:59:08152
msw@chromium.orga40ca4302011-05-14 01:10:24153 // Get and Set the program part of the command line string (the first item).
brettw@chromium.org2f3b1cc2014-03-17 23:07:15154 FilePath GetProgram() const;
155 void SetProgram(const FilePath& program);
evan@chromium.org0189bbd2009-10-12 22:50:39156
msw@chromium.org06cc083a2011-03-01 02:28:42157 // Returns true if this command line contains the given switch.
jackhou1bd9da92015-05-21 04:48:00158 // Switch names must be lowercase.
159 // The second override provides an optimized version to avoid inlining codegen
160 // at every callsite to find the length of the constant and construct a
161 // StringPiece.
thestigfd085682016-07-15 02:50:16162 bool HasSwitch(const StringPiece& switch_string) const;
tapted009a1dc82015-03-30 03:57:10163 bool HasSwitch(const char switch_constant[]) const;
initial.commitd7cae122008-07-26 21:49:38164
msw@chromium.org06cc083a2011-03-01 02:28:42165 // Returns the value associated with the given switch. If the switch has no
166 // value or isn't present, this method returns the empty string.
jackhou1bd9da92015-05-21 04:48:00167 // Switch names must be lowercase.
thestigfd085682016-07-15 02:50:16168 std::string GetSwitchValueASCII(const StringPiece& switch_string) const;
169 FilePath GetSwitchValuePath(const StringPiece& switch_string) const;
170 StringType GetSwitchValueNative(const StringPiece& switch_string) const;
msw@chromium.org06cc083a2011-03-01 02:28:42171
msw@chromium.org06cc083a2011-03-01 02:28:42172 // Get a copy of all switches, along with their values.
173 const SwitchMap& GetSwitches() const { return switches_; }
174
175 // Append a switch [with optional value] to the command line.
msw@chromium.orga40ca4302011-05-14 01:10:24176 // Note: Switches will precede arguments regardless of appending order.
msw@chromium.org06cc083a2011-03-01 02:28:42177 void AppendSwitch(const std::string& switch_string);
brettw@chromium.orga3ef4832013-02-02 05:12:33178 void AppendSwitchPath(const std::string& switch_string,
brettw@chromium.org2f3b1cc2014-03-17 23:07:15179 const FilePath& path);
evan@chromium.org4f08c83f2010-07-29 23:02:34180 void AppendSwitchNative(const std::string& switch_string,
181 const StringType& value);
evan@chromium.orgd420c31e2010-07-30 02:14:22182 void AppendSwitchASCII(const std::string& switch_string,
183 const std::string& value);
evan@chromium.org4f08c83f2010-07-29 23:02:34184
Avi Drissman1aa6cb92019-01-23 15:58:38185 // Removes a switch.
186 void RemoveSwitch(const StringPiece& switch_string);
187
msw@chromium.org06cc083a2011-03-01 02:28:42188 // Copy a set of switches (and any values) from another command line.
189 // Commonly used when launching a subprocess.
msw@chromium.orga40ca4302011-05-14 01:10:24190 void CopySwitchesFrom(const CommandLine& source,
191 const char* const switches[],
msw@chromium.org06cc083a2011-03-01 02:28:42192 size_t count);
193
194 // Get the remaining arguments to the command.
msw@chromium.org75f1c782011-07-13 23:41:22195 StringVector GetArgs() const;
msw@chromium.org06cc083a2011-03-01 02:28:42196
197 // Append an argument to the command line. Note that the argument is quoted
198 // properly such that it is interpreted as one argument to the target command.
199 // AppendArg is primarily for ASCII; non-ASCII input is interpreted as UTF-8.
msw@chromium.orga40ca4302011-05-14 01:10:24200 // Note: Switches will precede arguments regardless of appending order.
evan@chromium.org0445eb42010-08-13 22:10:30201 void AppendArg(const std::string& value);
brettw@chromium.org2f3b1cc2014-03-17 23:07:15202 void AppendArgPath(const FilePath& value);
evan@chromium.org0445eb42010-08-13 22:10:30203 void AppendArgNative(const StringType& value);
evan@chromium.orgbb975362009-01-21 01:00:22204
msw@chromium.orga40ca4302011-05-14 01:10:24205 // Append the switches and arguments from another command line to this one.
evan@chromium.orgbb975362009-01-21 01:00:22206 // If |include_program| is true, include |other|'s program as well.
msw@chromium.orga40ca4302011-05-14 01:10:24207 void AppendArguments(const CommandLine& other, bool include_program);
initial.commitd7cae122008-07-26 21:49:38208
msw@chromium.org06cc083a2011-03-01 02:28:42209 // Insert a command before the current command.
Mostyn Bramley-Moored0ecd6a2017-12-06 19:13:21210 // Common for debuggers, like "gdb --args".
evan@chromium.org13081fc2010-08-04 18:24:38211 void PrependWrapper(const StringType& wrapper);
agl@chromium.org052f1d482009-02-10 00:52:14212
msw@chromium.org06cc083a2011-03-01 02:28:42213#if defined(OS_WIN)
214 // Initialize by parsing the given command line string.
215 // The program name is assumed to be the first item in the string.
thestigfd085682016-07-15 02:50:16216 void ParseFromString(const string16& command_line);
msw@chromium.org06cc083a2011-03-01 02:28:42217#endif
evan@chromium.org4f08c83f2010-07-29 23:02:34218
initial.commitd7cae122008-07-26 21:49:38219 private:
msw@chromium.orga40ca4302011-05-14 01:10:24220 // Disallow default constructor; a program name must be explicitly specified.
Chris Watkins091d6292017-12-13 04:25:58221 CommandLine() = delete;
msw@chromium.orga40ca4302011-05-14 01:10:24222 // Allow the copy constructor. A common pattern is to copy of the current
223 // process's command line and then add some flags to it. For example:
224 // CommandLine cl(*CommandLine::ForCurrentProcess());
225 // cl.AppendSwitch(...);
sky@google.comd4515eb2009-01-30 00:40:43226
mgiucac974d5102014-10-01 09:24:51227 // Internal version of GetCommandLineString. If |quote_placeholders| is true,
228 // also quotes parts with '%' in them.
229 StringType GetCommandLineStringInternal(bool quote_placeholders) const;
230
231 // Internal version of GetArgumentsString. If |quote_placeholders| is true,
232 // also quotes parts with '%' in them.
233 StringType GetArgumentsStringInternal(bool quote_placeholders) const;
234
msw@chromium.org06cc083a2011-03-01 02:28:42235 // The singleton CommandLine representing the current process's command line.
evan@chromium.orgbb975362009-01-21 01:00:22236 static CommandLine* current_process_commandline_;
initial.commitd7cae122008-07-26 21:49:38237
msw@chromium.orga40ca4302011-05-14 01:10:24238 // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* }
msw@chromium.org06cc083a2011-03-01 02:28:42239 StringVector argv_;
evan@chromium.orgbb975362009-01-21 01:00:22240
msw@chromium.org06cc083a2011-03-01 02:28:42241 // Parsed-out switch keys and values.
kinuko@chromium.orgf6c34832010-05-24 10:39:59242 SwitchMap switches_;
evan@chromium.orgbb975362009-01-21 01:00:22243
msw@chromium.orga40ca4302011-05-14 01:10:24244 // The index after the program and switches, any arguments start here.
245 size_t begin_args_;
initial.commitd7cae122008-07-26 21:49:38246};
247
brettw@chromium.org2f3b1cc2014-03-17 23:07:15248} // namespace base
249
deanm@chromium.org02c87962008-10-06 10:25:35250#endif // BASE_COMMAND_LINE_H_