OLD | NEW |
---|---|
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 file contains a class that can be used to extract the salient | 5 // This file contains a class that can be used to extract the salient |
6 // elements of a command line in a relatively lightweight manner. | 6 // elements of a command line in a relatively lightweight manner. |
7 // Switches can optionally have a value attached using an equals sign, | 7 // Switches can optionally have a value attached using an equals sign, |
8 // as in "-switch=value". Arguments that aren't prefixed with a | 8 // as in "-switch=value". Arguments that aren't prefixed with a |
9 // switch prefix are considered "loose parameters". Switch names | 9 // switch prefix are considered "loose parameters". Switch names |
10 // are case-insensitive. An argument of "--" will terminate switch parsing, | 10 // are case-insensitive. An argument of "--" will terminate switch parsing, |
(...skipping 14 matching lines...) Expand all Loading... | |
25 // Creates a parsed version of the command line used to launch | 25 // Creates a parsed version of the command line used to launch |
26 // the current process. | 26 // the current process. |
27 CommandLine(); | 27 CommandLine(); |
28 | 28 |
29 #if defined(OS_WIN) | 29 #if defined(OS_WIN) |
30 // Creates a parsed version of the given command-line string. | 30 // Creates a parsed version of the given command-line string. |
31 // The program name is assumed to be the first item in the string. | 31 // The program name is assumed to be the first item in the string. |
32 CommandLine(const std::wstring& command_line); | 32 CommandLine(const std::wstring& command_line); |
33 #elif defined(OS_POSIX) | 33 #elif defined(OS_POSIX) |
34 CommandLine(int argc, const char* const* argv); | 34 CommandLine(int argc, const char* const* argv); |
35 CommandLine(const std::vector<std::string>& argv); | |
35 #endif | 36 #endif |
36 | 37 |
37 ~CommandLine(); | 38 ~CommandLine(); |
38 | 39 |
39 // On non-Windows platforms, main() must call SetArgcArgv() before accessing | 40 // On non-Windows platforms, main() must call SetArgcArgv() before accessing |
40 // any members of this class. | 41 // any members of this class. |
41 // On Windows, this call is a no-op (we instead parse GetCommandLineW() | 42 // On Windows, this call is a no-op (we instead parse GetCommandLineW() |
42 // directly) because we don't trust the CRT's parsing of the command line. | 43 // directly) because we don't trust the CRT's parsing of the command line. |
43 static void SetArgcArgv(int argc, const char* const* argv); | 44 static void SetArgcArgv(int argc, const char* const* argv); |
44 | 45 |
(...skipping 15 matching lines...) Expand all Loading... | |
60 | 61 |
61 // Returns a const_iterator to the list of loose values. | 62 // Returns a const_iterator to the list of loose values. |
62 LooseValueIterator GetLooseValuesBegin() const; | 63 LooseValueIterator GetLooseValuesBegin() const; |
63 | 64 |
64 // Returns the end const_iterator for the list of loose values. | 65 // Returns the end const_iterator for the list of loose values. |
65 LooseValueIterator GetLooseValuesEnd() const; | 66 LooseValueIterator GetLooseValuesEnd() const; |
66 | 67 |
67 // Simply returns the original command line string. | 68 // Simply returns the original command line string. |
68 std::wstring command_line_string() const; | 69 std::wstring command_line_string() const; |
69 | 70 |
71 #if defined(OS_POSIX) | |
72 // Returns the original command line string as a vector of c-strings. | |
Evan Martin
2008/10/15 21:54:40
This comment is no longer accurate. ("c-strings"
| |
73 const std::vector<std::string>& argv() const; | |
74 #endif | |
75 | |
70 // Returns the program part of the command line string (the first item). | 76 // Returns the program part of the command line string (the first item). |
71 std::wstring program() const; | 77 std::wstring program() const; |
72 | 78 |
73 // An array containing the prefixes that identify an argument as | 79 // An array containing the prefixes that identify an argument as |
74 // a switch. | 80 // a switch. |
75 static const wchar_t* const kSwitchPrefixes[]; | 81 static const wchar_t* const kSwitchPrefixes[]; |
76 | 82 |
77 // The string that's used to separate switches from their values. | 83 // The string that's used to separate switches from their values. |
78 static const wchar_t kSwitchValueSeparator[]; | 84 static const wchar_t kSwitchValueSeparator[]; |
79 | 85 |
80 // Treat everything after this argument as loose parameters. | 86 // Treat everything after this argument as loose parameters. |
81 static const wchar_t kSwitchTerminator[]; | 87 static const wchar_t kSwitchTerminator[]; |
82 | 88 |
89 // Return a copy of the string prefixed with a switch prefix. | |
90 // Used internally. | |
91 static std::wstring PrefixedSwitchString(const std::wstring& switch_string); | |
92 | |
93 // Return a copy of the string prefixed with a switch prefix, | |
94 // and appended with the given value. Used internally. | |
95 static std::wstring PrefixedSwitchStringWithValue( | |
96 const std::wstring& switch_string, | |
97 const std::wstring& value_string); | |
98 | |
83 // Appends the given switch string (preceded by a space and a switch | 99 // Appends the given switch string (preceded by a space and a switch |
84 // prefix) to the given string. | 100 // prefix) to the given string. |
85 static void AppendSwitch(std::wstring* command_line_string, | 101 static void AppendSwitch(std::wstring* command_line_string, |
86 const std::wstring& switch_string); | 102 const std::wstring& switch_string); |
87 | 103 |
88 // Appends the given switch string (preceded by a space and a switch | 104 // Appends the given switch string (preceded by a space and a switch |
89 // prefix) to the given string, with the given value attached. | 105 // prefix) to the given string, with the given value attached. |
90 static void AppendSwitchWithValue(std::wstring* command_line_string, | 106 static void AppendSwitchWithValue(std::wstring* command_line_string, |
91 const std::wstring& switch_string, | 107 const std::wstring& switch_string, |
92 const std::wstring& value_string); | 108 const std::wstring& value_string); |
93 | 109 |
94 private: | 110 private: |
95 class Data; | 111 class Data; |
96 | 112 |
97 // True if we are responsible for deleting our |data_| pointer. In some cases | 113 // True if we are responsible for deleting our |data_| pointer. In some cases |
98 // we cache the result of parsing the command line and |data_|'s lifetime is | 114 // we cache the result of parsing the command line and |data_|'s lifetime is |
99 // managed by someone else (e.g., the |Singleton| class). | 115 // managed by someone else (e.g., the |Singleton| class). |
100 bool we_own_data_; | 116 bool we_own_data_; |
101 | 117 |
102 // A pointer to the parsed version of the command line. | 118 // A pointer to the parsed version of the command line. |
103 Data* data_; | 119 Data* data_; |
104 | 120 |
105 DISALLOW_EVIL_CONSTRUCTORS(CommandLine); | 121 DISALLOW_EVIL_CONSTRUCTORS(CommandLine); |
106 }; | 122 }; |
107 | 123 |
108 #endif // BASE_COMMAND_LINE_H_ | 124 #endif // BASE_COMMAND_LINE_H_ |
OLD | NEW |