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