[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame^] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 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 | |
| 20 | #include <map> |
| 21 | #include <string> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include "base/basictypes.h" |
[email protected] | 74e9fa2 | 2010-12-29 21:06:43 | [diff] [blame] | 25 | #include "build/build_config.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 26 | |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 27 | class FilePath; |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 28 | class InProcessBrowserTest; |
| 29 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 30 | class CommandLine { |
| 31 | public: |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame^] | 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 | |
[email protected] | 947446b | 2010-10-21 03:36:31 | [diff] [blame] | 43 | // A constructor for CommandLines that are used only to carry switches and |
| 44 | // arguments. |
| 45 | enum NoProgram { NO_PROGRAM }; |
| 46 | explicit CommandLine(NoProgram no_program); |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame^] | 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 | |
[email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 57 | ~CommandLine(); |
[email protected] | 51343d5a | 2009-10-26 22:39:33 | [diff] [blame] | 58 | |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 59 | #if defined(OS_WIN) |
[email protected] | 0189bbd | 2009-10-12 22:50:39 | [diff] [blame] | 60 | // Initialize by parsing the given command-line string. |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 61 | // The program name is assumed to be the first item in the string. |
| 62 | void ParseFromString(const std::wstring& command_line); |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 63 | static CommandLine FromString(const std::wstring& command_line); |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 64 | #elif defined(OS_POSIX) |
[email protected] | 0189bbd | 2009-10-12 22:50:39 | [diff] [blame] | 65 | // Initialize from an argv vector. |
| 66 | void InitFromArgv(int argc, const char* const* argv); |
| 67 | void InitFromArgv(const std::vector<std::string>& argv); |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 68 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 69 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 70 | // Initialize the current process CommandLine singleton. On Windows, |
| 71 | // ignores its arguments (we instead parse GetCommandLineW() |
| 72 | // directly) because we don't trust the CRT's parsing of the command |
| 73 | // line, but it still must be called to set up the command line. |
| 74 | static void Init(int argc, const char* const* argv); |
| 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(); |
[email protected] | a2318cda | 2009-02-25 21:13:53 | [diff] [blame] | 82 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 83 | // Get the singleton CommandLine representing the current process's |
[email protected] | 0189bbd | 2009-10-12 22:50:39 | [diff] [blame] | 84 | // command line. Note: returned value is mutable, but not thread safe; |
| 85 | // only mutate if you know what you're doing! |
[email protected] | dcd869c | 2010-08-30 20:15:25 | [diff] [blame] | 86 | static CommandLine* ForCurrentProcess(); |
[email protected] | 1a48f31 | 2008-08-12 01:14:37 | [diff] [blame] | 87 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 88 | // Returns true if this command line contains the given switch. |
| 89 | // (Switch names are case-insensitive.) |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 90 | bool HasSwitch(const std::string& switch_string) const; |
| 91 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 92 | // Returns the value associated with the given switch. If the |
| 93 | // switch has no value or isn't present, this method returns |
| 94 | // the empty string. |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 95 | std::string GetSwitchValueASCII(const std::string& switch_string) const; |
| 96 | FilePath GetSwitchValuePath(const std::string& switch_string) const; |
[email protected] | 4f08c83f | 2010-07-29 23:02:34 | [diff] [blame] | 97 | StringType GetSwitchValueNative(const std::string& switch_string) const; |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 98 | |
[email protected] | 9274524 | 2009-06-12 16:52:21 | [diff] [blame] | 99 | // Get the number of switches in this process. |
| 100 | size_t GetSwitchCount() const { return switches_.size(); } |
| 101 | |
[email protected] | bd48c2b0 | 2010-04-09 20:32:42 | [diff] [blame] | 102 | // Get a copy of all switches, along with their values |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 103 | const SwitchMap& GetSwitches() const { |
[email protected] | bd48c2b0 | 2010-04-09 20:32:42 | [diff] [blame] | 104 | return switches_; |
| 105 | } |
| 106 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 107 | // Get the remaining arguments to the command. |
[email protected] | 2e4c50c | 2010-07-21 15:57:23 | [diff] [blame] | 108 | const std::vector<StringType>& args() const { return args_; } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 109 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 110 | #if defined(OS_WIN) |
| 111 | // Returns the original command line string. |
| 112 | const std::wstring& command_line_string() const { |
| 113 | return command_line_string_; |
| 114 | } |
| 115 | #elif defined(OS_POSIX) |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 116 | // Returns the original command line string as a vector of strings. |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 117 | const std::vector<std::string>& argv() const { |
| 118 | return argv_; |
| 119 | } |
[email protected] | d3b04ab | 2010-05-21 17:14:57 | [diff] [blame] | 120 | // Try to match the same result as command_line_string() would get you |
| 121 | // on windows. |
| 122 | std::string command_line_string() const; |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 123 | #endif |
| 124 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 125 | // Returns the program part of the command line string (the first item). |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 126 | FilePath GetProgram() const; |
[email protected] | 0189bbd | 2009-10-12 22:50:39 | [diff] [blame] | 127 | |
[email protected] | 4f08c83f | 2010-07-29 23:02:34 | [diff] [blame] | 128 | // Append a switch to the command line. |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 129 | void AppendSwitch(const std::string& switch_string); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 130 | |
[email protected] | 4f08c83f | 2010-07-29 23:02:34 | [diff] [blame] | 131 | // Append a switch and value to the command line. |
| 132 | void AppendSwitchPath(const std::string& switch_string, const FilePath& path); |
| 133 | void AppendSwitchNative(const std::string& switch_string, |
| 134 | const StringType& value); |
[email protected] | d420c31e | 2010-07-30 02:14:22 | [diff] [blame] | 135 | void AppendSwitchASCII(const std::string& switch_string, |
| 136 | const std::string& value); |
[email protected] | 4f08c83f | 2010-07-29 23:02:34 | [diff] [blame] | 137 | |
[email protected] | 0445eb4 | 2010-08-13 22:10:30 | [diff] [blame] | 138 | // Append an argument to the command line. |
| 139 | // Note on quoting: the argument will be quoted properly such that it is |
| 140 | // interpreted as one argument to the target command. |
| 141 | // AppendArg is primarily for ASCII; non-ASCII input will be |
| 142 | // interpreted as UTF-8. |
| 143 | void AppendArg(const std::string& value); |
| 144 | void AppendArgPath(const FilePath& value); |
| 145 | void AppendArgNative(const StringType& value); |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 146 | |
| 147 | // Append the arguments from another command line to this one. |
| 148 | // If |include_program| is true, include |other|'s program as well. |
| 149 | void AppendArguments(const CommandLine& other, |
| 150 | bool include_program); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 151 | |
[email protected] | 13081fc | 2010-08-04 18:24:38 | [diff] [blame] | 152 | // Insert a command before the current command. Common for debuggers, |
| 153 | // like "valgrind" or "gdb --args". |
| 154 | void PrependWrapper(const StringType& wrapper); |
[email protected] | 052f1d48 | 2009-02-10 00:52:14 | [diff] [blame] | 155 | |
[email protected] | 4f08c83f | 2010-07-29 23:02:34 | [diff] [blame] | 156 | // Copy a set of switches (and their values, if any) from another command |
| 157 | // line. Commonly used when launching a subprocess. |
| 158 | void CopySwitchesFrom(const CommandLine& source, const char* const switches[], |
| 159 | size_t count); |
| 160 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 161 | private: |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 162 | friend class InProcessBrowserTest; |
| 163 | |
[email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 164 | CommandLine(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 165 | |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 166 | // Used by InProcessBrowserTest. |
[email protected] | dcd869c | 2010-08-30 20:15:25 | [diff] [blame] | 167 | static CommandLine* ForCurrentProcessMutable(); |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 168 | |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame^] | 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 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 175 | // The singleton CommandLine instance representing the current process's |
| 176 | // command line. |
| 177 | static CommandLine* current_process_commandline_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 178 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 179 | // We store a platform-native version of the command line, used when building |
| 180 | // up a new command line to be executed. This ifdef delimits that code. |
| 181 | |
| 182 | #if defined(OS_WIN) |
| 183 | // The quoted, space-separated command-line string. |
| 184 | std::wstring command_line_string_; |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 185 | // The name of the program. |
| 186 | std::wstring program_; |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 187 | #elif defined(OS_POSIX) |
| 188 | // The argv array, with the program name in argv_[0]. |
| 189 | std::vector<std::string> argv_; |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 190 | #endif |
| 191 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 192 | // Parsed-out values. |
[email protected] | f6c3483 | 2010-05-24 10:39:59 | [diff] [blame] | 193 | SwitchMap switches_; |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 194 | |
| 195 | // Non-switch command-line arguments. |
[email protected] | 2e4c50c | 2010-07-21 15:57:23 | [diff] [blame] | 196 | std::vector<StringType> args_; |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 197 | |
| 198 | // We allow copy constructors, because a common pattern is to grab a |
| 199 | // copy of the current process's command line and then add some |
| 200 | // flags to it. E.g.: |
| 201 | // CommandLine cl(*CommandLine::ForCurrentProcess()); |
| 202 | // cl.AppendSwitch(...); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 203 | }; |
| 204 | |
[email protected] | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 205 | #endif // BASE_COMMAND_LINE_H_ |