Chromium Code Reviews| 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 15 matching lines...) Expand all Loading... | |
| 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_WIN) |
| 35 // The native command line string type. | 35 // The native command line string type. |
| 36 typedef base::string16 StringType; | 36 using StringType = string16; |
| 37 #elif defined(OS_POSIX) | 37 #elif defined(OS_POSIX) |
| 38 typedef std::string StringType; | 38 using StringType = std::string; |
| 39 #endif | 39 #endif |
| 40 | 40 |
| 41 typedef StringType::value_type CharType; | 41 using CharType = StringType::value_type; |
| 42 typedef std::vector<StringType> StringVector; | 42 using StringVector = std::vector<StringType>; |
| 43 typedef std::map<std::string, StringType> SwitchMap; | 43 using SwitchMap = std::map<std::string, StringType>; |
| 44 typedef std::map<base::StringPiece, const StringType*> StringPieceSwitchMap; | 44 using StringPieceSwitchMap = std::map<StringPiece, const StringType*>; |
| 45 | 45 |
| 46 // A constructor for CommandLines that only carry switches and arguments. | 46 // A constructor for CommandLines that only carry switches and arguments. |
| 47 enum NoProgram { NO_PROGRAM }; | 47 enum NoProgram { NO_PROGRAM }; |
| 48 explicit CommandLine(NoProgram no_program); | 48 explicit CommandLine(NoProgram no_program); |
| 49 | 49 |
| 50 // Construct a new command line with |program| as argv[0]. | 50 // Construct a new command line with |program| as argv[0]. |
| 51 explicit CommandLine(const FilePath& program); | 51 explicit CommandLine(const FilePath& program); |
| 52 | 52 |
| 53 // Construct a new command line from an argument list. | 53 // Construct a new command line from an argument list. |
| 54 CommandLine(int argc, const CharType* const* argv); | 54 CommandLine(int argc, const CharType* const* argv); |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 83 // don't trust the CRT's parsing of the command line, but it still must be | 83 // don't trust the CRT's parsing of the command line, but it still must be |
| 84 // called to set up the command line. Returns false if initialization has | 84 // called to set up the command line. Returns false if initialization has |
| 85 // already occurred, and true otherwise. Only the caller receiving a 'true' | 85 // already occurred, and true otherwise. Only the caller receiving a 'true' |
| 86 // return value should take responsibility for calling Reset. | 86 // return value should take responsibility for calling Reset. |
| 87 static bool Init(int argc, const char* const* argv); | 87 static bool Init(int argc, const char* const* argv); |
| 88 | 88 |
| 89 // Destroys the current process CommandLine singleton. This is necessary if | 89 // Destroys the current process CommandLine singleton. This is necessary if |
| 90 // you want to reset the base library to its initial state (for example, in an | 90 // you want to reset the base library to its initial state (for example, in an |
| 91 // outer library that needs to be able to terminate, and be re-initialized). | 91 // outer library that needs to be able to terminate, and be re-initialized). |
| 92 // If Init is called only once, as in main(), Reset() is not necessary. | 92 // If Init is called only once, as in main(), Reset() is not necessary. |
| 93 // Do not call this in tests. Use base::test::ScopedCommandLine instead. | |
| 93 static void Reset(); | 94 static void Reset(); |
| 94 | 95 |
| 95 // Get the singleton CommandLine representing the current process's | 96 // Get the singleton CommandLine representing the current process's |
| 96 // command line. Note: returned value is mutable, but not thread safe; | 97 // command line. Note: returned value is mutable, but not thread safe; |
| 97 // only mutate if you know what you're doing! | 98 // only mutate if you know what you're doing! |
| 98 static CommandLine* ForCurrentProcess(); | 99 static CommandLine* ForCurrentProcess(); |
| 99 | 100 |
| 100 // Returns true if the CommandLine has been initialized for the given process. | 101 // Returns true if the CommandLine has been initialized for the given process. |
| 101 static bool InitializedForCurrentProcess(); | 102 static bool InitializedForCurrentProcess(); |
| 102 | 103 |
| 103 #if defined(OS_WIN) | 104 #if defined(OS_WIN) |
| 104 static CommandLine FromString(const base::string16& command_line); | 105 static CommandLine FromString(const string16& command_line); |
| 105 #endif | 106 #endif |
| 106 | 107 |
| 107 // Initialize from an argv vector. | 108 // Initialize from an argv vector. |
| 108 void InitFromArgv(int argc, const CharType* const* argv); | 109 void InitFromArgv(int argc, const CharType* const* argv); |
| 109 void InitFromArgv(const StringVector& argv); | 110 void InitFromArgv(const StringVector& argv); |
| 110 | 111 |
| 111 // Constructs and returns the represented command line string. | 112 // Constructs and returns the represented command line string. |
| 112 // CAUTION! This should be avoided on POSIX because quoting behavior is | 113 // CAUTION! This should be avoided on POSIX because quoting behavior is |
| 113 // unclear. | 114 // unclear. |
| 114 StringType GetCommandLineString() const { | 115 StringType GetCommandLineString() const { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 152 | 153 |
| 153 // Get and Set the program part of the command line string (the first item). | 154 // Get and Set the program part of the command line string (the first item). |
| 154 FilePath GetProgram() const; | 155 FilePath GetProgram() const; |
| 155 void SetProgram(const FilePath& program); | 156 void SetProgram(const FilePath& program); |
| 156 | 157 |
| 157 // Returns true if this command line contains the given switch. | 158 // Returns true if this command line contains the given switch. |
| 158 // Switch names must be lowercase. | 159 // Switch names must be lowercase. |
| 159 // The second override provides an optimized version to avoid inlining codegen | 160 // 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 // at every callsite to find the length of the constant and construct a |
| 161 // StringPiece. | 162 // StringPiece. |
| 162 bool HasSwitch(const base::StringPiece& switch_string) const; | 163 bool HasSwitch(const StringPiece& switch_string) const; |
|
Lei Zhang
2016/07/14 10:24:22
Shall we pass these by value while we are here?
Mark Mentovai
2016/07/14 14:24:02
Lei Zhang (Slow Thursday) wrote:
Lei Zhang
2016/07/15 00:44:39
base::StringPiece (and base::Time) says to prefer
| |
| 163 bool HasSwitch(const char switch_constant[]) const; | 164 bool HasSwitch(const char switch_constant[]) const; |
| 164 | 165 |
| 165 // Returns the value associated with the given switch. If the switch has no | 166 // 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. | 167 // value or isn't present, this method returns the empty string. |
| 167 // Switch names must be lowercase. | 168 // Switch names must be lowercase. |
| 168 std::string GetSwitchValueASCII(const base::StringPiece& switch_string) const; | 169 std::string GetSwitchValueASCII(const StringPiece& switch_string) const; |
| 169 FilePath GetSwitchValuePath(const base::StringPiece& switch_string) const; | 170 FilePath GetSwitchValuePath(const StringPiece& switch_string) const; |
| 170 StringType GetSwitchValueNative(const base::StringPiece& switch_string) const; | 171 StringType GetSwitchValueNative(const StringPiece& switch_string) const; |
| 171 | 172 |
| 172 // Get a copy of all switches, along with their values. | 173 // Get a copy of all switches, along with their values. |
| 173 const SwitchMap& GetSwitches() const { return switches_; } | 174 const SwitchMap& GetSwitches() const { return switches_; } |
| 174 | 175 |
| 175 // Append a switch [with optional value] to the command line. | 176 // Append a switch [with optional value] to the command line. |
| 176 // Note: Switches will precede arguments regardless of appending order. | 177 // Note: Switches will precede arguments regardless of appending order. |
| 177 void AppendSwitch(const std::string& switch_string); | 178 void AppendSwitch(const std::string& switch_string); |
| 178 void AppendSwitchPath(const std::string& switch_string, | 179 void AppendSwitchPath(const std::string& switch_string, |
| 179 const FilePath& path); | 180 const FilePath& path); |
| 180 void AppendSwitchNative(const std::string& switch_string, | 181 void AppendSwitchNative(const std::string& switch_string, |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 203 // If |include_program| is true, include |other|'s program as well. | 204 // If |include_program| is true, include |other|'s program as well. |
| 204 void AppendArguments(const CommandLine& other, bool include_program); | 205 void AppendArguments(const CommandLine& other, bool include_program); |
| 205 | 206 |
| 206 // Insert a command before the current command. | 207 // Insert a command before the current command. |
| 207 // Common for debuggers, like "valgrind" or "gdb --args". | 208 // Common for debuggers, like "valgrind" or "gdb --args". |
| 208 void PrependWrapper(const StringType& wrapper); | 209 void PrependWrapper(const StringType& wrapper); |
| 209 | 210 |
| 210 #if defined(OS_WIN) | 211 #if defined(OS_WIN) |
| 211 // Initialize by parsing the given command line string. | 212 // Initialize by parsing the given command line string. |
| 212 // The program name is assumed to be the first item in the string. | 213 // The program name is assumed to be the first item in the string. |
| 213 void ParseFromString(const base::string16& command_line); | 214 void ParseFromString(const string16& command_line); |
| 214 #endif | 215 #endif |
| 215 | 216 |
| 216 private: | 217 private: |
| 217 // Disallow default constructor; a program name must be explicitly specified. | 218 // Disallow default constructor; a program name must be explicitly specified. |
| 218 CommandLine(); | 219 CommandLine(); |
| 219 // Allow the copy constructor. A common pattern is to copy of the current | 220 // 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 // process's command line and then add some flags to it. For example: |
| 221 // CommandLine cl(*CommandLine::ForCurrentProcess()); | 222 // CommandLine cl(*CommandLine::ForCurrentProcess()); |
| 222 // cl.AppendSwitch(...); | 223 // cl.AppendSwitch(...); |
| 223 | 224 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 249 // Used for allocation-free lookups. | 250 // Used for allocation-free lookups. |
| 250 StringPieceSwitchMap switches_by_stringpiece_; | 251 StringPieceSwitchMap switches_by_stringpiece_; |
| 251 | 252 |
| 252 // The index after the program and switches, any arguments start here. | 253 // The index after the program and switches, any arguments start here. |
| 253 size_t begin_args_; | 254 size_t begin_args_; |
| 254 }; | 255 }; |
| 255 | 256 |
| 256 } // namespace base | 257 } // namespace base |
| 257 | 258 |
| 258 #endif // BASE_COMMAND_LINE_H_ | 259 #endif // BASE_COMMAND_LINE_H_ |
| OLD | NEW |