| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // Switches can optionally have a value attached using an equals sign, | 6 // Switches can optionally have a value attached using an equals sign, |
| 7 // as in "-switch=value". Arguments that aren't prefixed with a | 7 // as in "-switch=value". Arguments that aren't prefixed with a |
| 8 // switch prefix are saved as extra arguments. An argument of "--" | 8 // switch prefix are saved as extra arguments. An argument of "--" |
| 9 // will terminate switch parsing, causing everything after to be | 9 // will terminate switch parsing, causing everything after to be |
| 10 // considered as extra arguments. | 10 // considered as extra arguments. |
| 11 | 11 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 22 #include <vector> | 22 #include <vector> |
| 23 | 23 |
| 24 #include "base/basictypes.h" | 24 #include "base/basictypes.h" |
| 25 #include "build/build_config.h" | 25 #include "build/build_config.h" |
| 26 | 26 |
| 27 class FilePath; | 27 class FilePath; |
| 28 class InProcessBrowserTest; | 28 class InProcessBrowserTest; |
| 29 | 29 |
| 30 class CommandLine { | 30 class CommandLine { |
| 31 public: | 31 public: |
| 32 #if defined(OS_WIN) |
| 33 // The type of native command line arguments. |
| 34 typedef std::wstring StringType; |
| 35 #elif defined(OS_POSIX) |
| 36 // The type of native command line arguments. |
| 37 typedef std::string StringType; |
| 38 #endif |
| 39 |
| 40 // The type of map for parsed-out switch key and values. |
| 41 typedef std::map<std::string, StringType> SwitchMap; |
| 42 |
| 32 // A constructor for CommandLines that are used only to carry switches and | 43 // A constructor for CommandLines that are used only to carry switches and |
| 33 // arguments. | 44 // arguments. |
| 34 enum NoProgram { NO_PROGRAM }; | 45 enum NoProgram { NO_PROGRAM }; |
| 35 explicit CommandLine(NoProgram no_program); | 46 explicit CommandLine(NoProgram no_program); |
| 47 |
| 48 // Construct a new, empty command line. |
| 49 // |program| is the name of the program to run (aka argv[0]). |
| 50 explicit CommandLine(const FilePath& program); |
| 51 |
| 52 #if defined(OS_POSIX) |
| 53 CommandLine(int argc, const char* const* argv); |
| 54 explicit CommandLine(const std::vector<std::string>& argv); |
| 55 #endif |
| 56 |
| 36 ~CommandLine(); | 57 ~CommandLine(); |
| 37 | 58 |
| 38 #if defined(OS_WIN) | 59 #if defined(OS_WIN) |
| 39 // The type of native command line arguments. | |
| 40 typedef std::wstring StringType; | |
| 41 | |
| 42 // Initialize by parsing the given command-line string. | 60 // Initialize by parsing the given command-line string. |
| 43 // The program name is assumed to be the first item in the string. | 61 // The program name is assumed to be the first item in the string. |
| 44 void ParseFromString(const std::wstring& command_line); | 62 void ParseFromString(const std::wstring& command_line); |
| 45 static CommandLine FromString(const std::wstring& command_line); | 63 static CommandLine FromString(const std::wstring& command_line); |
| 46 #elif defined(OS_POSIX) | 64 #elif defined(OS_POSIX) |
| 47 // The type of native command line arguments. | |
| 48 typedef std::string StringType; | |
| 49 | |
| 50 // Initialize from an argv vector. | 65 // Initialize from an argv vector. |
| 51 void InitFromArgv(int argc, const char* const* argv); | 66 void InitFromArgv(int argc, const char* const* argv); |
| 52 void InitFromArgv(const std::vector<std::string>& argv); | 67 void InitFromArgv(const std::vector<std::string>& argv); |
| 53 | |
| 54 CommandLine(int argc, const char* const* argv); | |
| 55 explicit CommandLine(const std::vector<std::string>& argv); | |
| 56 #endif | 68 #endif |
| 57 | 69 |
| 58 // Construct a new, empty command line. | |
| 59 // |program| is the name of the program to run (aka argv[0]). | |
| 60 explicit CommandLine(const FilePath& program); | |
| 61 | |
| 62 // Initialize the current process CommandLine singleton. On Windows, | 70 // Initialize the current process CommandLine singleton. On Windows, |
| 63 // ignores its arguments (we instead parse GetCommandLineW() | 71 // ignores its arguments (we instead parse GetCommandLineW() |
| 64 // directly) because we don't trust the CRT's parsing of the command | 72 // directly) because we don't trust the CRT's parsing of the command |
| 65 // line, but it still must be called to set up the command line. | 73 // line, but it still must be called to set up the command line. |
| 66 static void Init(int argc, const char* const* argv); | 74 static void Init(int argc, const char* const* argv); |
| 67 | 75 |
| 68 // Destroys the current process CommandLine singleton. This is necessary if | 76 // Destroys the current process CommandLine singleton. This is necessary if |
| 69 // you want to reset the base library to its initial state (for example in an | 77 // you want to reset the base library to its initial state (for example in an |
| 70 // outer library that needs to be able to terminate, and be re-initialized). | 78 // outer library that needs to be able to terminate, and be re-initialized). |
| 71 // If Init is called only once, e.g. in main(), calling Reset() is not | 79 // If Init is called only once, e.g. in main(), calling Reset() is not |
| (...skipping 12 matching lines...) Expand all Loading... |
| 84 // Returns the value associated with the given switch. If the | 92 // Returns the value associated with the given switch. If the |
| 85 // switch has no value or isn't present, this method returns | 93 // switch has no value or isn't present, this method returns |
| 86 // the empty string. | 94 // the empty string. |
| 87 std::string GetSwitchValueASCII(const std::string& switch_string) const; | 95 std::string GetSwitchValueASCII(const std::string& switch_string) const; |
| 88 FilePath GetSwitchValuePath(const std::string& switch_string) const; | 96 FilePath GetSwitchValuePath(const std::string& switch_string) const; |
| 89 StringType GetSwitchValueNative(const std::string& switch_string) const; | 97 StringType GetSwitchValueNative(const std::string& switch_string) const; |
| 90 | 98 |
| 91 // Get the number of switches in this process. | 99 // Get the number of switches in this process. |
| 92 size_t GetSwitchCount() const { return switches_.size(); } | 100 size_t GetSwitchCount() const { return switches_.size(); } |
| 93 | 101 |
| 94 // The type of map for parsed-out switch key and values. | |
| 95 typedef std::map<std::string, StringType> SwitchMap; | |
| 96 | |
| 97 // Get a copy of all switches, along with their values | 102 // Get a copy of all switches, along with their values |
| 98 const SwitchMap& GetSwitches() const { | 103 const SwitchMap& GetSwitches() const { |
| 99 return switches_; | 104 return switches_; |
| 100 } | 105 } |
| 101 | 106 |
| 102 // Get the remaining arguments to the command. | 107 // Get the remaining arguments to the command. |
| 103 const std::vector<StringType>& args() const { return args_; } | 108 const std::vector<StringType>& args() const { return args_; } |
| 104 | 109 |
| 105 #if defined(OS_WIN) | 110 #if defined(OS_WIN) |
| 106 // Returns the original command line string. | 111 // Returns the original command line string. |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 size_t count); | 159 size_t count); |
| 155 | 160 |
| 156 private: | 161 private: |
| 157 friend class InProcessBrowserTest; | 162 friend class InProcessBrowserTest; |
| 158 | 163 |
| 159 CommandLine(); | 164 CommandLine(); |
| 160 | 165 |
| 161 // Used by InProcessBrowserTest. | 166 // Used by InProcessBrowserTest. |
| 162 static CommandLine* ForCurrentProcessMutable(); | 167 static CommandLine* ForCurrentProcessMutable(); |
| 163 | 168 |
| 169 // Returns true and fills in |switch_string| and |switch_value| |
| 170 // if |parameter_string| represents a switch. |
| 171 static bool IsSwitch(const StringType& parameter_string, |
| 172 std::string* switch_string, |
| 173 StringType* switch_value); |
| 174 |
| 164 // The singleton CommandLine instance representing the current process's | 175 // The singleton CommandLine instance representing the current process's |
| 165 // command line. | 176 // command line. |
| 166 static CommandLine* current_process_commandline_; | 177 static CommandLine* current_process_commandline_; |
| 167 | 178 |
| 168 // We store a platform-native version of the command line, used when building | 179 // We store a platform-native version of the command line, used when building |
| 169 // up a new command line to be executed. This ifdef delimits that code. | 180 // up a new command line to be executed. This ifdef delimits that code. |
| 170 | 181 |
| 171 #if defined(OS_WIN) | 182 #if defined(OS_WIN) |
| 172 // The quoted, space-separated command-line string. | 183 // The quoted, space-separated command-line string. |
| 173 std::wstring command_line_string_; | 184 std::wstring command_line_string_; |
| 174 // The name of the program. | 185 // The name of the program. |
| 175 std::wstring program_; | 186 std::wstring program_; |
| 176 #elif defined(OS_POSIX) | 187 #elif defined(OS_POSIX) |
| 177 // The argv array, with the program name in argv_[0]. | 188 // The argv array, with the program name in argv_[0]. |
| 178 std::vector<std::string> argv_; | 189 std::vector<std::string> argv_; |
| 179 #endif | 190 #endif |
| 180 | 191 |
| 181 // Returns true and fills in |switch_string| and |switch_value| | |
| 182 // if |parameter_string| represents a switch. | |
| 183 static bool IsSwitch(const StringType& parameter_string, | |
| 184 std::string* switch_string, | |
| 185 StringType* switch_value); | |
| 186 | |
| 187 // Parsed-out values. | 192 // Parsed-out values. |
| 188 SwitchMap switches_; | 193 SwitchMap switches_; |
| 189 | 194 |
| 190 // Non-switch command-line arguments. | 195 // Non-switch command-line arguments. |
| 191 std::vector<StringType> args_; | 196 std::vector<StringType> args_; |
| 192 | 197 |
| 193 // We allow copy constructors, because a common pattern is to grab a | 198 // We allow copy constructors, because a common pattern is to grab a |
| 194 // copy of the current process's command line and then add some | 199 // copy of the current process's command line and then add some |
| 195 // flags to it. E.g.: | 200 // flags to it. E.g.: |
| 196 // CommandLine cl(*CommandLine::ForCurrentProcess()); | 201 // CommandLine cl(*CommandLine::ForCurrentProcess()); |
| 197 // cl.AppendSwitch(...); | 202 // cl.AppendSwitch(...); |
| 198 }; | 203 }; |
| 199 | 204 |
| 200 #endif // BASE_COMMAND_LINE_H_ | 205 #endif // BASE_COMMAND_LINE_H_ |
| OLD | NEW |