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

Side by Side Diff: base/command_line.h

Issue 6596020: Reorganize CommandLine code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments. Created 9 years, 9 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 | Annotate | Revision Log
« 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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, as in
7 // as in "-switch=value". Arguments that aren't prefixed with a 7 // "-switch=value". Arguments that aren't prefixed with a switch prefix are
8 // switch prefix are saved as extra arguments. An argument of "--" 8 // saved as extra arguments. An argument of "--" will terminate switch parsing,
9 // will terminate switch parsing, causing everything after to be 9 // causing everything after to be considered as extra arguments.
10 // considered as extra arguments.
11 10
12 // There is a singleton read-only CommandLine that represents the command 11 // There is a singleton read-only CommandLine that represents the command line
13 // line that the current process was started with. It must be initialized 12 // that the current process was started with. It must be initialized in main().
14 // in main() (or whatever the platform's equivalent function is).
15 13
16 #ifndef BASE_COMMAND_LINE_H_ 14 #ifndef BASE_COMMAND_LINE_H_
17 #define BASE_COMMAND_LINE_H_ 15 #define BASE_COMMAND_LINE_H_
18 #pragma once 16 #pragma once
19 17
20 #include <map> 18 #include <map>
21 #include <string> 19 #include <string>
22 #include <vector> 20 #include <vector>
23 21
24 #include "base/basictypes.h" 22 #include "base/basictypes.h"
25 #include "build/build_config.h" 23 #include "build/build_config.h"
26 24
27 class FilePath; 25 class FilePath;
28 class InProcessBrowserTest;
29 26
30 class CommandLine { 27 class CommandLine {
31 public: 28 public:
32 #if defined(OS_WIN) 29 #if defined(OS_WIN)
33 // The type of native command line arguments. 30 // The native command line string type.
34 typedef std::wstring StringType; 31 typedef std::wstring StringType;
35 #elif defined(OS_POSIX) 32 #elif defined(OS_POSIX)
36 // The type of native command line arguments.
37 typedef std::string StringType; 33 typedef std::string StringType;
38 #endif 34 #endif
39 35
36 typedef std::vector<StringType> StringVector;
40 // The type of map for parsed-out switch key and values. 37 // The type of map for parsed-out switch key and values.
41 typedef std::map<std::string, StringType> SwitchMap; 38 typedef std::map<std::string, StringType> SwitchMap;
42 39
43 // A constructor for CommandLines that are used only to carry switches and 40 // A constructor for CommandLines that only carry switches and arguments.
44 // arguments.
45 enum NoProgram { NO_PROGRAM }; 41 enum NoProgram { NO_PROGRAM };
46 explicit CommandLine(NoProgram no_program); 42 explicit CommandLine(NoProgram no_program);
47 43
48 // Construct a new, empty command line. 44 // Construct a new command line with |program| as argv[0].
49 // |program| is the name of the program to run (aka argv[0]).
50 explicit CommandLine(const FilePath& program); 45 explicit CommandLine(const FilePath& program);
51 46
52 #if defined(OS_POSIX) 47 #if defined(OS_POSIX)
53 CommandLine(int argc, const char* const* argv); 48 CommandLine(int argc, const char* const* argv);
54 explicit CommandLine(const std::vector<std::string>& argv); 49 explicit CommandLine(const StringVector& argv);
55 #endif 50 #endif
56 51
57 ~CommandLine(); 52 // Initialize the current process CommandLine singleton. On Windows, ignores
58 53 // its arguments (we instead parse GetCommandLineW() directly) because we
59 #if defined(OS_WIN) 54 // don't trust the CRT's parsing of the command line, but it still must be
60 // Initialize by parsing the given command-line string. 55 // called to set up the command line.
61 // The program name is assumed to be the first item in the string.
62 void ParseFromString(const std::wstring& command_line);
63 static CommandLine FromString(const std::wstring& command_line);
64 #elif defined(OS_POSIX)
65 // Initialize from an argv vector.
66 void InitFromArgv(int argc, const char* const* argv);
67 void InitFromArgv(const std::vector<std::string>& argv);
68 #endif
69
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); 56 static void Init(int argc, const char* const* argv);
75 57
76 // Destroys the current process CommandLine singleton. This is necessary if 58 // 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 59 // 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). 60 // outer library that needs to be able to terminate, and be re-initialized).
79 // If Init is called only once, e.g. in main(), calling Reset() is not 61 // If Init is called only once, as in main(), Reset() is not necessary.
80 // necessary.
81 static void Reset(); 62 static void Reset();
82 63
83 // Get the singleton CommandLine representing the current process's 64 // Get the singleton CommandLine representing the current process's
84 // command line. Note: returned value is mutable, but not thread safe; 65 // command line. Note: returned value is mutable, but not thread safe;
85 // only mutate if you know what you're doing! 66 // only mutate if you know what you're doing!
86 static CommandLine* ForCurrentProcess(); 67 static CommandLine* ForCurrentProcess();
87 68
69 #if defined(OS_WIN)
70 static CommandLine FromString(const std::wstring& command_line);
71 #endif
72
73 #if defined(OS_POSIX)
74 // Initialize from an argv vector.
75 void InitFromArgv(int argc, const char* const* argv);
76 void InitFromArgv(const StringVector& argv);
77 #endif
78
79 // Returns the represented command line string.
80 // CAUTION! This should be avoided because quoting behavior is unclear.
81 StringType command_line_string() const;
82
83 #if defined(OS_POSIX)
84 // Returns the original command line string as a vector of strings.
85 const StringVector& argv() const { return argv_; }
86 #endif
87
88 // Returns the program part of the command line string (the first item).
89 FilePath GetProgram() const;
90
88 // Returns true if this command line contains the given switch. 91 // Returns true if this command line contains the given switch.
89 // (Switch names are case-insensitive.) 92 // (Switch names are case-insensitive).
90 bool HasSwitch(const std::string& switch_string) const; 93 bool HasSwitch(const std::string& switch_string) const;
91 94
92 // Returns the value associated with the given switch. If the 95 // Returns the value associated with the given switch. If the switch has no
93 // switch has no value or isn't present, this method returns 96 // value or isn't present, this method returns the empty string.
94 // the empty string.
95 std::string GetSwitchValueASCII(const std::string& switch_string) const; 97 std::string GetSwitchValueASCII(const std::string& switch_string) const;
96 FilePath GetSwitchValuePath(const std::string& switch_string) const; 98 FilePath GetSwitchValuePath(const std::string& switch_string) const;
97 StringType GetSwitchValueNative(const std::string& switch_string) const; 99 StringType GetSwitchValueNative(const std::string& switch_string) const;
98 100
99 // Get the number of switches in this process. 101 // Get the number of switches in this process.
100 size_t GetSwitchCount() const { return switches_.size(); } 102 // TODO(msw): Remove unnecessary API.
103 size_t GetSwitchCount() const;
101 104
102 // Get a copy of all switches, along with their values 105 // Get a copy of all switches, along with their values.
103 const SwitchMap& GetSwitches() const { 106 const SwitchMap& GetSwitches() const { return switches_; }
104 return switches_;
105 }
106 107
107 // Get the remaining arguments to the command. 108 // Append a switch [with optional value] to the command line.
108 const std::vector<StringType>& args() const { return args_; } 109 // CAUTION! Appending a switch after the "--" switch terminator is futile!
109
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)
116 // Returns the original command line string as a vector of strings.
117 const std::vector<std::string>& argv() const {
118 return argv_;
119 }
120 // Try to match the same result as command_line_string() would get you
121 // on windows.
122 std::string command_line_string() const;
123 #endif
124
125 // Returns the program part of the command line string (the first item).
126 FilePath GetProgram() const;
127
128 // Append a switch to the command line.
129 void AppendSwitch(const std::string& switch_string); 110 void AppendSwitch(const std::string& switch_string);
130
131 // Append a switch and value to the command line.
132 // CAUTION! Appending a switch after the "--" kSwitchTerminator is futile!
133 void AppendSwitchPath(const std::string& switch_string, const FilePath& path); 111 void AppendSwitchPath(const std::string& switch_string, const FilePath& path);
134 void AppendSwitchNative(const std::string& switch_string, 112 void AppendSwitchNative(const std::string& switch_string,
135 const StringType& value); 113 const StringType& value);
136 void AppendSwitchASCII(const std::string& switch_string, 114 void AppendSwitchASCII(const std::string& switch_string,
137 const std::string& value); 115 const std::string& value);
138 void AppendSwitches(const CommandLine& other); 116 void AppendSwitches(const CommandLine& other);
139 117
140 // Append an argument to the command line. 118 // Copy a set of switches (and any values) from another command line.
141 // Note on quoting: the argument will be quoted properly such that it is 119 // Commonly used when launching a subprocess.
142 // interpreted as one argument to the target command. 120 void CopySwitchesFrom(const CommandLine& source, const char* const switches[],
143 // AppendArg is primarily for ASCII; non-ASCII input will be 121 size_t count);
144 // interpreted as UTF-8. 122
123 // Get the remaining arguments to the command.
124 const StringVector& args() const { return args_; }
125
126 // Append an argument to the command line. Note that the argument is quoted
127 // properly such that it is interpreted as one argument to the target command.
128 // AppendArg is primarily for ASCII; non-ASCII input is interpreted as UTF-8.
145 void AppendArg(const std::string& value); 129 void AppendArg(const std::string& value);
146 void AppendArgPath(const FilePath& value); 130 void AppendArgPath(const FilePath& value);
147 void AppendArgNative(const StringType& value); 131 void AppendArgNative(const StringType& value);
148 void AppendArgs(const CommandLine& other); 132 void AppendArgs(const CommandLine& other);
149 133
150 // Append the arguments from another command line to this one. 134 // Append the arguments from another command line to this one.
151 // If |include_program| is true, include |other|'s program as well. 135 // If |include_program| is true, include |other|'s program as well.
152 void AppendArguments(const CommandLine& other, 136 void AppendArguments(const CommandLine& other,
153 bool include_program); 137 bool include_program);
154 138
155 // Insert a command before the current command. Common for debuggers, 139 // Insert a command before the current command.
156 // like "valgrind" or "gdb --args". 140 // Common for debuggers, like "valgrind" or "gdb --args".
157 void PrependWrapper(const StringType& wrapper); 141 void PrependWrapper(const StringType& wrapper);
158 142
159 // Copy a set of switches (and their values, if any) from another command 143 #if defined(OS_WIN)
160 // line. Commonly used when launching a subprocess. 144 // Initialize by parsing the given command line string.
161 void CopySwitchesFrom(const CommandLine& source, const char* const switches[], 145 // The program name is assumed to be the first item in the string.
162 size_t count); 146 void ParseFromString(const std::wstring& command_line);
147 #endif
163 148
164 private: 149 private:
165 friend class InProcessBrowserTest; 150 // Disallow default constructor; a program name must be explicitly specified.
151 CommandLine() {}
166 152
167 CommandLine(); 153 // The singleton CommandLine representing the current process's command line.
168
169 // Used by InProcessBrowserTest.
170 static CommandLine* ForCurrentProcessMutable();
171
172 // Returns true and fills in |switch_string| and |switch_value|
173 // if |parameter_string| represents a switch.
174 static bool IsSwitch(const StringType& parameter_string,
175 std::string* switch_string,
176 StringType* switch_value);
177
178 // The singleton CommandLine instance representing the current process's
179 // command line.
180 static CommandLine* current_process_commandline_; 154 static CommandLine* current_process_commandline_;
181 155
182 // We store a platform-native version of the command line, used when building 156 // We store a platform-native version of the command line, used when building
183 // up a new command line to be executed. This ifdef delimits that code. 157 // up a new command line to be executed. This ifdef delimits that code.
184
185 #if defined(OS_WIN) 158 #if defined(OS_WIN)
186 // The quoted, space-separated command-line string. 159 // The quoted, space-separated command line string.
187 std::wstring command_line_string_; 160 StringType command_line_string_;
188 // The name of the program. 161 // The name of the program.
189 std::wstring program_; 162 StringType program_;
190 #elif defined(OS_POSIX) 163 #elif defined(OS_POSIX)
191 // The argv array, with the program name in argv_[0]. 164 // The argv array, with the program name in argv_[0].
192 std::vector<std::string> argv_; 165 StringVector argv_;
193 #endif 166 #endif
194 167
195 // Parsed-out values. 168 // Parsed-out switch keys and values.
196 SwitchMap switches_; 169 SwitchMap switches_;
197 170
198 // Non-switch command-line arguments. 171 // Non-switch command line arguments.
199 std::vector<StringType> args_; 172 StringVector args_;
200 173
201 // We allow copy constructors, because a common pattern is to grab a 174 // Allow the copy constructor. A common pattern is to copy the current
202 // copy of the current process's command line and then add some 175 // process's command line and then add some flags to it. For example:
203 // flags to it. E.g.:
204 // CommandLine cl(*CommandLine::ForCurrentProcess()); 176 // CommandLine cl(*CommandLine::ForCurrentProcess());
205 // cl.AppendSwitch(...); 177 // cl.AppendSwitch(...);
206 }; 178 };
207 179
208 #endif // BASE_COMMAND_LINE_H_ 180 #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