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 class works with command lines: building and parsing. |
6 // elements of a command line in a relatively lightweight manner. | |
7 // Switches can optionally have a value attached using an equals sign, | 6 // Switches can optionally have a value attached using an equals sign, |
8 // as in "-switch=value". Arguments that aren't prefixed with a | 7 // as in "-switch=value". Arguments that aren't prefixed with a |
9 // switch prefix are considered "loose parameters". Switch names | 8 // switch prefix are considered "loose parameters". Switch names are |
10 // are case-insensitive. An argument of "--" will terminate switch parsing, | 9 // case-insensitive. An argument of "--" will terminate switch |
11 // causing everything after to be considered as loose parameters. | 10 // parsing, causing everything after to be considered as loose |
| 11 // parameters. |
| 12 |
| 13 // There is a singleton read-only CommandLine that represents the command |
| 14 // line that the current process was started with. It must be initialized |
| 15 // in main() (or whatever the platform's equivalent function is). |
12 | 16 |
13 #ifndef BASE_COMMAND_LINE_H_ | 17 #ifndef BASE_COMMAND_LINE_H_ |
14 #define BASE_COMMAND_LINE_H_ | 18 #define BASE_COMMAND_LINE_H_ |
15 | 19 |
| 20 #include "build/build_config.h" |
| 21 |
16 #include <map> | 22 #include <map> |
17 #include <string> | 23 #include <string> |
18 #include <vector> | 24 #include <vector> |
19 | 25 |
20 #include "base/basictypes.h" | 26 #include "base/basictypes.h" |
| 27 #include "base/logging.h" |
21 #include "base/scoped_ptr.h" | 28 #include "base/scoped_ptr.h" |
22 | 29 |
23 class CommandLine { | 30 class CommandLine { |
24 public: | 31 public: |
25 // Creates a parsed version of the command line used to launch | |
26 // the current process. | |
27 CommandLine(); | |
28 | |
29 #if defined(OS_WIN) | 32 #if defined(OS_WIN) |
30 // Creates a parsed version of the given command-line string. | 33 // Creates a parsed version of the given command-line string. |
31 // The program name is assumed to be the first item in the string. | 34 // The program name is assumed to be the first item in the string. |
32 CommandLine(const std::wstring& command_line); | 35 void ParseFromString(const std::wstring& command_line); |
33 #elif defined(OS_POSIX) | 36 #elif defined(OS_POSIX) |
| 37 // Initialize from an argv vector (or directly from main()'s argv). |
34 CommandLine(int argc, const char* const* argv); | 38 CommandLine(int argc, const char* const* argv); |
35 CommandLine(const std::vector<std::string>& argv); | 39 explicit CommandLine(const std::vector<std::string>& argv); |
36 #endif | 40 #endif |
37 | 41 |
38 ~CommandLine(); | 42 // Construct a new, empty command line. |
| 43 // |program| is the name of the program to run (aka argv[0]). |
| 44 // TODO(port): should be a FilePath. |
| 45 explicit CommandLine(const std::wstring& program); |
39 | 46 |
40 // On non-Windows platforms, main() must call SetArgcArgv() before accessing | 47 // Initialize the current process CommandLine singleton. On Windows, |
41 // any members of this class. | 48 // ignores its arguments (we instead parse GetCommandLineW() |
42 // On Windows, this call is a no-op (we instead parse GetCommandLineW() | 49 // directly) because we don't trust the CRT's parsing of the command |
43 // directly) because we don't trust the CRT's parsing of the command line. | 50 // line, but it still must be called to set up the command line. |
44 static void SetArgcArgv(int argc, const char* const* argv); | 51 static void Init(int argc, const char* const* argv); |
| 52 |
| 53 // Get the singleton CommandLine representing the current process's |
| 54 // command line. |
| 55 static const CommandLine* ForCurrentProcess() { |
| 56 DCHECK(current_process_commandline_); |
| 57 return current_process_commandline_; |
| 58 } |
45 | 59 |
46 // Returns true if this command line contains the given switch. | 60 // Returns true if this command line contains the given switch. |
47 // (Switch names are case-insensitive.) | 61 // (Switch names are case-insensitive.) |
48 bool HasSwitch(const std::wstring& switch_string) const; | 62 bool HasSwitch(const std::wstring& switch_string) const; |
49 | 63 |
50 // Returns the value associated with the given switch. If the | 64 // Returns the value associated with the given switch. If the |
51 // switch has no value or isn't present, this method returns | 65 // switch has no value or isn't present, this method returns |
52 // the empty string. | 66 // the empty string. |
53 std::wstring GetSwitchValue(const std::wstring& switch_string) const; | 67 std::wstring GetSwitchValue(const std::wstring& switch_string) const; |
54 | 68 |
55 // Returns the number of "loose values" found in the command line. | 69 // Get the remaining arguments to the command. |
56 // Loose values are arguments that aren't switches. | 70 // WARNING: this is incorrect on POSIX; we must do string conversions. |
57 // (The program name is also excluded from the set of loose values.) | 71 std::vector<std::wstring> GetLooseValues() const; |
58 size_t GetLooseValueCount() const; | |
59 | 72 |
60 typedef std::vector<std::wstring>::const_iterator LooseValueIterator; | 73 #if defined(OS_WIN) |
61 | 74 // Returns the original command line string. |
62 // Returns a const_iterator to the list of loose values. | 75 const std::wstring& command_line_string() const { |
63 LooseValueIterator GetLooseValuesBegin() const; | 76 return command_line_string_; |
64 | 77 } |
65 // Returns the end const_iterator for the list of loose values. | 78 #elif defined(OS_POSIX) |
66 LooseValueIterator GetLooseValuesEnd() const; | |
67 | |
68 // Simply returns the original command line string. | |
69 std::wstring command_line_string() const; | |
70 | |
71 #if defined(OS_POSIX) | |
72 // Returns the original command line string as a vector of strings. | 79 // Returns the original command line string as a vector of strings. |
73 const std::vector<std::string>& argv() const; | 80 const std::vector<std::string>& argv() const { |
| 81 return argv_; |
| 82 } |
74 #endif | 83 #endif |
75 | 84 |
76 // Returns the program part of the command line string (the first item). | 85 // Returns the program part of the command line string (the first item). |
77 std::wstring program() const; | 86 std::wstring program() const; |
78 | 87 |
79 // An array containing the prefixes that identify an argument as | |
80 // a switch. | |
81 static const wchar_t* const kSwitchPrefixes[]; | |
82 | |
83 // The string that's used to separate switches from their values. | |
84 static const wchar_t kSwitchValueSeparator[]; | |
85 | |
86 // Treat everything after this argument as loose parameters. | |
87 static const wchar_t kSwitchTerminator[]; | |
88 | |
89 // Return a copy of the string prefixed with a switch prefix. | 88 // Return a copy of the string prefixed with a switch prefix. |
90 // Used internally. | 89 // Used internally. |
91 static std::wstring PrefixedSwitchString(const std::wstring& switch_string); | 90 static std::wstring PrefixedSwitchString(const std::wstring& switch_string); |
92 | 91 |
93 // Return a copy of the string prefixed with a switch prefix, | 92 // Return a copy of the string prefixed with a switch prefix, |
94 // and appended with the given value. Used internally. | 93 // and appended with the given value. Used internally. |
95 static std::wstring PrefixedSwitchStringWithValue( | 94 static std::wstring PrefixedSwitchStringWithValue( |
96 const std::wstring& switch_string, | 95 const std::wstring& switch_string, |
97 const std::wstring& value_string); | 96 const std::wstring& value_string); |
98 | 97 |
99 // Appends the given switch string (preceded by a space and a switch | 98 // Appends the given switch string (preceded by a space and a switch |
100 // prefix) to the given string. | 99 // prefix) to the given string. |
101 static void AppendSwitch(std::wstring* command_line_string, | 100 void AppendSwitch(const std::wstring& switch_string); |
102 const std::wstring& switch_string); | |
103 | 101 |
104 // Appends the given switch string (preceded by a space and a switch | 102 // Appends the given switch string (preceded by a space and a switch |
105 // prefix) to the given string, with the given value attached. | 103 // prefix) to the given string, with the given value attached. |
106 static void AppendSwitchWithValue(std::wstring* command_line_string, | 104 void AppendSwitchWithValue(const std::wstring& switch_string, |
107 const std::wstring& switch_string, | 105 const std::wstring& value_string); |
108 const std::wstring& value_string); | 106 |
| 107 // Append a loose value to the command line. |
| 108 void AppendLooseValue(const std::wstring& value); |
| 109 |
| 110 // Append the arguments from another command line to this one. |
| 111 // If |include_program| is true, include |other|'s program as well. |
| 112 void AppendArguments(const CommandLine& other, |
| 113 bool include_program); |
109 | 114 |
110 private: | 115 private: |
111 class Data; | 116 CommandLine() {} |
112 | 117 |
113 // True if we are responsible for deleting our |data_| pointer. In some cases | 118 // The singleton CommandLine instance representing the current process's |
114 // we cache the result of parsing the command line and |data_|'s lifetime is | 119 // command line. |
115 // managed by someone else (e.g., the |Singleton| class). | 120 static CommandLine* current_process_commandline_; |
116 bool we_own_data_; | |
117 | 121 |
118 // A pointer to the parsed version of the command line. | 122 // We store a platform-native version of the command line, used when building |
119 Data* data_; | 123 // up a new command line to be executed. This ifdef delimits that code. |
120 | 124 |
121 DISALLOW_EVIL_CONSTRUCTORS(CommandLine); | 125 #if defined(OS_WIN) |
| 126 // The quoted, space-separated command-line string. |
| 127 std::wstring command_line_string_; |
| 128 |
| 129 // The name of the program. |
| 130 std::wstring program_; |
| 131 |
| 132 // The type of native command line arguments. |
| 133 typedef std::wstring StringType; |
| 134 |
| 135 #elif defined(OS_POSIX) |
| 136 // The argv array, with the program name in argv_[0]. |
| 137 std::vector<std::string> argv_; |
| 138 |
| 139 // The type of native command line arguments. |
| 140 typedef std::string StringType; |
| 141 |
| 142 // Shared by the two POSIX constructor forms. Initalize from argv_. |
| 143 void InitFromArgv(); |
| 144 #endif |
| 145 |
| 146 // Returns true and fills in |switch_string| and |switch_value| |
| 147 // if |parameter_string| represents a switch. |
| 148 static bool IsSwitch(const StringType& parameter_string, |
| 149 std::string* switch_string, |
| 150 StringType* switch_value); |
| 151 |
| 152 // Parsed-out values. |
| 153 std::map<std::string, StringType> switches_; |
| 154 |
| 155 // Non-switch command-line arguments. |
| 156 std::vector<StringType> loose_values_; |
| 157 |
| 158 // We allow copy constructors, because a common pattern is to grab a |
| 159 // copy of the current process's command line and then add some |
| 160 // flags to it. E.g.: |
| 161 // CommandLine cl(*CommandLine::ForCurrentProcess()); |
| 162 // cl.AppendSwitch(...); |
122 }; | 163 }; |
123 | 164 |
124 #endif // BASE_COMMAND_LINE_H_ | 165 #endif // BASE_COMMAND_LINE_H_ |
OLD | NEW |