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 class FilePath; | 27 class FilePath; |
| 28 } |
27 | 29 |
28 class BASE_EXPORT CommandLine { | 30 class BASE_EXPORT CommandLine { |
29 public: | 31 public: |
30 #if defined(OS_WIN) | 32 #if defined(OS_WIN) |
31 // The native command line string type. | 33 // The native command line string type. |
32 typedef std::wstring StringType; | 34 typedef std::wstring StringType; |
33 #elif defined(OS_POSIX) | 35 #elif defined(OS_POSIX) |
34 typedef std::string StringType; | 36 typedef std::string StringType; |
35 #endif | 37 #endif |
36 | 38 |
37 typedef StringType::value_type CharType; | 39 typedef StringType::value_type CharType; |
38 typedef std::vector<StringType> StringVector; | 40 typedef std::vector<StringType> StringVector; |
39 typedef std::map<std::string, StringType> SwitchMap; | 41 typedef std::map<std::string, StringType> SwitchMap; |
40 | 42 |
41 // A constructor for CommandLines that only carry switches and arguments. | 43 // A constructor for CommandLines that only carry switches and arguments. |
42 enum NoProgram { NO_PROGRAM }; | 44 enum NoProgram { NO_PROGRAM }; |
43 explicit CommandLine(NoProgram no_program); | 45 explicit CommandLine(NoProgram no_program); |
44 | 46 |
45 // Construct a new command line with |program| as argv[0]. | 47 // Construct a new command line with |program| as argv[0]. |
46 explicit CommandLine(const FilePath& program); | 48 explicit CommandLine(const base::FilePath& program); |
47 | 49 |
48 // Construct a new command line from an argument list. | 50 // Construct a new command line from an argument list. |
49 CommandLine(int argc, const CharType* const* argv); | 51 CommandLine(int argc, const CharType* const* argv); |
50 explicit CommandLine(const StringVector& argv); | 52 explicit CommandLine(const StringVector& argv); |
51 | 53 |
52 ~CommandLine(); | 54 ~CommandLine(); |
53 | 55 |
54 // Initialize the current process CommandLine singleton. On Windows, ignores | 56 // Initialize the current process CommandLine singleton. On Windows, ignores |
55 // its arguments (we instead parse GetCommandLineW() directly) because we | 57 // its arguments (we instead parse GetCommandLineW() directly) because we |
56 // don't trust the CRT's parsing of the command line, but it still must be | 58 // don't trust the CRT's parsing of the command line, but it still must be |
(...skipping 28 matching lines...) Expand all Loading... |
85 | 87 |
86 // Constructs and returns the represented arguments string. | 88 // Constructs and returns the represented arguments string. |
87 // CAUTION! This should be avoided on POSIX because quoting behavior is | 89 // CAUTION! This should be avoided on POSIX because quoting behavior is |
88 // unclear. | 90 // unclear. |
89 StringType GetArgumentsString() const; | 91 StringType GetArgumentsString() const; |
90 | 92 |
91 // Returns the original command line string as a vector of strings. | 93 // Returns the original command line string as a vector of strings. |
92 const StringVector& argv() const { return argv_; } | 94 const StringVector& argv() const { return argv_; } |
93 | 95 |
94 // Get and Set the program part of the command line string (the first item). | 96 // Get and Set the program part of the command line string (the first item). |
95 FilePath GetProgram() const; | 97 base::FilePath GetProgram() const; |
96 void SetProgram(const FilePath& program); | 98 void SetProgram(const base::FilePath& program); |
97 | 99 |
98 // Returns true if this command line contains the given switch. | 100 // Returns true if this command line contains the given switch. |
99 // (Switch names are case-insensitive). | 101 // (Switch names are case-insensitive). |
100 bool HasSwitch(const std::string& switch_string) const; | 102 bool HasSwitch(const std::string& switch_string) const; |
101 | 103 |
102 // Returns the value associated with the given switch. If the switch has no | 104 // Returns the value associated with the given switch. If the switch has no |
103 // value or isn't present, this method returns the empty string. | 105 // value or isn't present, this method returns the empty string. |
104 std::string GetSwitchValueASCII(const std::string& switch_string) const; | 106 std::string GetSwitchValueASCII(const std::string& switch_string) const; |
105 FilePath GetSwitchValuePath(const std::string& switch_string) const; | 107 base::FilePath GetSwitchValuePath(const std::string& switch_string) const; |
106 StringType GetSwitchValueNative(const std::string& switch_string) const; | 108 StringType GetSwitchValueNative(const std::string& switch_string) const; |
107 | 109 |
108 // Get a copy of all switches, along with their values. | 110 // Get a copy of all switches, along with their values. |
109 const SwitchMap& GetSwitches() const { return switches_; } | 111 const SwitchMap& GetSwitches() const { return switches_; } |
110 | 112 |
111 // Append a switch [with optional value] to the command line. | 113 // Append a switch [with optional value] to the command line. |
112 // Note: Switches will precede arguments regardless of appending order. | 114 // Note: Switches will precede arguments regardless of appending order. |
113 void AppendSwitch(const std::string& switch_string); | 115 void AppendSwitch(const std::string& switch_string); |
114 void AppendSwitchPath(const std::string& switch_string, const FilePath& path); | 116 void AppendSwitchPath(const std::string& switch_string, |
| 117 const base::FilePath& path); |
115 void AppendSwitchNative(const std::string& switch_string, | 118 void AppendSwitchNative(const std::string& switch_string, |
116 const StringType& value); | 119 const StringType& value); |
117 void AppendSwitchASCII(const std::string& switch_string, | 120 void AppendSwitchASCII(const std::string& switch_string, |
118 const std::string& value); | 121 const std::string& value); |
119 | 122 |
120 // Copy a set of switches (and any values) from another command line. | 123 // Copy a set of switches (and any values) from another command line. |
121 // Commonly used when launching a subprocess. | 124 // Commonly used when launching a subprocess. |
122 void CopySwitchesFrom(const CommandLine& source, | 125 void CopySwitchesFrom(const CommandLine& source, |
123 const char* const switches[], | 126 const char* const switches[], |
124 size_t count); | 127 size_t count); |
125 | 128 |
126 // Get the remaining arguments to the command. | 129 // Get the remaining arguments to the command. |
127 StringVector GetArgs() const; | 130 StringVector GetArgs() const; |
128 | 131 |
129 // Append an argument to the command line. Note that the argument is quoted | 132 // Append an argument to the command line. Note that the argument is quoted |
130 // properly such that it is interpreted as one argument to the target command. | 133 // properly such that it is interpreted as one argument to the target command. |
131 // AppendArg is primarily for ASCII; non-ASCII input is interpreted as UTF-8. | 134 // AppendArg is primarily for ASCII; non-ASCII input is interpreted as UTF-8. |
132 // Note: Switches will precede arguments regardless of appending order. | 135 // Note: Switches will precede arguments regardless of appending order. |
133 void AppendArg(const std::string& value); | 136 void AppendArg(const std::string& value); |
134 void AppendArgPath(const FilePath& value); | 137 void AppendArgPath(const base::FilePath& value); |
135 void AppendArgNative(const StringType& value); | 138 void AppendArgNative(const StringType& value); |
136 | 139 |
137 // Append the switches and arguments from another command line to this one. | 140 // Append the switches and arguments from another command line to this one. |
138 // If |include_program| is true, include |other|'s program as well. | 141 // If |include_program| is true, include |other|'s program as well. |
139 void AppendArguments(const CommandLine& other, bool include_program); | 142 void AppendArguments(const CommandLine& other, bool include_program); |
140 | 143 |
141 // Insert a command before the current command. | 144 // Insert a command before the current command. |
142 // Common for debuggers, like "valgrind" or "gdb --args". | 145 // Common for debuggers, like "valgrind" or "gdb --args". |
143 void PrependWrapper(const StringType& wrapper); | 146 void PrependWrapper(const StringType& wrapper); |
144 | 147 |
(...skipping 18 matching lines...) Expand all Loading... |
163 StringVector argv_; | 166 StringVector argv_; |
164 | 167 |
165 // Parsed-out switch keys and values. | 168 // Parsed-out switch keys and values. |
166 SwitchMap switches_; | 169 SwitchMap switches_; |
167 | 170 |
168 // The index after the program and switches, any arguments start here. | 171 // The index after the program and switches, any arguments start here. |
169 size_t begin_args_; | 172 size_t begin_args_; |
170 }; | 173 }; |
171 | 174 |
172 #endif // BASE_COMMAND_LINE_H_ | 175 #endif // BASE_COMMAND_LINE_H_ |
OLD | NEW |