Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
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". |
John Rummell | b1d5fcb | 2019-04-27 01:13:33 | [diff] [blame] | 9 | // If a switch is specified multiple times, only the last value is used. |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 10 | // An argument of "--" will terminate switch parsing during initialization, |
| 11 | // interpreting subsequent tokens as non-switch arguments, regardless of prefix. |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 12 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 13 | // There is a singleton read-only CommandLine that represents the command line |
| 14 | // that the current process was started with. It must be initialized in main(). |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 15 | |
[email protected] | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 16 | #ifndef BASE_COMMAND_LINE_H_ |
| 17 | #define BASE_COMMAND_LINE_H_ |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 18 | |
[email protected] | 2edc286 | 2011-04-04 18:04:37 | [diff] [blame] | 19 | #include <stddef.h> |
Sumaid Syed | 22f60eeb | 2021-08-26 05:16:26 | [diff] [blame] | 20 | #include <functional> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 21 | #include <map> |
Stephan Hartmann | 4185978 | 2021-10-12 18:49:23 | [diff] [blame] | 22 | #include <memory> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 26 | #include "base/base_export.h" |
Will Harris | ee9f0a2 | 2023-03-02 21:38:58 | [diff] [blame] | 27 | #include "base/debug/debugging_buildflags.h" |
jackhou | 1bd9da9 | 2015-05-21 04:48:00 | [diff] [blame] | 28 | #include "base/strings/string_piece.h" |
[email protected] | 74e9fa2 | 2010-12-29 21:06:43 | [diff] [blame] | 29 | #include "build/build_config.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 30 | |
Will Harris | ee9f0a2 | 2023-03-02 21:38:58 | [diff] [blame] | 31 | #if BUILDFLAG(ENABLE_COMMANDLINE_SEQUENCE_CHECKS) |
| 32 | #include "base/sequence_checker.h" |
| 33 | #endif // BUILDFLAG(ENABLE_COMMANDLINE_SEQUENCE_CHECKS) |
| 34 | |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 35 | namespace base { |
[email protected] | 2f3b1cc | 2014-03-17 23:07:15 | [diff] [blame] | 36 | |
Greg Guterman | fe16560d | 2021-10-07 19:58:15 | [diff] [blame] | 37 | class DuplicateSwitchHandler; |
Greg Guterman | e605119 | 2021-10-15 21:14:36 | [diff] [blame] | 38 | class FilePath; |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 39 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 40 | class BASE_EXPORT CommandLine { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 41 | public: |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 42 | #if BUILDFLAG(IS_WIN) |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 43 | // The native command line string type. |
Jan Wilken Dörrie | da77fd43 | 2019-10-24 21:40:34 | [diff] [blame] | 44 | using StringType = std::wstring; |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 45 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
thestig | fd08568 | 2016-07-15 02:50:16 | [diff] [blame] | 46 | using StringType = std::string; |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 47 | #endif |
| 48 | |
thestig | fd08568 | 2016-07-15 02:50:16 | [diff] [blame] | 49 | using CharType = StringType::value_type; |
Jan Wilken Dörrie | 8ed6fce | 2021-03-25 23:00:38 | [diff] [blame] | 50 | using StringPieceType = base::BasicStringPiece<CharType>; |
thestig | fd08568 | 2016-07-15 02:50:16 | [diff] [blame] | 51 | using StringVector = std::vector<StringType>; |
Jeremy Roman | 863386d | 2017-10-31 19:25:38 | [diff] [blame] | 52 | using SwitchMap = std::map<std::string, StringType, std::less<>>; |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 53 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 54 | // A constructor for CommandLines that only carry switches and arguments. |
[email protected] | 947446b | 2010-10-21 03:36:31 | [diff] [blame] | 55 | enum NoProgram { NO_PROGRAM }; |
| 56 | explicit CommandLine(NoProgram no_program); |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 57 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 58 | // Construct a new command line with |program| as argv[0]. |
[email protected] | 2f3b1cc | 2014-03-17 23:07:15 | [diff] [blame] | 59 | explicit CommandLine(const FilePath& program); |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 60 | |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 61 | // Construct a new command line from an argument list. |
| 62 | CommandLine(int argc, const CharType* const* argv); |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 63 | explicit CommandLine(const StringVector& argv); |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 64 | |
Will Harris | ee9f0a2 | 2023-03-02 21:38:58 | [diff] [blame] | 65 | // Allow the copy constructor. A common pattern is to copy of the current |
| 66 | // process's command line and then add some flags to it. For example: |
| 67 | // CommandLine cl(*CommandLine::ForCurrentProcess()); |
| 68 | // cl.AppendSwitch(...); |
jackhou | 1bd9da9 | 2015-05-21 04:48:00 | [diff] [blame] | 69 | CommandLine(const CommandLine& other); |
| 70 | CommandLine& operator=(const CommandLine& other); |
| 71 | |
[email protected] | acbeb3d | 2011-03-01 20:47:58 | [diff] [blame] | 72 | ~CommandLine(); |
| 73 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 74 | #if BUILDFLAG(IS_WIN) |
[email protected] | bf98a0e1 | 2013-09-25 23:36:00 | [diff] [blame] | 75 | // By default this class will treat command-line arguments beginning with |
| 76 | // slashes as switches on Windows, but not other platforms. |
| 77 | // |
| 78 | // If this behavior is inappropriate for your application, you can call this |
| 79 | // function BEFORE initializing the current process' global command line |
| 80 | // object and the behavior will be the same as Posix systems (only hyphens |
| 81 | // begin switches, everything else will be an arg). |
| 82 | static void set_slash_is_not_a_switch(); |
ananta | d936bc1 | 2016-06-22 21:40:31 | [diff] [blame] | 83 | |
| 84 | // Normally when the CommandLine singleton is initialized it gets the command |
| 85 | // line via the GetCommandLineW API and then uses the shell32 API |
| 86 | // CommandLineToArgvW to parse the command line and convert it back to |
| 87 | // argc and argv. Tests who don't want this dependency on shell32 and need |
| 88 | // to honor the arguments passed in should use this function. |
| 89 | static void InitUsingArgvForTesting(int argc, const char* const* argv); |
[email protected] | bf98a0e1 | 2013-09-25 23:36:00 | [diff] [blame] | 90 | #endif |
| 91 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 92 | // Initialize the current process CommandLine singleton. On Windows, ignores |
| 93 | // its arguments (we instead parse GetCommandLineW() directly) because we |
| 94 | // 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] | 95 | // called to set up the command line. Returns false if initialization has |
| 96 | // already occurred, and true otherwise. Only the caller receiving a 'true' |
| 97 | // return value should take responsibility for calling Reset. |
| 98 | static bool Init(int argc, const char* const* argv); |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 99 | |
[email protected] | a2318cda | 2009-02-25 21:13:53 | [diff] [blame] | 100 | // Destroys the current process CommandLine singleton. This is necessary if |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 101 | // 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] | 102 | // outer library that needs to be able to terminate, and be re-initialized). |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 103 | // If Init is called only once, as in main(), Reset() is not necessary. |
thestig | fd08568 | 2016-07-15 02:50:16 | [diff] [blame] | 104 | // Do not call this in tests. Use base::test::ScopedCommandLine instead. |
[email protected] | 02fb75ab | 2009-10-12 16:11:40 | [diff] [blame] | 105 | static void Reset(); |
[email protected] | a2318cda | 2009-02-25 21:13:53 | [diff] [blame] | 106 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 107 | // Get the singleton CommandLine representing the current process's |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 108 | // command line. Note: returned value is mutable, but not thread safe; |
[email protected] | 0189bbd | 2009-10-12 22:50:39 | [diff] [blame] | 109 | // only mutate if you know what you're doing! |
[email protected] | dcd869c | 2010-08-30 20:15:25 | [diff] [blame] | 110 | static CommandLine* ForCurrentProcess(); |
[email protected] | 1a48f31 | 2008-08-12 01:14:37 | [diff] [blame] | 111 | |
[email protected] | 2bf64a9 | 2013-07-11 23:10:40 | [diff] [blame] | 112 | // Returns true if the CommandLine has been initialized for the given process. |
| 113 | static bool InitializedForCurrentProcess(); |
| 114 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 115 | #if BUILDFLAG(IS_WIN) |
Jan Wilken Dörrie | da77fd43 | 2019-10-24 21:40:34 | [diff] [blame] | 116 | static CommandLine FromString(StringPieceType command_line); |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 117 | #endif |
| 118 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 119 | // Initialize from an argv vector. |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 120 | void InitFromArgv(int argc, const CharType* const* argv); |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 121 | void InitFromArgv(const StringVector& argv); |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 122 | |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 123 | // Constructs and returns the represented command line string. |
[email protected] | 45f982e | 2012-10-29 21:31:31 | [diff] [blame] | 124 | // CAUTION! This should be avoided on POSIX because quoting behavior is |
| 125 | // unclear. |
Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 126 | // CAUTION! If writing a command line to the Windows registry, use |
| 127 | // GetCommandLineStringForShell() instead. |
| 128 | StringType GetCommandLineString() const; |
mgiuca | c974d510 | 2014-10-01 09:24:51 | [diff] [blame] | 129 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 130 | #if BUILDFLAG(IS_WIN) |
S. Ganesh | f2c7cb73 | 2022-12-16 22:54:42 | [diff] [blame] | 131 | // Quotes and escapes `arg` if necessary so that it will be interpreted as a |
| 132 | // single command-line parameter according to the following rules in line with |
| 133 | // `::CommandLineToArgvW` and C++ `main`: |
| 134 | // * Returns `arg` unchanged if `arg` does not include any characters that may |
| 135 | // need encoding, which is spaces, tabs, backslashes, and double-quotes. |
| 136 | // * Otherwise, double-quotes `arg` and in addition: |
| 137 | // * Escapes any double-quotes in `arg` with backslashes. |
| 138 | // * Escapes backslashes in `arg` if: |
| 139 | // * `arg` ends with backslashes , or |
| 140 | // * the backslashes end in a pre-existing double quote. |
| 141 | // |
| 142 | // https://learn.microsoft.com/en-us/search/?terms=CommandLineToArgvW and |
| 143 | // http://msdn.microsoft.com/en-us/library/17w5ykft.aspx#parsing-c-command-line-arguments. |
| 144 | static std::wstring QuoteForCommandLineToArgvW(const std::wstring& arg); |
| 145 | |
Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 146 | // Returns the command-line string in the proper format for the Windows shell, |
| 147 | // ending with the argument placeholder "--single-argument %1". The single- |
| 148 | // argument switch prevents unexpected parsing of arguments from other |
| 149 | // software that cannot be trusted to escape double quotes when substituting |
Jesse McKenna | 185ceda2 | 2021-03-10 06:47:55 | [diff] [blame] | 150 | // into a placeholder (e.g., "%1" insert sequences populated by the Windows |
Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 151 | // shell). |
| 152 | // NOTE: this must be used to generate the command-line string for the shell |
| 153 | // even if this command line was parsed from a string with the proper syntax, |
| 154 | // because the --single-argument switch is not preserved during parsing. |
| 155 | StringType GetCommandLineStringForShell() const; |
Jesse McKenna | 185ceda2 | 2021-03-10 06:47:55 | [diff] [blame] | 156 | |
| 157 | // Returns the represented command-line string. Allows the use of unsafe |
| 158 | // Windows insert sequences like "%1". Only use this method if |
| 159 | // GetCommandLineStringForShell() is not adequate AND the processor inserting |
| 160 | // the arguments is known to do so securely (i.e., is not the Windows shell). |
| 161 | // If in doubt, do not use. |
| 162 | StringType GetCommandLineStringWithUnsafeInsertSequences() const; |
mgiuca | c974d510 | 2014-10-01 09:24:51 | [diff] [blame] | 163 | #endif |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 164 | |
[email protected] | 45f982e | 2012-10-29 21:31:31 | [diff] [blame] | 165 | // Constructs and returns the represented arguments string. |
| 166 | // CAUTION! This should be avoided on POSIX because quoting behavior is |
| 167 | // unclear. |
Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 168 | StringType GetArgumentsString() const; |
[email protected] | 45f982e | 2012-10-29 21:31:31 | [diff] [blame] | 169 | |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 170 | // Returns the original command line string as a vector of strings. |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 171 | const StringVector& argv() const { return argv_; } |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 172 | |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 173 | // 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] | 174 | FilePath GetProgram() const; |
| 175 | void SetProgram(const FilePath& program); |
[email protected] | 0189bbd | 2009-10-12 22:50:39 | [diff] [blame] | 176 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 177 | // Returns true if this command line contains the given switch. |
jackhou | 1bd9da9 | 2015-05-21 04:48:00 | [diff] [blame] | 178 | // Switch names must be lowercase. |
| 179 | // The second override provides an optimized version to avoid inlining codegen |
| 180 | // at every callsite to find the length of the constant and construct a |
| 181 | // StringPiece. |
Wez | 65018d0 | 2021-05-20 15:47:45 | [diff] [blame] | 182 | bool HasSwitch(StringPiece switch_string) const; |
tapted | 009a1dc8 | 2015-03-30 03:57:10 | [diff] [blame] | 183 | bool HasSwitch(const char switch_constant[]) const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 184 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 185 | // Returns the value associated with the given switch. If the switch has no |
| 186 | // value or isn't present, this method returns the empty string. |
jackhou | 1bd9da9 | 2015-05-21 04:48:00 | [diff] [blame] | 187 | // Switch names must be lowercase. |
Wez | 65018d0 | 2021-05-20 15:47:45 | [diff] [blame] | 188 | std::string GetSwitchValueASCII(StringPiece switch_string) const; |
| 189 | FilePath GetSwitchValuePath(StringPiece switch_string) const; |
| 190 | StringType GetSwitchValueNative(StringPiece switch_string) const; |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 191 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 192 | // Get a copy of all switches, along with their values. |
| 193 | const SwitchMap& GetSwitches() const { return switches_; } |
| 194 | |
| 195 | // Append a switch [with optional value] to the command line. |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 196 | // Note: Switches will precede arguments regardless of appending order. |
Wez | 65018d0 | 2021-05-20 15:47:45 | [diff] [blame] | 197 | void AppendSwitch(StringPiece switch_string); |
| 198 | void AppendSwitchPath(StringPiece switch_string, const FilePath& path); |
| 199 | void AppendSwitchNative(StringPiece switch_string, StringPieceType value); |
| 200 | void AppendSwitchASCII(StringPiece switch_string, StringPiece value); |
[email protected] | 4f08c83f | 2010-07-29 23:02:34 | [diff] [blame] | 201 | |
Pavol Marko | bf16b81 | 2019-06-14 00:53:12 | [diff] [blame] | 202 | // Removes the switch that matches |switch_key_without_prefix|, regardless of |
| 203 | // prefix and value. If no such switch is present, this has no effect. |
| 204 | void RemoveSwitch(const base::StringPiece switch_key_without_prefix); |
Avi Drissman | 1aa6cb9 | 2019-01-23 15:58:38 | [diff] [blame] | 205 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 206 | // Copy a set of switches (and any values) from another command line. |
| 207 | // Commonly used when launching a subprocess. |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 208 | void CopySwitchesFrom(const CommandLine& source, |
| 209 | const char* const switches[], |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 210 | size_t count); |
| 211 | |
| 212 | // Get the remaining arguments to the command. |
[email protected] | 75f1c78 | 2011-07-13 23:41:22 | [diff] [blame] | 213 | StringVector GetArgs() const; |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 214 | |
| 215 | // Append an argument to the command line. Note that the argument is quoted |
| 216 | // properly such that it is interpreted as one argument to the target command. |
| 217 | // AppendArg is primarily for ASCII; non-ASCII input is interpreted as UTF-8. |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 218 | // Note: Switches will precede arguments regardless of appending order. |
Wez | 65018d0 | 2021-05-20 15:47:45 | [diff] [blame] | 219 | void AppendArg(StringPiece value); |
[email protected] | 2f3b1cc | 2014-03-17 23:07:15 | [diff] [blame] | 220 | void AppendArgPath(const FilePath& value); |
Wez | 65018d0 | 2021-05-20 15:47:45 | [diff] [blame] | 221 | void AppendArgNative(StringPieceType value); |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 222 | |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 223 | // Append the switches and arguments from another command line to this one. |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 224 | // If |include_program| is true, include |other|'s program as well. |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 225 | void AppendArguments(const CommandLine& other, bool include_program); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 226 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 227 | // Insert a command before the current command. |
Mostyn Bramley-Moore | d0ecd6a | 2017-12-06 19:13:21 | [diff] [blame] | 228 | // Common for debuggers, like "gdb --args". |
Wez | 65018d0 | 2021-05-20 15:47:45 | [diff] [blame] | 229 | void PrependWrapper(StringPieceType wrapper); |
[email protected] | 052f1d48 | 2009-02-10 00:52:14 | [diff] [blame] | 230 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 231 | #if BUILDFLAG(IS_WIN) |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 232 | // Initialize by parsing the given command line string. |
| 233 | // The program name is assumed to be the first item in the string. |
Jan Wilken Dörrie | da77fd43 | 2019-10-24 21:40:34 | [diff] [blame] | 234 | void ParseFromString(StringPieceType command_line); |
David Bienvenu | edbb4ce | 2022-09-28 16:02:16 | [diff] [blame] | 235 | |
| 236 | // Returns true if the command line had the --single-argument switch, and |
| 237 | // thus likely came from a Windows shell registration. This is only set if the |
| 238 | // command line is parsed, and is not changed after it is parsed. |
| 239 | bool HasSingleArgumentSwitch() const { return has_single_argument_switch_; } |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 240 | #endif |
[email protected] | 4f08c83f | 2010-07-29 23:02:34 | [diff] [blame] | 241 | |
Will Harris | ee9f0a2 | 2023-03-02 21:38:58 | [diff] [blame] | 242 | // Detaches this object from the current sequence in preparation for a move to |
| 243 | // a different sequence. |
| 244 | void DetachFromCurrentSequence(); |
| 245 | |
Greg Guterman | fe16560d | 2021-10-07 19:58:15 | [diff] [blame] | 246 | // Sets a delegate that's called when we encounter a duplicate switch |
| 247 | static void SetDuplicateSwitchHandler( |
| 248 | std::unique_ptr<DuplicateSwitchHandler>); |
| 249 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 250 | private: |
Will Harris | ee9f0a2 | 2023-03-02 21:38:58 | [diff] [blame] | 251 | #if BUILDFLAG(ENABLE_COMMANDLINE_SEQUENCE_CHECKS) |
| 252 | // A helper class that encapsulates a SEQUENCE_CHECKER but allows copy. |
| 253 | // Copying this class will detach the sequence checker from the owning object. |
| 254 | class InstanceBoundSequenceChecker { |
| 255 | public: |
| 256 | InstanceBoundSequenceChecker() = default; |
| 257 | |
| 258 | InstanceBoundSequenceChecker(const InstanceBoundSequenceChecker& other) {} |
| 259 | |
| 260 | InstanceBoundSequenceChecker& operator=( |
| 261 | const InstanceBoundSequenceChecker& other) { |
| 262 | return *this; |
| 263 | } |
| 264 | |
| 265 | // Disallow move. |
| 266 | InstanceBoundSequenceChecker(InstanceBoundSequenceChecker&&) = delete; |
| 267 | InstanceBoundSequenceChecker& operator=(InstanceBoundSequenceChecker&&) = |
| 268 | delete; |
| 269 | |
| 270 | void Detach() { DETACH_FROM_SEQUENCE(sequence_checker_); } |
| 271 | void Check() { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); } |
| 272 | |
| 273 | private: |
| 274 | SEQUENCE_CHECKER(sequence_checker_); |
| 275 | }; |
| 276 | #endif // BUILDFLAG(ENABLE_COMMANDLINE_SEQUENCE_CHECKS) |
| 277 | |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 278 | // Disallow default constructor; a program name must be explicitly specified. |
Chris Watkins | 091d629 | 2017-12-13 04:25:58 | [diff] [blame] | 279 | CommandLine() = delete; |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 280 | |
Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 281 | // Append switches and arguments, keeping switches before arguments. |
| 282 | void AppendSwitchesAndArguments(const StringVector& argv); |
mgiuca | c974d510 | 2014-10-01 09:24:51 | [diff] [blame] | 283 | |
Jesse McKenna | 185ceda2 | 2021-03-10 06:47:55 | [diff] [blame] | 284 | // Internal version of GetArgumentsString to support allowing unsafe insert |
| 285 | // sequences in rare cases (see |
| 286 | // GetCommandLineStringWithUnsafeInsertSequences). |
| 287 | StringType GetArgumentsStringInternal( |
| 288 | bool allow_unsafe_insert_sequences) const; |
| 289 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 290 | #if BUILDFLAG(IS_WIN) |
Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 291 | // Initializes by parsing |raw_command_line_string_|, treating everything |
| 292 | // after |single_arg_switch_string| + <a single character> as the command |
| 293 | // line's single argument, and dropping any arguments previously parsed. The |
| 294 | // command line must contain |single_arg_switch_string|, and the argument, if |
| 295 | // present, must be separated from |single_arg_switch_string| by one |
| 296 | // character. |
| 297 | // NOTE: the single-argument switch is not preserved after parsing; |
| 298 | // GetCommandLineStringForShell() must be used to reproduce the original |
| 299 | // command-line string with single-argument switch. |
| 300 | void ParseAsSingleArgument(const StringType& single_arg_switch_string); |
| 301 | |
| 302 | // The string returned by GetCommandLineW(), to be parsed via |
| 303 | // ParseFromString(). Empty if this command line was not parsed from a string, |
| 304 | // or if ParseFromString() has finished executing. |
| 305 | StringPieceType raw_command_line_string_; |
David Bienvenu | edbb4ce | 2022-09-28 16:02:16 | [diff] [blame] | 306 | |
| 307 | // Set to true if the command line had --single-argument when initially |
| 308 | // parsed. It does not change if the command line mutates after initial |
| 309 | // parsing. |
| 310 | bool has_single_argument_switch_ = false; |
Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 311 | #endif |
mgiuca | c974d510 | 2014-10-01 09:24:51 | [diff] [blame] | 312 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 313 | // The singleton CommandLine representing the current process's command line. |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 314 | static CommandLine* current_process_commandline_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 315 | |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 316 | // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* } |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 317 | StringVector argv_; |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 318 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 319 | // Parsed-out switch keys and values. |
[email protected] | f6c3483 | 2010-05-24 10:39:59 | [diff] [blame] | 320 | SwitchMap switches_; |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 321 | |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 322 | // The index after the program and switches, any arguments start here. |
Peter Kasting | de85e74 | 2022-06-01 17:41:54 | [diff] [blame] | 323 | ptrdiff_t begin_args_; |
Will Harris | ee9f0a2 | 2023-03-02 21:38:58 | [diff] [blame] | 324 | |
| 325 | #if BUILDFLAG(ENABLE_COMMANDLINE_SEQUENCE_CHECKS) |
| 326 | InstanceBoundSequenceChecker sequence_checker_; |
| 327 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 328 | }; |
| 329 | |
Greg Guterman | fe16560d | 2021-10-07 19:58:15 | [diff] [blame] | 330 | class BASE_EXPORT DuplicateSwitchHandler { |
| 331 | public: |
| 332 | // out_value contains the existing value of the switch |
| 333 | virtual void ResolveDuplicate(base::StringPiece key, |
| 334 | CommandLine::StringPieceType new_value, |
| 335 | CommandLine::StringType& out_value) = 0; |
| 336 | virtual ~DuplicateSwitchHandler() = default; |
| 337 | }; |
| 338 | |
[email protected] | 2f3b1cc | 2014-03-17 23:07:15 | [diff] [blame] | 339 | } // namespace base |
| 340 | |
[email protected] | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 341 | #endif // BASE_COMMAND_LINE_H_ |