Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1518)

Side by Side Diff: base/command_line.h

Issue 3068004: base/ header cleanup. Forward declaration instead of including. (Closed) Base URL: http://src.chromium.org/git/chromium.git
Patch Set: chrome_launcher_unittest.cc Created 10 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | base/command_line.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 saved as extra arguments. An argument of "--" 8 // switch prefix are saved as extra arguments. An argument of "--"
9 // will terminate switch parsing, causing everything after to be 9 // will terminate switch parsing, causing everything after to be
10 // considered as extra arguments. 10 // considered as extra arguments.
11 11
12 // There is a singleton read-only CommandLine that represents the command 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 13 // line that the current process was started with. It must be initialized
14 // in main() (or whatever the platform's equivalent function is). 14 // in main() (or whatever the platform's equivalent function is).
15 15
16 #ifndef BASE_COMMAND_LINE_H_ 16 #ifndef BASE_COMMAND_LINE_H_
17 #define BASE_COMMAND_LINE_H_ 17 #define BASE_COMMAND_LINE_H_
18 #pragma once 18 #pragma once
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"
28 #include "base/logging.h" 27 #include "base/logging.h"
29 #include "base/string_util.h"
30 28
29 class FilePath;
31 class InProcessBrowserTest; 30 class InProcessBrowserTest;
32 31
33 class CommandLine { 32 class CommandLine {
34 public: 33 public:
35 // A constructor for CommandLines that are used only to carry arguments. 34 // A constructor for CommandLines that are used only to carry arguments.
36 enum ArgumentsOnly { ARGUMENTS_ONLY }; 35 enum ArgumentsOnly { ARGUMENTS_ONLY };
37 explicit CommandLine(ArgumentsOnly args_only); 36 explicit CommandLine(ArgumentsOnly args_only);
38 ~CommandLine(); 37 ~CommandLine();
39 38
40 #if defined(OS_WIN) 39 #if defined(OS_WIN)
41 // The type of native command line arguments. 40 // The type of native command line arguments.
42 typedef std::wstring StringType; 41 typedef std::wstring StringType;
43 42
44 // Initialize by parsing the given command-line string. 43 // Initialize by parsing the given command-line string.
45 // The program name is assumed to be the first item in the string. 44 // The program name is assumed to be the first item in the string.
46 void ParseFromString(const std::wstring& command_line); 45 void ParseFromString(const std::wstring& command_line);
47 static CommandLine FromString(const std::wstring& command_line) { 46 static CommandLine FromString(const std::wstring& command_line);
48 CommandLine cmd;
49 cmd.ParseFromString(command_line);
50 return cmd;
51 }
52 #elif defined(OS_POSIX) 47 #elif defined(OS_POSIX)
53 // The type of native command line arguments. 48 // The type of native command line arguments.
54 typedef std::string StringType; 49 typedef std::string StringType;
55 50
56 // Initialize from an argv vector. 51 // Initialize from an argv vector.
57 void InitFromArgv(int argc, const char* const* argv); 52 void InitFromArgv(int argc, const char* const* argv);
58 void InitFromArgv(const std::vector<std::string>& argv); 53 void InitFromArgv(const std::vector<std::string>& argv);
59 54
60 CommandLine(int argc, const char* const* argv) { 55 CommandLine(int argc, const char* const* argv);
61 InitFromArgv(argc, argv); 56 explicit CommandLine(const std::vector<std::string>& argv);
62 }
63 explicit CommandLine(const std::vector<std::string>& argv) {
64 InitFromArgv(argv);
65 }
66 #endif 57 #endif
67 58
68 // Construct a new, empty command line. 59 // Construct a new, empty command line.
69 // |program| is the name of the program to run (aka argv[0]). 60 // |program| is the name of the program to run (aka argv[0]).
70 explicit CommandLine(const FilePath& program); 61 explicit CommandLine(const FilePath& program);
71 62
72 // Initialize the current process CommandLine singleton. On Windows, 63 // Initialize the current process CommandLine singleton. On Windows,
73 // ignores its arguments (we instead parse GetCommandLineW() 64 // ignores its arguments (we instead parse GetCommandLineW()
74 // directly) because we don't trust the CRT's parsing of the command 65 // directly) because we don't trust the CRT's parsing of the command
75 // line, but it still must be called to set up the command line. 66 // line, but it still must be called to set up the command line.
(...skipping 22 matching lines...) Expand all
98 static CommandLine* ForCurrentProcess() { 89 static CommandLine* ForCurrentProcess() {
99 DCHECK(current_process_commandline_); 90 DCHECK(current_process_commandline_);
100 return current_process_commandline_; 91 return current_process_commandline_;
101 } 92 }
102 93
103 // Returns true if this command line contains the given switch. 94 // Returns true if this command line contains the given switch.
104 // (Switch names are case-insensitive.) 95 // (Switch names are case-insensitive.)
105 bool HasSwitch(const std::string& switch_string) const; 96 bool HasSwitch(const std::string& switch_string) const;
106 97
107 // Deprecated version of the above. 98 // Deprecated version of the above.
108 bool HasSwitch(const std::wstring& switch_string) const { 99 bool HasSwitch(const std::wstring& switch_string) const;
109 return HasSwitch(WideToASCII(switch_string));
110 }
111 100
112 // Returns the value associated with the given switch. If the 101 // Returns the value associated with the given switch. If the
113 // switch has no value or isn't present, this method returns 102 // switch has no value or isn't present, this method returns
114 // the empty string. 103 // the empty string.
115 // TODO(evanm): move these into command_line.cpp once we've fixed the 104 // TODO(evanm): move these into command_line.cpp once we've fixed the
116 // wstringness. 105 // wstringness.
117 std::string GetSwitchValueASCII(const std::string& switch_string) const { 106 std::string GetSwitchValueASCII(const std::string& switch_string) const;
118 return WideToASCII(GetSwitchValue(switch_string)); 107 FilePath GetSwitchValuePath(const std::string& switch_string) const;
119 }
120 FilePath GetSwitchValuePath(const std::string& switch_string) const {
121 return FilePath::FromWStringHack(GetSwitchValue(switch_string));
122 }
123 108
124 // Deprecated versions of the above. 109 // Deprecated versions of the above.
125 std::wstring GetSwitchValue(const std::string& switch_string) const; 110 std::wstring GetSwitchValue(const std::string& switch_string) const;
126 std::wstring GetSwitchValue(const std::wstring& switch_string) const { 111 std::wstring GetSwitchValue(const std::wstring& switch_string) const;
127 return GetSwitchValue(WideToASCII(switch_string));
128 }
129 112
130 // Get the number of switches in this process. 113 // Get the number of switches in this process.
131 size_t GetSwitchCount() const { return switches_.size(); } 114 size_t GetSwitchCount() const { return switches_.size(); }
132 115
133 // The type of map for parsed-out switch key and values. 116 // The type of map for parsed-out switch key and values.
134 typedef std::map<std::string, StringType> SwitchMap; 117 typedef std::map<std::string, StringType> SwitchMap;
135 118
136 // Get a copy of all switches, along with their values 119 // Get a copy of all switches, along with their values
137 SwitchMap GetSwitches() const { 120 const SwitchMap& GetSwitches() const {
138 return switches_; 121 return switches_;
139 } 122 }
140 123
141 // Get the remaining arguments to the command. 124 // Get the remaining arguments to the command.
142 const std::vector<StringType>& args() const { return args_; } 125 const std::vector<StringType>& args() const { return args_; }
143 126
144 #if defined(OS_WIN) 127 #if defined(OS_WIN)
145 // Returns the original command line string. 128 // Returns the original command line string.
146 const std::wstring& command_line_string() const { 129 const std::wstring& command_line_string() const {
147 return command_line_string_; 130 return command_line_string_;
148 } 131 }
149 #elif defined(OS_POSIX) 132 #elif defined(OS_POSIX)
150 // Returns the original command line string as a vector of strings. 133 // Returns the original command line string as a vector of strings.
151 const std::vector<std::string>& argv() const { 134 const std::vector<std::string>& argv() const {
152 return argv_; 135 return argv_;
153 } 136 }
154 // Try to match the same result as command_line_string() would get you 137 // Try to match the same result as command_line_string() would get you
155 // on windows. 138 // on windows.
156 std::string command_line_string() const; 139 std::string command_line_string() const;
157 #endif 140 #endif
158 141
159 // Returns the program part of the command line string (the first item). 142 // Returns the program part of the command line string (the first item).
160 FilePath GetProgram() const { 143 FilePath GetProgram() const;
161 return FilePath::FromWStringHack(program());
162 }
163 144
164 // Returns the program part of the command line string (the first item). 145 // Returns the program part of the command line string (the first item).
165 // Deprecated version of the above. 146 // Deprecated version of the above.
166 std::wstring program() const; 147 std::wstring program() const;
167 148
168 // Return a copy of the string prefixed with a switch prefix. 149 // Return a copy of the string prefixed with a switch prefix.
169 // Used internally. 150 // Used internally.
170 static std::wstring PrefixedSwitchString(const std::string& switch_string); 151 static std::wstring PrefixedSwitchString(const std::string& switch_string);
171 152
172 // Return a copy of the string prefixed with a switch prefix, 153 // Return a copy of the string prefixed with a switch prefix,
173 // and appended with the given value. Used internally. 154 // and appended with the given value. Used internally.
174 static std::wstring PrefixedSwitchStringWithValue( 155 static std::wstring PrefixedSwitchStringWithValue(
175 const std::string& switch_string, 156 const std::string& switch_string,
176 const std::wstring& value_string); 157 const std::wstring& value_string);
177 158
178 // Appends the given switch string (preceded by a space and a switch 159 // Appends the given switch string (preceded by a space and a switch
179 // prefix) to the given string. 160 // prefix) to the given string.
180 void AppendSwitch(const std::string& switch_string); 161 void AppendSwitch(const std::string& switch_string);
181 162
182 // Appends the given switch string (preceded by a space and a switch 163 // Appends the given switch string (preceded by a space and a switch
183 // prefix) to the given string, with the given value attached. 164 // prefix) to the given string, with the given value attached.
184 void AppendSwitchWithValue(const std::string& switch_string, 165 void AppendSwitchWithValue(const std::string& switch_string,
185 const std::wstring& value_string); 166 const std::wstring& value_string);
186 void AppendSwitchWithValue(const std::string& switch_string, 167 void AppendSwitchWithValue(const std::string& switch_string,
187 const std::string& value_string) { 168 const std::string& value_string);
188 AppendSwitchWithValue(switch_string, ASCIIToWide(value_string));
189 }
190 169
191 // Append a loose value to the command line. 170 // Append a loose value to the command line.
192 void AppendLooseValue(const std::wstring& value); 171 void AppendLooseValue(const std::wstring& value);
193 172
194 // Append the arguments from another command line to this one. 173 // Append the arguments from another command line to this one.
195 // If |include_program| is true, include |other|'s program as well. 174 // If |include_program| is true, include |other|'s program as well.
196 void AppendArguments(const CommandLine& other, 175 void AppendArguments(const CommandLine& other,
197 bool include_program); 176 bool include_program);
198 177
199 // On POSIX systems it's common to run processes via a wrapper (like 178 // On POSIX systems it's common to run processes via a wrapper (like
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 std::vector<StringType> args_; 220 std::vector<StringType> args_;
242 221
243 // We allow copy constructors, because a common pattern is to grab a 222 // We allow copy constructors, because a common pattern is to grab a
244 // copy of the current process's command line and then add some 223 // copy of the current process's command line and then add some
245 // flags to it. E.g.: 224 // flags to it. E.g.:
246 // CommandLine cl(*CommandLine::ForCurrentProcess()); 225 // CommandLine cl(*CommandLine::ForCurrentProcess());
247 // cl.AppendSwitch(...); 226 // cl.AppendSwitch(...);
248 }; 227 };
249 228
250 #endif // BASE_COMMAND_LINE_H_ 229 #endif // BASE_COMMAND_LINE_H_
OLDNEW
« no previous file with comments | « no previous file | base/command_line.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698