| license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 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. |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 6 | // Switches can optionally have a value attached using an equals sign, |
| 7 | // as in "-switch=value". Arguments that aren't prefixed with a |
| [email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 8 | // switch prefix are considered "loose parameters". Switch names are |
| 9 | // case-insensitive. An argument of "--" will terminate switch |
| 10 | // parsing, causing everything after to be considered as loose |
| 11 | // parameters. |
| 12 | |
| 13 | // There is a singleton read-only CommandLine that represents the command |
| 14 | // line that the current process was started with. It must be initialized |
| 15 | // in main() (or whatever the platform's equivalent function is). |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 16 | |
| [email protected] | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 17 | #ifndef BASE_COMMAND_LINE_H_ |
| 18 | #define BASE_COMMAND_LINE_H_ |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 19 | |
| [email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 20 | #include "build/build_config.h" |
| 21 | |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 22 | #include <map> |
| 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
| 26 | #include "base/basictypes.h" |
| [email protected] | 0189bbd | 2009-10-12 22:50:39 | [diff] [blame] | 27 | #include "base/file_path.h" |
| [email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 28 | #include "base/logging.h" |
| [email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame^] | 29 | #include "base/string_util.h" |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 30 | |
| [email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 31 | class InProcessBrowserTest; |
| 32 | |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 33 | class CommandLine { |
| 34 | public: |
| [email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 35 | #if defined(OS_WIN) |
| [email protected] | 0189bbd | 2009-10-12 22:50:39 | [diff] [blame] | 36 | // Initialize by parsing the given command-line string. |
| [email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 37 | // The program name is assumed to be the first item in the string. |
| 38 | void ParseFromString(const std::wstring& command_line); |
| [email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 39 | #elif defined(OS_POSIX) |
| [email protected] | 0189bbd | 2009-10-12 22:50:39 | [diff] [blame] | 40 | // Initialize from an argv vector. |
| 41 | void InitFromArgv(int argc, const char* const* argv); |
| 42 | void InitFromArgv(const std::vector<std::string>& argv); |
| 43 | |
| 44 | CommandLine(int argc, const char* const* argv) { |
| 45 | InitFromArgv(argc, argv); |
| 46 | } |
| 47 | explicit CommandLine(const std::vector<std::string>& argv) { |
| 48 | InitFromArgv(argv); |
| 49 | } |
| [email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 50 | #endif |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 51 | |
| [email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 52 | // Construct a new, empty command line. |
| 53 | // |program| is the name of the program to run (aka argv[0]). |
| [email protected] | 8f681e4 | 2009-10-09 20:37:56 | [diff] [blame] | 54 | explicit CommandLine(const FilePath& program); |
| 55 | |
| 56 | // Deprecated in favor of FilePath version. |
| [email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 57 | explicit CommandLine(const std::wstring& program); |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 58 | |
| [email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 59 | // Initialize the current process CommandLine singleton. On Windows, |
| 60 | // ignores its arguments (we instead parse GetCommandLineW() |
| 61 | // directly) because we don't trust the CRT's parsing of the command |
| 62 | // line, but it still must be called to set up the command line. |
| 63 | static void Init(int argc, const char* const* argv); |
| 64 | |
| [email protected] | 7f113f3 | 2009-09-10 18:02:17 | [diff] [blame] | 65 | #if defined(OS_LINUX) || defined(OS_FREEBSD) |
| 66 | // Sets the current process' arguments that show in "ps" etc. to those |
| 67 | // in |current_process_commandline_|. Used by the zygote host so that |
| 68 | // renderers show up with --type=renderer. |
| 69 | static void SetProcTitle(); |
| [email protected] | 7f113f3 | 2009-09-10 18:02:17 | [diff] [blame] | 70 | #endif |
| 71 | |
| [email protected] | a2318cda | 2009-02-25 21:13:53 | [diff] [blame] | 72 | // Destroys the current process CommandLine singleton. This is necessary if |
| 73 | // you want to reset the base library to its initial state (for example in an |
| 74 | // outer library that needs to be able to terminate, and be re-initialized). |
| [email protected] | 02fb75ab | 2009-10-12 16:11:40 | [diff] [blame] | 75 | // If Init is called only once, e.g. in main(), calling Reset() is not |
| [email protected] | a2318cda | 2009-02-25 21:13:53 | [diff] [blame] | 76 | // necessary. |
| [email protected] | 02fb75ab | 2009-10-12 16:11:40 | [diff] [blame] | 77 | static void Reset(); |
| 78 | // The same function snuck into this class under two different names; |
| 79 | // this one remains for backwards compat with the older o3d build. |
| 80 | static void Terminate() { 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] | 0189bbd | 2009-10-12 22:50:39 | [diff] [blame] | 83 | // command line. Note: returned value is mutable, but not thread safe; |
| 84 | // only mutate if you know what you're doing! |
| 85 | static CommandLine* ForCurrentProcess() { |
| [email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 86 | DCHECK(current_process_commandline_); |
| 87 | return current_process_commandline_; |
| 88 | } |
| [email protected] | 1a48f31 | 2008-08-12 01:14:37 | [diff] [blame] | 89 | |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 90 | // Returns true if this command line contains the given switch. |
| 91 | // (Switch names are case-insensitive.) |
| [email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame^] | 92 | bool HasSwitch(const std::string& switch_string) const; |
| 93 | |
| 94 | // Deprecated version of the above. |
| 95 | bool HasSwitch(const std::wstring& switch_string) const { |
| 96 | return HasSwitch(WideToASCII(switch_string)); |
| 97 | } |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 98 | |
| 99 | // Returns the value associated with the given switch. If the |
| 100 | // switch has no value or isn't present, this method returns |
| 101 | // the empty string. |
| [email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame^] | 102 | std::wstring GetSwitchValue(const std::string& switch_string) const; |
| 103 | std::string GetSwitchValueASCII(const std::string& switch_string) const { |
| 104 | return WideToASCII(GetSwitchValue(switch_string)); |
| 105 | } |
| 106 | |
| 107 | // Deprecated version of the above. |
| 108 | std::wstring GetSwitchValue(const std::wstring& switch_string) const { |
| 109 | return GetSwitchValue(WideToASCII(switch_string)); |
| 110 | } |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 111 | |
| [email protected] | 9274524 | 2009-06-12 16:52:21 | [diff] [blame] | 112 | // Get the number of switches in this process. |
| 113 | size_t GetSwitchCount() const { return switches_.size(); } |
| 114 | |
| [email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 115 | // Get the remaining arguments to the command. |
| 116 | // WARNING: this is incorrect on POSIX; we must do string conversions. |
| 117 | std::vector<std::wstring> GetLooseValues() const; |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 118 | |
| [email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 119 | #if defined(OS_WIN) |
| 120 | // Returns the original command line string. |
| 121 | const std::wstring& command_line_string() const { |
| 122 | return command_line_string_; |
| 123 | } |
| 124 | #elif defined(OS_POSIX) |
| [email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 125 | // Returns the original command line string as a vector of strings. |
| [email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 126 | const std::vector<std::string>& argv() const { |
| 127 | return argv_; |
| 128 | } |
| [email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 129 | #endif |
| 130 | |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 131 | // Returns the program part of the command line string (the first item). |
| [email protected] | 0189bbd | 2009-10-12 22:50:39 | [diff] [blame] | 132 | FilePath GetProgram() const { |
| 133 | return FilePath::FromWStringHack(program()); |
| 134 | } |
| 135 | |
| 136 | // Returns the program part of the command line string (the first item). |
| 137 | // Deprecated version of the above. |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 138 | std::wstring program() const; |
| 139 | |
| [email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 140 | // Return a copy of the string prefixed with a switch prefix. |
| 141 | // Used internally. |
| [email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame^] | 142 | static std::wstring PrefixedSwitchString(const std::string& switch_string); |
| [email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 143 | |
| 144 | // Return a copy of the string prefixed with a switch prefix, |
| 145 | // and appended with the given value. Used internally. |
| 146 | static std::wstring PrefixedSwitchStringWithValue( |
| [email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame^] | 147 | const std::string& switch_string, |
| [email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 148 | const std::wstring& value_string); |
| 149 | |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 150 | // Appends the given switch string (preceded by a space and a switch |
| 151 | // prefix) to the given string. |
| [email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame^] | 152 | void AppendSwitch(const std::string& switch_string); |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 153 | |
| 154 | // Appends the given switch string (preceded by a space and a switch |
| 155 | // prefix) to the given string, with the given value attached. |
| [email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame^] | 156 | void AppendSwitchWithValue(const std::string& switch_string, |
| [email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 157 | const std::wstring& value_string); |
| [email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame^] | 158 | void AppendSwitchWithValue(const std::string& switch_string, |
| 159 | const std::string& value_string) { |
| 160 | AppendSwitchWithValue(switch_string, ASCIIToWide(value_string)); |
| 161 | } |
| [email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 162 | |
| 163 | // Append a loose value to the command line. |
| 164 | void AppendLooseValue(const std::wstring& value); |
| 165 | |
| 166 | // Append the arguments from another command line to this one. |
| 167 | // If |include_program| is true, include |other|'s program as well. |
| 168 | void AppendArguments(const CommandLine& other, |
| 169 | bool include_program); |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 170 | |
| [email protected] | 052f1d48 | 2009-02-10 00:52:14 | [diff] [blame] | 171 | // On POSIX systems it's common to run processes via a wrapper (like |
| [email protected] | e5e19c3 | 2009-04-20 22:10:02 | [diff] [blame] | 172 | // "valgrind" or "gdb --args"). |
| [email protected] | 052f1d48 | 2009-02-10 00:52:14 | [diff] [blame] | 173 | void PrependWrapper(const std::wstring& wrapper); |
| 174 | |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 175 | private: |
| [email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 176 | friend class InProcessBrowserTest; |
| 177 | |
| [email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 178 | CommandLine() {} |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 179 | |
| [email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 180 | // Used by InProcessBrowserTest. |
| 181 | static CommandLine* ForCurrentProcessMutable() { |
| 182 | DCHECK(current_process_commandline_); |
| 183 | return current_process_commandline_; |
| 184 | } |
| 185 | |
| [email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 186 | // The singleton CommandLine instance representing the current process's |
| 187 | // command line. |
| 188 | static CommandLine* current_process_commandline_; |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 189 | |
| [email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 190 | // We store a platform-native version of the command line, used when building |
| 191 | // up a new command line to be executed. This ifdef delimits that code. |
| 192 | |
| 193 | #if defined(OS_WIN) |
| 194 | // The quoted, space-separated command-line string. |
| 195 | std::wstring command_line_string_; |
| 196 | |
| 197 | // The name of the program. |
| 198 | std::wstring program_; |
| 199 | |
| 200 | // The type of native command line arguments. |
| 201 | typedef std::wstring StringType; |
| 202 | |
| 203 | #elif defined(OS_POSIX) |
| 204 | // The argv array, with the program name in argv_[0]. |
| 205 | std::vector<std::string> argv_; |
| 206 | |
| 207 | // The type of native command line arguments. |
| 208 | typedef std::string StringType; |
| [email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 209 | #endif |
| 210 | |
| 211 | // Returns true and fills in |switch_string| and |switch_value| |
| 212 | // if |parameter_string| represents a switch. |
| 213 | static bool IsSwitch(const StringType& parameter_string, |
| 214 | std::string* switch_string, |
| 215 | StringType* switch_value); |
| 216 | |
| 217 | // Parsed-out values. |
| 218 | std::map<std::string, StringType> switches_; |
| 219 | |
| 220 | // Non-switch command-line arguments. |
| 221 | std::vector<StringType> loose_values_; |
| 222 | |
| 223 | // We allow copy constructors, because a common pattern is to grab a |
| 224 | // copy of the current process's command line and then add some |
| 225 | // flags to it. E.g.: |
| 226 | // CommandLine cl(*CommandLine::ForCurrentProcess()); |
| 227 | // cl.AppendSwitch(...); |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 228 | }; |
| 229 | |
| [email protected] | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 230 | #endif // BASE_COMMAND_LINE_H_ |