OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This class works with command lines: building and parsing. | 5 // This class works with command lines: building and parsing. |
6 // Switches can optionally have a value attached using an equals sign, | 6 // Switches can optionally have a value attached using an equals sign, |
7 // as in "-switch=value". Arguments that aren't prefixed with a | 7 // as in "-switch=value". Arguments that aren't prefixed with a |
8 // switch prefix are considered "loose parameters". Switch names are | 8 // switch prefix are considered "loose parameters". Switch names are |
9 // case-insensitive. An argument of "--" will terminate switch | 9 // case-insensitive. An argument of "--" will terminate switch |
10 // parsing, causing everything after to be considered as loose | 10 // parsing, causing everything after to be considered as loose |
11 // parameters. | 11 // parameters. |
12 | 12 |
13 // There is a singleton read-only CommandLine that represents the command | 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 | 14 // line that the current process was started with. It must be initialized |
15 // in main() (or whatever the platform's equivalent function is). | 15 // in main() (or whatever the platform's equivalent function is). |
16 | 16 |
17 #ifndef BASE_COMMAND_LINE_H_ | 17 #ifndef BASE_COMMAND_LINE_H_ |
18 #define BASE_COMMAND_LINE_H_ | 18 #define BASE_COMMAND_LINE_H_ |
19 | 19 |
20 #include "build/build_config.h" | 20 #include "build/build_config.h" |
21 | 21 |
22 #include <map> | 22 #include <map> |
23 #include <string> | 23 #include <string> |
24 #include <vector> | 24 #include <vector> |
25 | 25 |
26 #include "base/basictypes.h" | 26 #include "base/basictypes.h" |
27 #include "base/file_path.h" | 27 #include "base/file_path.h" |
28 #include "base/logging.h" | 28 #include "base/logging.h" |
| 29 #include "base/string_util.h" |
29 | 30 |
30 class InProcessBrowserTest; | 31 class InProcessBrowserTest; |
31 | 32 |
32 class CommandLine { | 33 class CommandLine { |
33 public: | 34 public: |
34 #if defined(OS_WIN) | 35 #if defined(OS_WIN) |
35 // Initialize by parsing the given command-line string. | 36 // Initialize by parsing the given command-line string. |
36 // The program name is assumed to be the first item in the string. | 37 // The program name is assumed to be the first item in the string. |
37 void ParseFromString(const std::wstring& command_line); | 38 void ParseFromString(const std::wstring& command_line); |
38 #elif defined(OS_POSIX) | 39 #elif defined(OS_POSIX) |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 // Get the singleton CommandLine representing the current process's | 82 // Get the singleton CommandLine representing the current process's |
82 // command line. Note: returned value is mutable, but not thread safe; | 83 // command line. Note: returned value is mutable, but not thread safe; |
83 // only mutate if you know what you're doing! | 84 // only mutate if you know what you're doing! |
84 static CommandLine* ForCurrentProcess() { | 85 static CommandLine* ForCurrentProcess() { |
85 DCHECK(current_process_commandline_); | 86 DCHECK(current_process_commandline_); |
86 return current_process_commandline_; | 87 return current_process_commandline_; |
87 } | 88 } |
88 | 89 |
89 // Returns true if this command line contains the given switch. | 90 // Returns true if this command line contains the given switch. |
90 // (Switch names are case-insensitive.) | 91 // (Switch names are case-insensitive.) |
91 bool HasSwitch(const std::wstring& switch_string) const; | 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 } |
92 | 98 |
93 // Returns the value associated with the given switch. If the | 99 // Returns the value associated with the given switch. If the |
94 // switch has no value or isn't present, this method returns | 100 // switch has no value or isn't present, this method returns |
95 // the empty string. | 101 // the empty string. |
96 std::wstring GetSwitchValue(const std::wstring& switch_string) const; | 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 } |
97 | 111 |
98 // Get the number of switches in this process. | 112 // Get the number of switches in this process. |
99 size_t GetSwitchCount() const { return switches_.size(); } | 113 size_t GetSwitchCount() const { return switches_.size(); } |
100 | 114 |
101 // Get the remaining arguments to the command. | 115 // Get the remaining arguments to the command. |
102 // WARNING: this is incorrect on POSIX; we must do string conversions. | 116 // WARNING: this is incorrect on POSIX; we must do string conversions. |
103 std::vector<std::wstring> GetLooseValues() const; | 117 std::vector<std::wstring> GetLooseValues() const; |
104 | 118 |
105 #if defined(OS_WIN) | 119 #if defined(OS_WIN) |
106 // Returns the original command line string. | 120 // Returns the original command line string. |
(...skipping 11 matching lines...) Expand all Loading... |
118 FilePath GetProgram() const { | 132 FilePath GetProgram() const { |
119 return FilePath::FromWStringHack(program()); | 133 return FilePath::FromWStringHack(program()); |
120 } | 134 } |
121 | 135 |
122 // Returns the program part of the command line string (the first item). | 136 // Returns the program part of the command line string (the first item). |
123 // Deprecated version of the above. | 137 // Deprecated version of the above. |
124 std::wstring program() const; | 138 std::wstring program() const; |
125 | 139 |
126 // Return a copy of the string prefixed with a switch prefix. | 140 // Return a copy of the string prefixed with a switch prefix. |
127 // Used internally. | 141 // Used internally. |
128 static std::wstring PrefixedSwitchString(const std::wstring& switch_string); | 142 static std::wstring PrefixedSwitchString(const std::string& switch_string); |
129 | 143 |
130 // Return a copy of the string prefixed with a switch prefix, | 144 // Return a copy of the string prefixed with a switch prefix, |
131 // and appended with the given value. Used internally. | 145 // and appended with the given value. Used internally. |
132 static std::wstring PrefixedSwitchStringWithValue( | 146 static std::wstring PrefixedSwitchStringWithValue( |
133 const std::wstring& switch_string, | 147 const std::string& switch_string, |
134 const std::wstring& value_string); | 148 const std::wstring& value_string); |
135 | 149 |
136 // Appends the given switch string (preceded by a space and a switch | 150 // Appends the given switch string (preceded by a space and a switch |
137 // prefix) to the given string. | 151 // prefix) to the given string. |
138 void AppendSwitch(const std::wstring& switch_string); | 152 void AppendSwitch(const std::string& switch_string); |
139 | 153 |
140 // Appends the given switch string (preceded by a space and a switch | 154 // Appends the given switch string (preceded by a space and a switch |
141 // prefix) to the given string, with the given value attached. | 155 // prefix) to the given string, with the given value attached. |
142 void AppendSwitchWithValue(const std::wstring& switch_string, | 156 void AppendSwitchWithValue(const std::string& switch_string, |
143 const std::wstring& value_string); | 157 const std::wstring& value_string); |
| 158 void AppendSwitchWithValue(const std::string& switch_string, |
| 159 const std::string& value_string) { |
| 160 AppendSwitchWithValue(switch_string, ASCIIToWide(value_string)); |
| 161 } |
144 | 162 |
145 // Append a loose value to the command line. | 163 // Append a loose value to the command line. |
146 void AppendLooseValue(const std::wstring& value); | 164 void AppendLooseValue(const std::wstring& value); |
147 | 165 |
148 // Append the arguments from another command line to this one. | 166 // Append the arguments from another command line to this one. |
149 // If |include_program| is true, include |other|'s program as well. | 167 // If |include_program| is true, include |other|'s program as well. |
150 void AppendArguments(const CommandLine& other, | 168 void AppendArguments(const CommandLine& other, |
151 bool include_program); | 169 bool include_program); |
152 | 170 |
153 // On POSIX systems it's common to run processes via a wrapper (like | 171 // On POSIX systems it's common to run processes via a wrapper (like |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 std::vector<StringType> loose_values_; | 221 std::vector<StringType> loose_values_; |
204 | 222 |
205 // We allow copy constructors, because a common pattern is to grab a | 223 // We allow copy constructors, because a common pattern is to grab a |
206 // copy of the current process's command line and then add some | 224 // copy of the current process's command line and then add some |
207 // flags to it. E.g.: | 225 // flags to it. E.g.: |
208 // CommandLine cl(*CommandLine::ForCurrentProcess()); | 226 // CommandLine cl(*CommandLine::ForCurrentProcess()); |
209 // cl.AppendSwitch(...); | 227 // cl.AppendSwitch(...); |
210 }; | 228 }; |
211 | 229 |
212 #endif // BASE_COMMAND_LINE_H_ | 230 #endif // BASE_COMMAND_LINE_H_ |
OLD | NEW |