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