[email protected] | 72e2e242 | 2012-02-27 18:38:12 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
[email protected] | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 4 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 5 | // This class works with command lines: building and parsing. |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 6 | // 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] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 11 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 12 | // 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.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 14 | |
[email protected] | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 15 | #ifndef BASE_COMMAND_LINE_H_ |
| 16 | #define BASE_COMMAND_LINE_H_ |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 17 | |
[email protected] | 2edc286 | 2011-04-04 18:04:37 | [diff] [blame] | 18 | #include <stddef.h> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 19 | #include <map> |
| 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 23 | #include "base/base_export.h" |
mgiuca | c9e499e | 2014-10-01 07:54:57 | [diff] [blame] | 24 | #include "base/strings/string16.h" |
[email protected] | 74e9fa2 | 2010-12-29 21:06:43 | [diff] [blame] | 25 | #include "build/build_config.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 26 | |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 27 | namespace base { |
[email protected] | 2f3b1cc | 2014-03-17 23:07:15 | [diff] [blame] | 28 | |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 29 | class FilePath; |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 30 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 31 | class BASE_EXPORT CommandLine { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 32 | public: |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 33 | #if defined(OS_WIN) |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 34 | // The native command line string type. |
mgiuca | c9e499e | 2014-10-01 07:54:57 | [diff] [blame] | 35 | typedef base::string16 StringType; |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 36 | #elif defined(OS_POSIX) |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 37 | typedef std::string StringType; |
| 38 | #endif |
| 39 | |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 40 | typedef StringType::value_type CharType; |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 41 | typedef std::vector<StringType> StringVector; |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 42 | typedef std::map<std::string, StringType> SwitchMap; |
| 43 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 44 | // A constructor for CommandLines that only carry switches and arguments. |
[email protected] | 947446b | 2010-10-21 03:36:31 | [diff] [blame] | 45 | enum NoProgram { NO_PROGRAM }; |
| 46 | explicit CommandLine(NoProgram no_program); |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 47 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 48 | // Construct a new command line with |program| as argv[0]. |
[email protected] | 2f3b1cc | 2014-03-17 23:07:15 | [diff] [blame] | 49 | explicit CommandLine(const FilePath& program); |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 50 | |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 51 | // Construct a new command line from an argument list. |
| 52 | CommandLine(int argc, const CharType* const* argv); |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 53 | explicit CommandLine(const StringVector& argv); |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 54 | |
[email protected] | acbeb3d | 2011-03-01 20:47:58 | [diff] [blame] | 55 | ~CommandLine(); |
| 56 | |
[email protected] | bf98a0e1 | 2013-09-25 23:36:00 | [diff] [blame] | 57 | #if defined(OS_WIN) |
| 58 | // By default this class will treat command-line arguments beginning with |
| 59 | // slashes as switches on Windows, but not other platforms. |
| 60 | // |
| 61 | // If this behavior is inappropriate for your application, you can call this |
| 62 | // function BEFORE initializing the current process' global command line |
| 63 | // object and the behavior will be the same as Posix systems (only hyphens |
| 64 | // begin switches, everything else will be an arg). |
| 65 | static void set_slash_is_not_a_switch(); |
| 66 | #endif |
| 67 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 68 | // Initialize the current process CommandLine singleton. On Windows, ignores |
| 69 | // its arguments (we instead parse GetCommandLineW() directly) because we |
| 70 | // don't trust the CRT's parsing of the command line, but it still must be |
[email protected] | 72e2e242 | 2012-02-27 18:38:12 | [diff] [blame] | 71 | // called to set up the command line. Returns false if initialization has |
| 72 | // already occurred, and true otherwise. Only the caller receiving a 'true' |
| 73 | // return value should take responsibility for calling Reset. |
| 74 | static bool Init(int argc, const char* const* argv); |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 75 | |
[email protected] | a2318cda | 2009-02-25 21:13:53 | [diff] [blame] | 76 | // Destroys the current process CommandLine singleton. This is necessary if |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 77 | // you want to reset the base library to its initial state (for example, in an |
[email protected] | a2318cda | 2009-02-25 21:13:53 | [diff] [blame] | 78 | // outer library that needs to be able to terminate, and be re-initialized). |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 79 | // If Init is called only once, as in main(), Reset() is not necessary. |
[email protected] | 02fb75ab | 2009-10-12 16:11:40 | [diff] [blame] | 80 | static void Reset(); |
[email protected] | a2318cda | 2009-02-25 21:13:53 | [diff] [blame] | 81 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 82 | // Get the singleton CommandLine representing the current process's |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 83 | // command line. Note: returned value is mutable, but not thread safe; |
[email protected] | 0189bbd | 2009-10-12 22:50:39 | [diff] [blame] | 84 | // only mutate if you know what you're doing! |
[email protected] | dcd869c | 2010-08-30 20:15:25 | [diff] [blame] | 85 | static CommandLine* ForCurrentProcess(); |
[email protected] | 1a48f31 | 2008-08-12 01:14:37 | [diff] [blame] | 86 | |
[email protected] | 2bf64a9 | 2013-07-11 23:10:40 | [diff] [blame] | 87 | // Returns true if the CommandLine has been initialized for the given process. |
| 88 | static bool InitializedForCurrentProcess(); |
| 89 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 90 | #if defined(OS_WIN) |
mgiuca | c9e499e | 2014-10-01 07:54:57 | [diff] [blame] | 91 | static CommandLine FromString(const base::string16& command_line); |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 92 | #endif |
| 93 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 94 | // Initialize from an argv vector. |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 95 | void InitFromArgv(int argc, const CharType* const* argv); |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 96 | void InitFromArgv(const StringVector& argv); |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 97 | |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 98 | // Constructs and returns the represented command line string. |
[email protected] | 45f982e | 2012-10-29 21:31:31 | [diff] [blame] | 99 | // CAUTION! This should be avoided on POSIX because quoting behavior is |
| 100 | // unclear. |
mgiuca | c974d510 | 2014-10-01 09:24:51 | [diff] [blame^] | 101 | StringType GetCommandLineString() const { |
| 102 | return GetCommandLineStringInternal(false); |
| 103 | } |
| 104 | |
| 105 | #if defined(OS_WIN) |
| 106 | // Constructs and returns the represented command line string. Assumes the |
| 107 | // command line contains placeholders (eg, %1) and quotes any program or |
| 108 | // argument with a '%' in it. This should be avoided unless the placeholder is |
| 109 | // required by an external interface (eg, the Windows registry), because it is |
| 110 | // not generally safe to replace it with an arbitrary string. If possible, |
| 111 | // placeholders should be replaced *before* converting the command line to a |
| 112 | // string. |
| 113 | StringType GetCommandLineStringWithPlaceholders() const { |
| 114 | return GetCommandLineStringInternal(true); |
| 115 | } |
| 116 | #endif |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 117 | |
[email protected] | 45f982e | 2012-10-29 21:31:31 | [diff] [blame] | 118 | // Constructs and returns the represented arguments string. |
| 119 | // CAUTION! This should be avoided on POSIX because quoting behavior is |
| 120 | // unclear. |
mgiuca | c974d510 | 2014-10-01 09:24:51 | [diff] [blame^] | 121 | StringType GetArgumentsString() const { |
| 122 | return GetArgumentsStringInternal(false); |
| 123 | } |
| 124 | |
| 125 | #if defined(OS_WIN) |
| 126 | // Constructs and returns the represented arguments string. Assumes the |
| 127 | // command line contains placeholders (eg, %1) and quotes any argument with a |
| 128 | // '%' in it. This should be avoided unless the placeholder is required by an |
| 129 | // external interface (eg, the Windows registry), because it is not generally |
| 130 | // safe to replace it with an arbitrary string. If possible, placeholders |
| 131 | // should be replaced *before* converting the arguments to a string. |
| 132 | StringType GetArgumentsStringWithPlaceholders() const { |
| 133 | return GetArgumentsStringInternal(true); |
| 134 | } |
| 135 | #endif |
[email protected] | 45f982e | 2012-10-29 21:31:31 | [diff] [blame] | 136 | |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 137 | // Returns the original command line string as a vector of strings. |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 138 | const StringVector& argv() const { return argv_; } |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 139 | |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 140 | // Get and Set the program part of the command line string (the first item). |
[email protected] | 2f3b1cc | 2014-03-17 23:07:15 | [diff] [blame] | 141 | FilePath GetProgram() const; |
| 142 | void SetProgram(const FilePath& program); |
[email protected] | 0189bbd | 2009-10-12 22:50:39 | [diff] [blame] | 143 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 144 | // Returns true if this command line contains the given switch. |
| 145 | // (Switch names are case-insensitive). |
| 146 | bool HasSwitch(const std::string& switch_string) const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 147 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 148 | // Returns the value associated with the given switch. If the switch has no |
| 149 | // value or isn't present, this method returns the empty string. |
| 150 | std::string GetSwitchValueASCII(const std::string& switch_string) const; |
[email protected] | 2f3b1cc | 2014-03-17 23:07:15 | [diff] [blame] | 151 | FilePath GetSwitchValuePath(const std::string& switch_string) const; |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 152 | StringType GetSwitchValueNative(const std::string& switch_string) const; |
| 153 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 154 | // Get a copy of all switches, along with their values. |
| 155 | const SwitchMap& GetSwitches() const { return switches_; } |
| 156 | |
| 157 | // Append a switch [with optional value] to the command line. |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 158 | // Note: Switches will precede arguments regardless of appending order. |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 159 | void AppendSwitch(const std::string& switch_string); |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 160 | void AppendSwitchPath(const std::string& switch_string, |
[email protected] | 2f3b1cc | 2014-03-17 23:07:15 | [diff] [blame] | 161 | const FilePath& path); |
[email protected] | 4f08c83f | 2010-07-29 23:02:34 | [diff] [blame] | 162 | void AppendSwitchNative(const std::string& switch_string, |
| 163 | const StringType& value); |
[email protected] | d420c31e | 2010-07-30 02:14:22 | [diff] [blame] | 164 | void AppendSwitchASCII(const std::string& switch_string, |
| 165 | const std::string& value); |
[email protected] | 4f08c83f | 2010-07-29 23:02:34 | [diff] [blame] | 166 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 167 | // Copy a set of switches (and any values) from another command line. |
| 168 | // Commonly used when launching a subprocess. |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 169 | void CopySwitchesFrom(const CommandLine& source, |
| 170 | const char* const switches[], |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 171 | size_t count); |
| 172 | |
| 173 | // Get the remaining arguments to the command. |
[email protected] | 75f1c78 | 2011-07-13 23:41:22 | [diff] [blame] | 174 | StringVector GetArgs() const; |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 175 | |
| 176 | // Append an argument to the command line. Note that the argument is quoted |
| 177 | // properly such that it is interpreted as one argument to the target command. |
| 178 | // AppendArg is primarily for ASCII; non-ASCII input is interpreted as UTF-8. |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 179 | // Note: Switches will precede arguments regardless of appending order. |
[email protected] | 0445eb4 | 2010-08-13 22:10:30 | [diff] [blame] | 180 | void AppendArg(const std::string& value); |
[email protected] | 2f3b1cc | 2014-03-17 23:07:15 | [diff] [blame] | 181 | void AppendArgPath(const FilePath& value); |
[email protected] | 0445eb4 | 2010-08-13 22:10:30 | [diff] [blame] | 182 | void AppendArgNative(const StringType& value); |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 183 | |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 184 | // Append the switches and arguments from another command line to this one. |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 185 | // If |include_program| is true, include |other|'s program as well. |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 186 | void AppendArguments(const CommandLine& other, bool include_program); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 187 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 188 | // Insert a command before the current command. |
| 189 | // Common for debuggers, like "valgrind" or "gdb --args". |
[email protected] | 13081fc | 2010-08-04 18:24:38 | [diff] [blame] | 190 | void PrependWrapper(const StringType& wrapper); |
[email protected] | 052f1d48 | 2009-02-10 00:52:14 | [diff] [blame] | 191 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 192 | #if defined(OS_WIN) |
| 193 | // Initialize by parsing the given command line string. |
| 194 | // The program name is assumed to be the first item in the string. |
mgiuca | c9e499e | 2014-10-01 07:54:57 | [diff] [blame] | 195 | void ParseFromString(const base::string16& command_line); |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 196 | #endif |
[email protected] | 4f08c83f | 2010-07-29 23:02:34 | [diff] [blame] | 197 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 198 | private: |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 199 | // Disallow default constructor; a program name must be explicitly specified. |
[email protected] | acbeb3d | 2011-03-01 20:47:58 | [diff] [blame] | 200 | CommandLine(); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 201 | // Allow the copy constructor. A common pattern is to copy of the current |
| 202 | // process's command line and then add some flags to it. For example: |
| 203 | // CommandLine cl(*CommandLine::ForCurrentProcess()); |
| 204 | // cl.AppendSwitch(...); |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 205 | |
mgiuca | c974d510 | 2014-10-01 09:24:51 | [diff] [blame^] | 206 | // Internal version of GetCommandLineString. If |quote_placeholders| is true, |
| 207 | // also quotes parts with '%' in them. |
| 208 | StringType GetCommandLineStringInternal(bool quote_placeholders) const; |
| 209 | |
| 210 | // Internal version of GetArgumentsString. If |quote_placeholders| is true, |
| 211 | // also quotes parts with '%' in them. |
| 212 | StringType GetArgumentsStringInternal(bool quote_placeholders) const; |
| 213 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 214 | // The singleton CommandLine representing the current process's command line. |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 215 | static CommandLine* current_process_commandline_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 216 | |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 217 | // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* } |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 218 | StringVector argv_; |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 219 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 220 | // Parsed-out switch keys and values. |
[email protected] | f6c3483 | 2010-05-24 10:39:59 | [diff] [blame] | 221 | SwitchMap switches_; |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 222 | |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 223 | // The index after the program and switches, any arguments start here. |
| 224 | size_t begin_args_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 225 | }; |
| 226 | |
[email protected] | 2f3b1cc | 2014-03-17 23:07:15 | [diff] [blame] | 227 | } // namespace base |
| 228 | |
| 229 | // TODO(brettw) remove once all callers specify the namespace properly. |
| 230 | using base::CommandLine; |
| 231 | |
[email protected] | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 232 | #endif // BASE_COMMAND_LINE_H_ |