OLD | NEW |
---|---|
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 |
25 using std::string; | |
evanm
2011/02/24 01:01:05
This is against Chrome style. Typing out std:: ev
msw
2011/05/10 23:18:43
Done.
| |
26 | |
27 class FilePath; | 27 class FilePath; |
28 class InProcessBrowserTest; | 28 class InProcessBrowserTest; |
29 | 29 |
30 class CommandLine { | 30 class CommandLine { |
31 public: | 31 public: |
32 // The native command line string type. | |
32 #if defined(OS_WIN) | 33 #if defined(OS_WIN) |
33 // The type of native command line arguments. | 34 typedef std::wstring StringType; |
evanm
2011/02/24 01:01:05
probably didn't mean to add that whitespace
msw
2011/05/10 23:18:43
Done.
| |
34 typedef std::wstring StringType; | |
35 #elif defined(OS_POSIX) | 35 #elif defined(OS_POSIX) |
36 // The type of native command line arguments. | 36 typedef string StringType; |
37 typedef std::string StringType; | |
38 #endif | 37 #endif |
38 typedef StringType::value_type CharType; | |
39 | 39 |
40 // The type of map for parsed-out switch key and values. | 40 typedef std::vector<StringType> StringVector; |
41 typedef std::map<std::string, StringType> SwitchMap; | 41 // The type of map for parsed-out switch keys and values. |
42 typedef std::map<string, StringType> SwitchMap; | |
42 | 43 |
43 // A constructor for CommandLines that are used only to carry switches and | 44 // Enum & constructor for CommandLines that only carry switches and arguments. |
44 // arguments. | |
45 enum NoProgram { NO_PROGRAM }; | 45 enum NoProgram { NO_PROGRAM }; |
46 explicit CommandLine(NoProgram no_program); | 46 explicit CommandLine(NoProgram no_program); |
47 | 47 |
48 // Construct a new, empty command line. | 48 // 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); | 49 explicit CommandLine(const FilePath& program); |
50 explicit CommandLine(int argc, const CharType* const* argv); | |
51 explicit CommandLine(const StringVector& argv); | |
51 | 52 |
52 #if defined(OS_POSIX) | 53 // Initialize the current process CommandLine singleton. On Windows, ignores |
53 CommandLine(int argc, const char* const* argv); | 54 // its arguments (we instead parse GetCommandLineW() directly) because we |
54 explicit CommandLine(const std::vector<std::string>& argv); | 55 // don't trust the CRT's parsing of the command-line, but it still must be |
55 #endif | 56 // called to set up the command line. |
56 | |
57 ~CommandLine(); | |
58 | |
59 #if defined(OS_WIN) | |
60 // Initialize by parsing the given command-line string. | |
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); | 57 static void Init(int argc, const char* const* argv); |
75 | 58 |
76 // Destroys the current process CommandLine singleton. This is necessary if | 59 // 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 | 60 // 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). | 61 // 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 | 62 // If Init is called only once, as in main(), Reset() is not necessary. |
80 // necessary. | |
81 static void Reset(); | 63 static void Reset(); |
82 | 64 |
83 // Get the singleton CommandLine representing the current process's | 65 // Get the singleton CommandLine representing the current process's |
84 // command line. Note: returned value is mutable, but not thread safe; | 66 // command line. Note: returned value is mutable, but not thread safe; |
85 // only mutate if you know what you're doing! | 67 // only mutate if you know what you're doing! |
86 static CommandLine* ForCurrentProcess(); | 68 static CommandLine* ForCurrentProcess(); |
87 | 69 |
70 #if defined(OS_WIN) | |
71 static CommandLine FromString(const StringType& command_line); | |
72 #endif | |
73 | |
74 // Initialize from an argv vector. | |
75 void InitFromArgv(int argc, const CharType* const* argv); | |
76 void InitFromArgv(const StringVector argv); | |
77 | |
88 // Returns true if this command line contains the given switch. | 78 // Returns true if this command line contains the given switch. |
89 // (Switch names are case-insensitive.) | 79 // (Switch names are case-insensitive). |
90 bool HasSwitch(const std::string& switch_string) const; | 80 bool HasSwitch(const string& switch_string) const; |
91 | 81 |
92 // Returns the value associated with the given switch. If the | 82 // 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 | 83 // value or isn't present, this method returns the empty string. |
94 // the empty string. | 84 string GetSwitchValueASCII(const string& switch_string) const; |
95 std::string GetSwitchValueASCII(const std::string& switch_string) const; | 85 FilePath GetSwitchValuePath(const string& switch_string) const; |
96 FilePath GetSwitchValuePath(const std::string& switch_string) const; | 86 StringType GetSwitchValueNative(const string& switch_string) const; |
97 StringType GetSwitchValueNative(const std::string& switch_string) const; | |
98 | 87 |
99 // Get the number of switches in this process. | 88 // Get the number of switches in this process. |
100 size_t GetSwitchCount() const { return switches_.size(); } | 89 size_t GetSwitchCount() const; |
101 | 90 |
102 // Get a copy of all switches, along with their values | 91 // Get a copy of all switches, along with their values. |
103 const SwitchMap& GetSwitches() const { | 92 const SwitchMap& GetSwitches() const { return switches_; } |
104 return switches_; | |
105 } | |
106 | 93 |
107 // Get the remaining arguments to the command. | 94 // Get the remaining arguments to the command. |
108 const std::vector<StringType>& args() const { return args_; } | 95 StringVector args() const; |
evanm
2011/02/24 01:01:05
I guess if it's not a simple getter it ought to be
msw
2011/05/10 23:18:43
Added TODO (to keep this change smaller).
| |
109 | 96 |
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. | 97 // Returns the original command line string as a vector of strings. |
117 const std::vector<std::string>& argv() const { | 98 const StringVector& argv() const { return argv_; } |
118 return argv_; | 99 |
119 } | 100 // Constructs and returns the represented command line string. |
120 // Try to match the same result as command_line_string() would get you | 101 StringType command_line_string() const; |
121 // on windows. | |
122 std::string command_line_string() const; | |
123 #endif | |
124 | 102 |
125 // Returns the program part of the command line string (the first item). | 103 // Returns the program part of the command line string (the first item). |
126 FilePath GetProgram() const; | 104 FilePath GetProgram() const; |
127 | 105 |
106 // Sets the program part of the command line string (the first item). | |
107 void SetProgram(const CommandLine::StringType& program); | |
108 | |
128 // Append a switch to the command line. | 109 // Append a switch to the command line. |
129 void AppendSwitch(const std::string& switch_string); | 110 void AppendSwitch(const string& switch_string); |
130 | 111 |
131 // Append a switch and value to the command line. | 112 // Append a switch and value to the command line. |
132 void AppendSwitchPath(const std::string& switch_string, const FilePath& path); | 113 void AppendSwitchPath(const string& switch_string, const FilePath& path); |
133 void AppendSwitchNative(const std::string& switch_string, | 114 void AppendSwitchNative(const string& switch_string, const StringType& value); |
134 const StringType& value); | 115 void AppendSwitchASCII(const string& switch_string, const string& value); |
135 void AppendSwitchASCII(const std::string& switch_string, | |
136 const std::string& value); | |
137 | 116 |
138 // Append an argument to the command line. | 117 void AppendSwitches(const CommandLine& other); |
139 // Note on quoting: the argument will be quoted properly such that it is | 118 |
140 // interpreted as one argument to the target command. | 119 // Append an argument to the command line. Note that the argument is quoted |
141 // AppendArg is primarily for ASCII; non-ASCII input will be | 120 // properly such that it is interpreted as one argument to the target command. |
142 // interpreted as UTF-8. | 121 // AppendArg is primarily for ASCII; non-ASCII input is interpreted as UTF-8. |
143 void AppendArg(const std::string& value); | 122 void AppendArg(const string& value); |
144 void AppendArgPath(const FilePath& value); | 123 void AppendArgPath(const FilePath& value); |
145 void AppendArgNative(const StringType& value); | 124 void AppendArgNative(const StringType& value); |
146 | 125 |
147 // Append the arguments from another command line to this one. | 126 // Append the arguments from another command line to this one. |
148 // If |include_program| is true, include |other|'s program as well. | 127 // If |include_program| is true, include |other|'s program as well. |
149 void AppendArguments(const CommandLine& other, | 128 void AppendArguments(const CommandLine& other, |
150 bool include_program); | 129 bool include_program); |
151 | 130 |
152 // Insert a command before the current command. Common for debuggers, | 131 // Insert a command before the current command. Common for debuggers, |
153 // like "valgrind" or "gdb --args". | 132 // like "valgrind" or "gdb --args". |
154 void PrependWrapper(const StringType& wrapper); | 133 void PrependWrapper(const StringType& wrapper); |
155 | 134 |
156 // Copy a set of switches (and their values, if any) from another command | 135 // Copy a set of switches (and their values, if any) from another command |
157 // line. Commonly used when launching a subprocess. | 136 // line. Commonly used when launching a subprocess. |
158 void CopySwitchesFrom(const CommandLine& source, const char* const switches[], | 137 void CopySwitchesFrom(const CommandLine& source, const char* const switches[], |
159 size_t count); | 138 size_t count); |
160 | 139 |
140 #if defined(OS_WIN) | |
141 // Initialize by parsing the given command-line string. | |
142 void ParseFromString(const StringType& command_line); | |
143 #endif | |
144 | |
161 private: | 145 private: |
162 friend class InProcessBrowserTest; | 146 friend class InProcessBrowserTest; |
Paweł Hajdan Jr.
2011/02/25 18:01:53
Bonus points for getting rid of that friend declar
msw
2011/05/10 23:18:43
Done in r76339.
| |
163 | 147 |
164 CommandLine(); | 148 // Disallow default constructor; a program name must be explicitly specified. |
149 CommandLine() { divider_ = 0; } | |
evanm
2011/02/24 01:01:05
Why provide an impl if it's not to be run?
msw
2011/05/10 23:18:43
Done, but it'd be cleaner to just replace the NO_P
| |
150 // Allow the copy constructor. A common pattern is to copy of the current | |
151 // process's command line and then add some flags to it. For example: | |
152 // CommandLine cl(*CommandLine::ForCurrentProcess()); | |
153 // cl.AppendSwitch(...); | |
165 | 154 |
166 // Used by InProcessBrowserTest. | 155 // The singleton CommandLine representing the current process's command-line. |
167 static CommandLine* ForCurrentProcessMutable(); | |
168 | |
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 | |
175 // The singleton CommandLine instance representing the current process's | |
176 // command line. | |
177 static CommandLine* current_process_commandline_; | 156 static CommandLine* current_process_commandline_; |
178 | 157 |
179 // We store a platform-native version of the command line, used when building | 158 // The argv array; program name is in argv_[0], then switches, then arguments. |
180 // up a new command line to be executed. This ifdef delimits that code. | 159 StringVector argv_; |
181 | 160 |
182 #if defined(OS_WIN) | 161 // An index dividing switches from arguments in |argv_|. |
evanm
2011/02/24 01:01:05
Might be clearer if you say what it is (the positi
msw
2011/05/10 23:18:43
Done.
| |
183 // The quoted, space-separated command-line string. | 162 size_t divider_; |
184 std::wstring command_line_string_; | |
185 // The name of the program. | |
186 std::wstring program_; | |
187 #elif defined(OS_POSIX) | |
188 // The argv array, with the program name in argv_[0]. | |
189 std::vector<std::string> argv_; | |
190 #endif | |
191 | 163 |
192 // Parsed-out values. | 164 // Parsed-out switch keys and values. |
193 SwitchMap switches_; | 165 SwitchMap switches_; |
194 | |
195 // Non-switch command-line arguments. | |
196 std::vector<StringType> args_; | |
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(...); | |
203 }; | 166 }; |
204 | 167 |
205 #endif // BASE_COMMAND_LINE_H_ | 168 #endif // BASE_COMMAND_LINE_H_ |
OLD | NEW |