| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This class works with command lines: building and parsing. | 5 // This class works with command lines: building and parsing. |
| 6 // Arguments with prefixes ('--', '-', and on Windows, '/') are switches. | 6 // Arguments with prefixes ('--', '-', and on Windows, '/') are switches. |
| 7 // Switches will precede all other arguments without switch prefixes. | 7 // Switches will precede all other arguments without switch prefixes. |
| 8 // Switches can optionally have values, delimited by '=', e.g., "-switch=value". | 8 // Switches can optionally have values, delimited by '=', e.g., "-switch=value". |
| 9 // An argument of "--" will terminate switch parsing during initialization, | 9 // An argument of "--" will terminate switch parsing during initialization, |
| 10 // interpreting subsequent tokens as non-switch arguments, regardless of prefix. | 10 // interpreting subsequent tokens as non-switch arguments, regardless of prefix. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 #include "base/strings/string16.h" | 24 #include "base/strings/string16.h" |
| 25 #include "base/strings/string_piece.h" | 25 #include "base/strings/string_piece.h" |
| 26 #include "build/build_config.h" | 26 #include "build/build_config.h" |
| 27 | 27 |
| 28 namespace base { | 28 namespace base { |
| 29 | 29 |
| 30 class FilePath; | 30 class FilePath; |
| 31 | 31 |
| 32 class BASE_EXPORT CommandLine { | 32 class BASE_EXPORT CommandLine { |
| 33 public: | 33 public: |
| 34 #if defined(OS_WIN) | 34 #if defined(OS_POSIX) |
| 35 // The native command line string type. | |
| 36 typedef base::string16 StringType; | |
| 37 #elif defined(OS_POSIX) | |
| 38 typedef std::string StringType; | 35 typedef std::string StringType; |
| 39 #endif | 36 #endif |
| 40 | 37 |
| 41 typedef StringType::value_type CharType; | 38 typedef StringType::value_type CharType; |
| 42 typedef std::vector<StringType> StringVector; | 39 typedef std::vector<StringType> StringVector; |
| 43 typedef std::map<std::string, StringType> SwitchMap; | 40 typedef std::map<std::string, StringType> SwitchMap; |
| 44 typedef std::map<base::StringPiece, const StringType*> StringPieceSwitchMap; | 41 typedef std::map<base::StringPiece, const StringType*> StringPieceSwitchMap; |
| 45 | 42 |
| 46 // A constructor for CommandLines that only carry switches and arguments. | 43 // A constructor for CommandLines that only carry switches and arguments. |
| 47 enum NoProgram { NO_PROGRAM }; | 44 enum NoProgram { NO_PROGRAM }; |
| 48 explicit CommandLine(NoProgram no_program); | 45 explicit CommandLine(NoProgram no_program); |
| 49 | 46 |
| 50 // Construct a new command line with |program| as argv[0]. | 47 // Construct a new command line with |program| as argv[0]. |
| 51 explicit CommandLine(const FilePath& program); | 48 explicit CommandLine(const FilePath& program); |
| 52 | 49 |
| 53 // Construct a new command line from an argument list. | 50 // Construct a new command line from an argument list. |
| 54 CommandLine(int argc, const CharType* const* argv); | 51 CommandLine(int argc, const CharType* const* argv); |
| 55 explicit CommandLine(const StringVector& argv); | 52 explicit CommandLine(const StringVector& argv); |
| 56 | 53 |
| 57 // Override copy and assign to ensure |switches_by_stringpiece_| is valid. | 54 // Override copy and assign to ensure |switches_by_stringpiece_| is valid. |
| 58 CommandLine(const CommandLine& other); | 55 CommandLine(const CommandLine& other); |
| 59 CommandLine& operator=(const CommandLine& other); | 56 CommandLine& operator=(const CommandLine& other); |
| 60 | 57 |
| 61 ~CommandLine(); | 58 ~CommandLine(); |
| 62 | 59 |
| 63 #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(); | |
| 72 #endif | |
| 73 | |
| 74 // Initialize the current process CommandLine singleton. On Windows, ignores | 60 // Initialize the current process CommandLine singleton. On Windows, ignores |
| 75 // its arguments (we instead parse GetCommandLineW() directly) because we | 61 // its arguments (we instead parse GetCommandLineW() directly) because we |
| 76 // don't trust the CRT's parsing of the command line, but it still must be | 62 // don't trust the CRT's parsing of the command line, but it still must be |
| 77 // called to set up the command line. Returns false if initialization has | 63 // called to set up the command line. Returns false if initialization has |
| 78 // already occurred, and true otherwise. Only the caller receiving a 'true' | 64 // already occurred, and true otherwise. Only the caller receiving a 'true' |
| 79 // return value should take responsibility for calling Reset. | 65 // return value should take responsibility for calling Reset. |
| 80 static bool Init(int argc, const char* const* argv); | 66 static bool Init(int argc, const char* const* argv); |
| 81 | 67 |
| 82 // Destroys the current process CommandLine singleton. This is necessary if | 68 // Destroys the current process CommandLine singleton. This is necessary if |
| 83 // you want to reset the base library to its initial state (for example, in an | 69 // you want to reset the base library to its initial state (for example, in an |
| 84 // outer library that needs to be able to terminate, and be re-initialized). | 70 // outer library that needs to be able to terminate, and be re-initialized). |
| 85 // If Init is called only once, as in main(), Reset() is not necessary. | 71 // If Init is called only once, as in main(), Reset() is not necessary. |
| 86 static void Reset(); | 72 static void Reset(); |
| 87 | 73 |
| 88 // Get the singleton CommandLine representing the current process's | 74 // Get the singleton CommandLine representing the current process's |
| 89 // command line. Note: returned value is mutable, but not thread safe; | 75 // command line. Note: returned value is mutable, but not thread safe; |
| 90 // only mutate if you know what you're doing! | 76 // only mutate if you know what you're doing! |
| 91 static CommandLine* ForCurrentProcess(); | 77 static CommandLine* ForCurrentProcess(); |
| 92 | 78 |
| 93 // Returns true if the CommandLine has been initialized for the given process. | 79 // Returns true if the CommandLine has been initialized for the given process. |
| 94 static bool InitializedForCurrentProcess(); | 80 static bool InitializedForCurrentProcess(); |
| 95 | 81 |
| 96 #if defined(OS_WIN) | |
| 97 static CommandLine FromString(const base::string16& command_line); | |
| 98 #endif | |
| 99 | |
| 100 // Initialize from an argv vector. | 82 // Initialize from an argv vector. |
| 101 void InitFromArgv(int argc, const CharType* const* argv); | 83 void InitFromArgv(int argc, const CharType* const* argv); |
| 102 void InitFromArgv(const StringVector& argv); | 84 void InitFromArgv(const StringVector& argv); |
| 103 | 85 |
| 104 // Constructs and returns the represented command line string. | 86 // Constructs and returns the represented command line string. |
| 105 // CAUTION! This should be avoided on POSIX because quoting behavior is | 87 // CAUTION! This should be avoided on POSIX because quoting behavior is |
| 106 // unclear. | 88 // unclear. |
| 107 StringType GetCommandLineString() const { | 89 StringType GetCommandLineString() const { |
| 108 return GetCommandLineStringInternal(false); | 90 return GetCommandLineStringInternal(false); |
| 109 } | 91 } |
| 110 | 92 |
| 111 #if defined(OS_WIN) | |
| 112 // Constructs and returns the represented command line string. Assumes the | |
| 113 // command line contains placeholders (eg, %1) and quotes any program or | |
| 114 // argument with a '%' in it. This should be avoided unless the placeholder is | |
| 115 // required by an external interface (eg, the Windows registry), because it is | |
| 116 // not generally safe to replace it with an arbitrary string. If possible, | |
| 117 // placeholders should be replaced *before* converting the command line to a | |
| 118 // string. | |
| 119 StringType GetCommandLineStringWithPlaceholders() const { | |
| 120 return GetCommandLineStringInternal(true); | |
| 121 } | |
| 122 #endif | |
| 123 | |
| 124 // Constructs and returns the represented arguments string. | 93 // Constructs and returns the represented arguments string. |
| 125 // CAUTION! This should be avoided on POSIX because quoting behavior is | 94 // CAUTION! This should be avoided on POSIX because quoting behavior is |
| 126 // unclear. | 95 // unclear. |
| 127 StringType GetArgumentsString() const { | 96 StringType GetArgumentsString() const { |
| 128 return GetArgumentsStringInternal(false); | 97 return GetArgumentsStringInternal(false); |
| 129 } | 98 } |
| 130 | 99 |
| 131 #if defined(OS_WIN) | |
| 132 // Constructs and returns the represented arguments string. Assumes the | |
| 133 // command line contains placeholders (eg, %1) and quotes any argument with a | |
| 134 // '%' in it. This should be avoided unless the placeholder is required by an | |
| 135 // external interface (eg, the Windows registry), because it is not generally | |
| 136 // safe to replace it with an arbitrary string. If possible, placeholders | |
| 137 // should be replaced *before* converting the arguments to a string. | |
| 138 StringType GetArgumentsStringWithPlaceholders() const { | |
| 139 return GetArgumentsStringInternal(true); | |
| 140 } | |
| 141 #endif | |
| 142 | |
| 143 // Returns the original command line string as a vector of strings. | 100 // Returns the original command line string as a vector of strings. |
| 144 const StringVector& argv() const { return argv_; } | 101 const StringVector& argv() const { return argv_; } |
| 145 | 102 |
| 146 // Get and Set the program part of the command line string (the first item). | 103 // Get and Set the program part of the command line string (the first item). |
| 147 FilePath GetProgram() const; | 104 FilePath GetProgram() const; |
| 148 void SetProgram(const FilePath& program); | 105 void SetProgram(const FilePath& program); |
| 149 | 106 |
| 150 // Returns true if this command line contains the given switch. | 107 // Returns true if this command line contains the given switch. |
| 151 // Switch names must be lowercase. | 108 // Switch names must be lowercase. |
| 152 // The second override provides an optimized version to avoid inlining codegen | 109 // The second override provides an optimized version to avoid inlining codegen |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 void AppendArgNative(const StringType& value); | 150 void AppendArgNative(const StringType& value); |
| 194 | 151 |
| 195 // Append the switches and arguments from another command line to this one. | 152 // Append the switches and arguments from another command line to this one. |
| 196 // If |include_program| is true, include |other|'s program as well. | 153 // If |include_program| is true, include |other|'s program as well. |
| 197 void AppendArguments(const CommandLine& other, bool include_program); | 154 void AppendArguments(const CommandLine& other, bool include_program); |
| 198 | 155 |
| 199 // Insert a command before the current command. | 156 // Insert a command before the current command. |
| 200 // Common for debuggers, like "valgrind" or "gdb --args". | 157 // Common for debuggers, like "valgrind" or "gdb --args". |
| 201 void PrependWrapper(const StringType& wrapper); | 158 void PrependWrapper(const StringType& wrapper); |
| 202 | 159 |
| 203 #if defined(OS_WIN) | |
| 204 // Initialize by parsing the given command line string. | |
| 205 // The program name is assumed to be the first item in the string. | |
| 206 void ParseFromString(const base::string16& command_line); | |
| 207 #endif | |
| 208 | |
| 209 private: | 160 private: |
| 210 // Disallow default constructor; a program name must be explicitly specified. | 161 // Disallow default constructor; a program name must be explicitly specified. |
| 211 CommandLine(); | 162 CommandLine(); |
| 212 // Allow the copy constructor. A common pattern is to copy of the current | 163 // Allow the copy constructor. A common pattern is to copy of the current |
| 213 // process's command line and then add some flags to it. For example: | 164 // process's command line and then add some flags to it. For example: |
| 214 // CommandLine cl(*CommandLine::ForCurrentProcess()); | 165 // CommandLine cl(*CommandLine::ForCurrentProcess()); |
| 215 // cl.AppendSwitch(...); | 166 // cl.AppendSwitch(...); |
| 216 | 167 |
| 217 // Internal version of GetCommandLineString. If |quote_placeholders| is true, | 168 // Internal version of GetCommandLineString. If |quote_placeholders| is true, |
| 218 // also quotes parts with '%' in them. | 169 // also quotes parts with '%' in them. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 242 // Used for allocation-free lookups. | 193 // Used for allocation-free lookups. |
| 243 StringPieceSwitchMap switches_by_stringpiece_; | 194 StringPieceSwitchMap switches_by_stringpiece_; |
| 244 | 195 |
| 245 // The index after the program and switches, any arguments start here. | 196 // The index after the program and switches, any arguments start here. |
| 246 size_t begin_args_; | 197 size_t begin_args_; |
| 247 }; | 198 }; |
| 248 | 199 |
| 249 } // namespace base | 200 } // namespace base |
| 250 | 201 |
| 251 #endif // BASE_COMMAND_LINE_H_ | 202 #endif // BASE_COMMAND_LINE_H_ |
| OLD | NEW |