OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // Arguments with prefixes ('--', '-', and on Windows, '/') are switches. | 6 // Arguments with prefixes ('--', '-', and on Windows, '/') are switches. |
7 // Switches will precede all other arguments without switch prefixes. | 7 // Switches will precede all other arguments without switch prefixes. |
8 // Switches can optionally have values, delimited by '=', e.g., "-switch=value". | 8 // Switches can optionally have values, delimited by '=', e.g., "-switch=value". |
9 // An argument of "--" will terminate switch parsing during initialization, | 9 // An argument of "--" will terminate switch parsing during initialization, |
10 // interpreting subsequent tokens as non-switch arguments, regardless of prefix. | 10 // interpreting subsequent tokens as non-switch arguments, regardless of prefix. |
11 | 11 |
12 // There is a singleton read-only CommandLine that represents the command line | 12 // There is a singleton read-only CommandLine that represents the command line |
13 // that the current process was started with. It must be initialized in main(). | 13 // that the current process was started with. It must be initialized in main(). |
14 | 14 |
15 #ifndef BASE_COMMAND_LINE_H_ | 15 #ifndef BASE_COMMAND_LINE_H_ |
16 #define BASE_COMMAND_LINE_H_ | 16 #define BASE_COMMAND_LINE_H_ |
17 | 17 |
18 #include <stddef.h> | 18 #include <stddef.h> |
19 #include <map> | 19 #include <map> |
20 #include <string> | 20 #include <string> |
21 #include <vector> | 21 #include <vector> |
22 | 22 |
23 #include "base/base_export.h" | 23 #include "base/base_export.h" |
24 #include "build/build_config.h" | 24 #include "build/build_config.h" |
25 | 25 |
26 namespace base { | 26 namespace base { |
| 27 |
27 class FilePath; | 28 class FilePath; |
28 } | |
29 | 29 |
30 class BASE_EXPORT CommandLine { | 30 class BASE_EXPORT CommandLine { |
31 public: | 31 public: |
32 #if defined(OS_WIN) | 32 #if defined(OS_WIN) |
33 // The native command line string type. | 33 // The native command line string type. |
34 typedef std::wstring StringType; | 34 typedef std::wstring StringType; |
35 #elif defined(OS_POSIX) | 35 #elif defined(OS_POSIX) |
36 typedef std::string StringType; | 36 typedef std::string StringType; |
37 #endif | 37 #endif |
38 | 38 |
39 typedef StringType::value_type CharType; | 39 typedef StringType::value_type CharType; |
40 typedef std::vector<StringType> StringVector; | 40 typedef std::vector<StringType> StringVector; |
41 typedef std::map<std::string, StringType> SwitchMap; | 41 typedef std::map<std::string, StringType> SwitchMap; |
42 | 42 |
43 // A constructor for CommandLines that only carry switches and arguments. | 43 // A constructor for CommandLines that only carry switches and arguments. |
44 enum NoProgram { NO_PROGRAM }; | 44 enum NoProgram { NO_PROGRAM }; |
45 explicit CommandLine(NoProgram no_program); | 45 explicit CommandLine(NoProgram no_program); |
46 | 46 |
47 // Construct a new command line with |program| as argv[0]. | 47 // Construct a new command line with |program| as argv[0]. |
48 explicit CommandLine(const base::FilePath& program); | 48 explicit CommandLine(const FilePath& program); |
49 | 49 |
50 // Construct a new command line from an argument list. | 50 // Construct a new command line from an argument list. |
51 CommandLine(int argc, const CharType* const* argv); | 51 CommandLine(int argc, const CharType* const* argv); |
52 explicit CommandLine(const StringVector& argv); | 52 explicit CommandLine(const StringVector& argv); |
53 | 53 |
54 ~CommandLine(); | 54 ~CommandLine(); |
55 | 55 |
56 #if defined(OS_WIN) | 56 #if defined(OS_WIN) |
57 // By default this class will treat command-line arguments beginning with | 57 // By default this class will treat command-line arguments beginning with |
58 // slashes as switches on Windows, but not other platforms. | 58 // slashes as switches on Windows, but not other platforms. |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 | 101 |
102 // Constructs and returns the represented arguments string. | 102 // Constructs and returns the represented arguments string. |
103 // CAUTION! This should be avoided on POSIX because quoting behavior is | 103 // CAUTION! This should be avoided on POSIX because quoting behavior is |
104 // unclear. | 104 // unclear. |
105 StringType GetArgumentsString() const; | 105 StringType GetArgumentsString() const; |
106 | 106 |
107 // Returns the original command line string as a vector of strings. | 107 // Returns the original command line string as a vector of strings. |
108 const StringVector& argv() const { return argv_; } | 108 const StringVector& argv() const { return argv_; } |
109 | 109 |
110 // Get and Set the program part of the command line string (the first item). | 110 // Get and Set the program part of the command line string (the first item). |
111 base::FilePath GetProgram() const; | 111 FilePath GetProgram() const; |
112 void SetProgram(const base::FilePath& program); | 112 void SetProgram(const FilePath& program); |
113 | 113 |
114 // Returns true if this command line contains the given switch. | 114 // Returns true if this command line contains the given switch. |
115 // (Switch names are case-insensitive). | 115 // (Switch names are case-insensitive). |
116 bool HasSwitch(const std::string& switch_string) const; | 116 bool HasSwitch(const std::string& switch_string) const; |
117 | 117 |
118 // Returns the value associated with the given switch. If the switch has no | 118 // Returns the value associated with the given switch. If the switch has no |
119 // value or isn't present, this method returns the empty string. | 119 // value or isn't present, this method returns the empty string. |
120 std::string GetSwitchValueASCII(const std::string& switch_string) const; | 120 std::string GetSwitchValueASCII(const std::string& switch_string) const; |
121 base::FilePath GetSwitchValuePath(const std::string& switch_string) const; | 121 FilePath GetSwitchValuePath(const std::string& switch_string) const; |
122 StringType GetSwitchValueNative(const std::string& switch_string) const; | 122 StringType GetSwitchValueNative(const std::string& switch_string) const; |
123 | 123 |
124 // Get a copy of all switches, along with their values. | 124 // Get a copy of all switches, along with their values. |
125 const SwitchMap& GetSwitches() const { return switches_; } | 125 const SwitchMap& GetSwitches() const { return switches_; } |
126 | 126 |
127 // Append a switch [with optional value] to the command line. | 127 // Append a switch [with optional value] to the command line. |
128 // Note: Switches will precede arguments regardless of appending order. | 128 // Note: Switches will precede arguments regardless of appending order. |
129 void AppendSwitch(const std::string& switch_string); | 129 void AppendSwitch(const std::string& switch_string); |
130 void AppendSwitchPath(const std::string& switch_string, | 130 void AppendSwitchPath(const std::string& switch_string, |
131 const base::FilePath& path); | 131 const FilePath& path); |
132 void AppendSwitchNative(const std::string& switch_string, | 132 void AppendSwitchNative(const std::string& switch_string, |
133 const StringType& value); | 133 const StringType& value); |
134 void AppendSwitchASCII(const std::string& switch_string, | 134 void AppendSwitchASCII(const std::string& switch_string, |
135 const std::string& value); | 135 const std::string& value); |
136 | 136 |
137 // Copy a set of switches (and any values) from another command line. | 137 // Copy a set of switches (and any values) from another command line. |
138 // Commonly used when launching a subprocess. | 138 // Commonly used when launching a subprocess. |
139 void CopySwitchesFrom(const CommandLine& source, | 139 void CopySwitchesFrom(const CommandLine& source, |
140 const char* const switches[], | 140 const char* const switches[], |
141 size_t count); | 141 size_t count); |
142 | 142 |
143 // Get the remaining arguments to the command. | 143 // Get the remaining arguments to the command. |
144 StringVector GetArgs() const; | 144 StringVector GetArgs() const; |
145 | 145 |
146 // Append an argument to the command line. Note that the argument is quoted | 146 // Append an argument to the command line. Note that the argument is quoted |
147 // properly such that it is interpreted as one argument to the target command. | 147 // properly such that it is interpreted as one argument to the target command. |
148 // AppendArg is primarily for ASCII; non-ASCII input is interpreted as UTF-8. | 148 // AppendArg is primarily for ASCII; non-ASCII input is interpreted as UTF-8. |
149 // Note: Switches will precede arguments regardless of appending order. | 149 // Note: Switches will precede arguments regardless of appending order. |
150 void AppendArg(const std::string& value); | 150 void AppendArg(const std::string& value); |
151 void AppendArgPath(const base::FilePath& value); | 151 void AppendArgPath(const FilePath& value); |
152 void AppendArgNative(const StringType& value); | 152 void AppendArgNative(const StringType& value); |
153 | 153 |
154 // Append the switches and arguments from another command line to this one. | 154 // Append the switches and arguments from another command line to this one. |
155 // If |include_program| is true, include |other|'s program as well. | 155 // If |include_program| is true, include |other|'s program as well. |
156 void AppendArguments(const CommandLine& other, bool include_program); | 156 void AppendArguments(const CommandLine& other, bool include_program); |
157 | 157 |
158 // Insert a command before the current command. | 158 // Insert a command before the current command. |
159 // Common for debuggers, like "valgrind" or "gdb --args". | 159 // Common for debuggers, like "valgrind" or "gdb --args". |
160 void PrependWrapper(const StringType& wrapper); | 160 void PrependWrapper(const StringType& wrapper); |
161 | 161 |
(...skipping 17 matching lines...) Expand all Loading... |
179 // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* } | 179 // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* } |
180 StringVector argv_; | 180 StringVector argv_; |
181 | 181 |
182 // Parsed-out switch keys and values. | 182 // Parsed-out switch keys and values. |
183 SwitchMap switches_; | 183 SwitchMap switches_; |
184 | 184 |
185 // The index after the program and switches, any arguments start here. | 185 // The index after the program and switches, any arguments start here. |
186 size_t begin_args_; | 186 size_t begin_args_; |
187 }; | 187 }; |
188 | 188 |
| 189 } // namespace base |
| 190 |
| 191 // TODO(brettw) remove once all callers specify the namespace properly. |
| 192 using base::CommandLine; |
| 193 |
189 #endif // BASE_COMMAND_LINE_H_ | 194 #endif // BASE_COMMAND_LINE_H_ |
OLD | NEW |