blob: 31b22d6b9a9008330a874375e18b1da5dd21836c [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>;
Jeremy Roman58ac2952017-11-01 04:25:38 +090043 using SwitchMap = std::map<std::string, StringType, std::less<>>;
erg@google.comd5fffd42011-01-08 03:06:45 +090044
msw@chromium.org7b2af612011-03-01 11:28:42 +090045 // A constructor for CommandLines that only carry switches and arguments.
mattm@chromium.orgaf4c7a02010-10-21 12:36:31 +090046 enum NoProgram { NO_PROGRAM };
47 explicit CommandLine(NoProgram no_program);
erg@google.comd5fffd42011-01-08 03:06:45 +090048
msw@chromium.org7b2af612011-03-01 11:28:42 +090049 // Construct a new command line with |program| as argv[0].
brettw@chromium.org37273892014-03-18 08:07:15 +090050 explicit CommandLine(const FilePath& program);
erg@google.comd5fffd42011-01-08 03:06:45 +090051
msw@chromium.org53fbac12011-05-14 10:10:24 +090052 // Construct a new command line from an argument list.
53 CommandLine(int argc, const CharType* const* argv);
msw@chromium.org7b2af612011-03-01 11:28:42 +090054 explicit CommandLine(const StringVector& argv);
erg@google.comd5fffd42011-01-08 03:06:45 +090055
jackhoua5995af2015-05-21 13:48:00 +090056 // 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.org96b695b2011-03-02 05:47:58 +090060 ~CommandLine();
61
brettw@chromium.org40b2d582013-09-26 08:36:00 +090062#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();
anantad52b7112016-06-23 06:40:31 +090071
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.org40b2d582013-09-26 08:36:00 +090078#endif
79
msw@chromium.org7b2af612011-03-01 11:28:42 +090080 // 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.org506c72b2012-02-28 03:38:12 +090083 // 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.org4bbc6132009-01-21 10:00:22 +090087
evan@chromium.orgfee6d862009-02-26 06:13:53 +090088 // Destroys the current process CommandLine singleton. This is necessary if
msw@chromium.org7b2af612011-03-01 11:28:42 +090089 // you want to reset the base library to its initial state (for example, in an
evan@chromium.orgfee6d862009-02-26 06:13:53 +090090 // outer library that needs to be able to terminate, and be re-initialized).
msw@chromium.org7b2af612011-03-01 11:28:42 +090091 // If Init is called only once, as in main(), Reset() is not necessary.
thestigfb1da502016-07-15 11:50:16 +090092 // Do not call this in tests. Use base::test::ScopedCommandLine instead.
evan@chromium.orgb6e7c2b2009-10-13 01:11:40 +090093 static void Reset();
evan@chromium.orgfee6d862009-02-26 06:13:53 +090094
evan@chromium.org4bbc6132009-01-21 10:00:22 +090095 // Get the singleton CommandLine representing the current process's
msw@chromium.org7b2af612011-03-01 11:28:42 +090096 // command line. Note: returned value is mutable, but not thread safe;
evan@chromium.org757621d2009-10-13 07:50:39 +090097 // only mutate if you know what you're doing!
erg@google.comcb8b2cb2010-08-31 05:15:25 +090098 static CommandLine* ForCurrentProcess();
evanm@google.com638e9fb2008-08-12 10:14:37 +090099
vollick@chromium.org4ff9adc2013-07-12 08:10:40 +0900100 // Returns true if the CommandLine has been initialized for the given process.
101 static bool InitializedForCurrentProcess();
102
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900103#if defined(OS_WIN)
thestigfb1da502016-07-15 11:50:16 +0900104 static CommandLine FromString(const string16& command_line);
msw@chromium.org7b2af612011-03-01 11:28:42 +0900105#endif
106
msw@chromium.org7b2af612011-03-01 11:28:42 +0900107 // Initialize from an argv vector.
msw@chromium.org53fbac12011-05-14 10:10:24 +0900108 void InitFromArgv(int argc, const CharType* const* argv);
msw@chromium.org7b2af612011-03-01 11:28:42 +0900109 void InitFromArgv(const StringVector& argv);
msw@chromium.org7b2af612011-03-01 11:28:42 +0900110
msw@chromium.org53fbac12011-05-14 10:10:24 +0900111 // Constructs and returns the represented command line string.
gab@chromium.orge399ff42012-10-30 06:31:31 +0900112 // CAUTION! This should be avoided on POSIX because quoting behavior is
113 // unclear.
mgiuca90d74492014-10-01 18:24:51 +0900114 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.org7b2af612011-03-01 11:28:42 +0900130
gab@chromium.orge399ff42012-10-30 06:31:31 +0900131 // Constructs and returns the represented arguments string.
132 // CAUTION! This should be avoided on POSIX because quoting behavior is
133 // unclear.
mgiuca90d74492014-10-01 18:24:51 +0900134 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.orge399ff42012-10-30 06:31:31 +0900149
estade@chromium.org92c59f22008-10-16 06:59:08 +0900150 // Returns the original command line string as a vector of strings.
msw@chromium.org7b2af612011-03-01 11:28:42 +0900151 const StringVector& argv() const { return argv_; }
estade@chromium.org92c59f22008-10-16 06:59:08 +0900152
msw@chromium.org53fbac12011-05-14 10:10:24 +0900153 // Get and Set the program part of the command line string (the first item).
brettw@chromium.org37273892014-03-18 08:07:15 +0900154 FilePath GetProgram() const;
155 void SetProgram(const FilePath& program);
evan@chromium.org757621d2009-10-13 07:50:39 +0900156
msw@chromium.org7b2af612011-03-01 11:28:42 +0900157 // Returns true if this command line contains the given switch.
jackhoua5995af2015-05-21 13:48:00 +0900158 // 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.
thestigfb1da502016-07-15 11:50:16 +0900162 bool HasSwitch(const StringPiece& switch_string) const;
tapted5762d502015-03-30 12:57:10 +0900163 bool HasSwitch(const char switch_constant[]) const;
initial.commit3f4a7322008-07-27 06:49:38 +0900164
msw@chromium.org7b2af612011-03-01 11:28:42 +0900165 // 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.
jackhoua5995af2015-05-21 13:48:00 +0900167 // Switch names must be lowercase.
thestigfb1da502016-07-15 11:50:16 +0900168 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.org7b2af612011-03-01 11:28:42 +0900171
msw@chromium.org7b2af612011-03-01 11:28:42 +0900172 // 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.org53fbac12011-05-14 10:10:24 +0900176 // Note: Switches will precede arguments regardless of appending order.
msw@chromium.org7b2af612011-03-01 11:28:42 +0900177 void AppendSwitch(const std::string& switch_string);
brettw@chromium.orga7086942013-02-02 14:12:33 +0900178 void AppendSwitchPath(const std::string& switch_string,
brettw@chromium.org37273892014-03-18 08:07:15 +0900179 const FilePath& path);
evan@chromium.org0b90a5c2010-07-30 08:02:34 +0900180 void AppendSwitchNative(const std::string& switch_string,
181 const StringType& value);
evan@chromium.org3f985142010-07-30 11:14:22 +0900182 void AppendSwitchASCII(const std::string& switch_string,
183 const std::string& value);
evan@chromium.org0b90a5c2010-07-30 08:02:34 +0900184
msw@chromium.org7b2af612011-03-01 11:28:42 +0900185 // Copy a set of switches (and any values) from another command line.
186 // Commonly used when launching a subprocess.
msw@chromium.org53fbac12011-05-14 10:10:24 +0900187 void CopySwitchesFrom(const CommandLine& source,
188 const char* const switches[],
msw@chromium.org7b2af612011-03-01 11:28:42 +0900189 size_t count);
190
191 // Get the remaining arguments to the command.
msw@chromium.orga25adf42011-07-14 08:41:22 +0900192 StringVector GetArgs() const;
msw@chromium.org7b2af612011-03-01 11:28:42 +0900193
194 // Append an argument to the command line. Note that the argument is quoted
195 // properly such that it is interpreted as one argument to the target command.
196 // AppendArg is primarily for ASCII; non-ASCII input is interpreted as UTF-8.
msw@chromium.org53fbac12011-05-14 10:10:24 +0900197 // Note: Switches will precede arguments regardless of appending order.
evan@chromium.org580ab7f2010-08-14 07:10:30 +0900198 void AppendArg(const std::string& value);
brettw@chromium.org37273892014-03-18 08:07:15 +0900199 void AppendArgPath(const FilePath& value);
evan@chromium.org580ab7f2010-08-14 07:10:30 +0900200 void AppendArgNative(const StringType& value);
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900201
msw@chromium.org53fbac12011-05-14 10:10:24 +0900202 // Append the switches and arguments from another command line to this one.
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900203 // If |include_program| is true, include |other|'s program as well.
msw@chromium.org53fbac12011-05-14 10:10:24 +0900204 void AppendArguments(const CommandLine& other, bool include_program);
initial.commit3f4a7322008-07-27 06:49:38 +0900205
msw@chromium.org7b2af612011-03-01 11:28:42 +0900206 // Insert a command before the current command.
207 // Common for debuggers, like "valgrind" or "gdb --args".
evan@chromium.org65dcdf92010-08-05 03:24:38 +0900208 void PrependWrapper(const StringType& wrapper);
agl@chromium.orgc60ca402009-02-10 09:52:14 +0900209
msw@chromium.org7b2af612011-03-01 11:28:42 +0900210#if defined(OS_WIN)
211 // Initialize by parsing the given command line string.
212 // The program name is assumed to be the first item in the string.
thestigfb1da502016-07-15 11:50:16 +0900213 void ParseFromString(const string16& command_line);
msw@chromium.org7b2af612011-03-01 11:28:42 +0900214#endif
evan@chromium.org0b90a5c2010-07-30 08:02:34 +0900215
initial.commit3f4a7322008-07-27 06:49:38 +0900216 private:
msw@chromium.org53fbac12011-05-14 10:10:24 +0900217 // Disallow default constructor; a program name must be explicitly specified.
msw@chromium.org96b695b2011-03-02 05:47:58 +0900218 CommandLine();
msw@chromium.org53fbac12011-05-14 10:10:24 +0900219 // Allow the copy constructor. A common pattern is to copy of the current
220 // process's command line and then add some flags to it. For example:
221 // CommandLine cl(*CommandLine::ForCurrentProcess());
222 // cl.AppendSwitch(...);
sky@google.com87bdd752009-01-30 09:40:43 +0900223
mgiuca90d74492014-10-01 18:24:51 +0900224 // Internal version of GetCommandLineString. If |quote_placeholders| is true,
225 // also quotes parts with '%' in them.
226 StringType GetCommandLineStringInternal(bool quote_placeholders) const;
227
228 // Internal version of GetArgumentsString. If |quote_placeholders| is true,
229 // also quotes parts with '%' in them.
230 StringType GetArgumentsStringInternal(bool quote_placeholders) const;
231
msw@chromium.org7b2af612011-03-01 11:28:42 +0900232 // The singleton CommandLine representing the current process's command line.
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900233 static CommandLine* current_process_commandline_;
initial.commit3f4a7322008-07-27 06:49:38 +0900234
msw@chromium.org53fbac12011-05-14 10:10:24 +0900235 // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* }
msw@chromium.org7b2af612011-03-01 11:28:42 +0900236 StringVector argv_;
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900237
msw@chromium.org7b2af612011-03-01 11:28:42 +0900238 // Parsed-out switch keys and values.
kinuko@chromium.org16013dc2010-05-24 19:39:59 +0900239 SwitchMap switches_;
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900240
msw@chromium.org53fbac12011-05-14 10:10:24 +0900241 // The index after the program and switches, any arguments start here.
242 size_t begin_args_;
initial.commit3f4a7322008-07-27 06:49:38 +0900243};
244
brettw@chromium.org37273892014-03-18 08:07:15 +0900245} // namespace base
246
deanm@chromium.orga638cc92008-10-06 19:25:35 +0900247#endif // BASE_COMMAND_LINE_H_