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