|
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 |
|
3 // found in the LICENSE file. |
|
4 |
|
5 // This class works with command lines: building and parsing. |
|
6 // Switches can optionally have a value attached using an equals sign, |
|
7 // as in "-switch=value". Arguments that aren't prefixed with a |
|
8 // switch prefix are considered "loose parameters". Switch names are |
|
9 // case-insensitive. An argument of "--" will terminate switch |
|
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). |
|
16 |
|
17 #ifndef BASE_COMMAND_LINE_H_ |
|
18 #define BASE_COMMAND_LINE_H_ |
|
19 |
|
20 #include "build/build_config.h" |
|
21 |
|
22 #include <map> |
|
23 #include <string> |
|
24 #include <vector> |
|
25 |
|
26 #include "base/basictypes.h" |
|
27 #include "base/logging.h" |
|
28 |
|
29 class InProcessBrowserTest; |
|
30 |
|
31 class CommandLine { |
|
32 public: |
|
33 #if defined(OS_WIN) |
|
34 // Creates a parsed version of the given command-line string. |
|
35 // The program name is assumed to be the first item in the string. |
|
36 void ParseFromString(const std::wstring& command_line); |
|
37 #elif defined(OS_POSIX) |
|
38 // Initialize from an argv vector (or directly from main()'s argv). |
|
39 CommandLine(int argc, const char* const* argv); |
|
40 explicit CommandLine(const std::vector<std::string>& argv); |
|
41 #endif |
|
42 |
|
43 // Construct a new, empty command line. |
|
44 // |program| is the name of the program to run (aka argv[0]). |
|
45 // TODO(port): should be a FilePath. |
|
46 explicit CommandLine(const std::wstring& program); |
|
47 |
|
48 // Initialize the current process CommandLine singleton. On Windows, |
|
49 // ignores its arguments (we instead parse GetCommandLineW() |
|
50 // directly) because we don't trust the CRT's parsing of the command |
|
51 // line, but it still must be called to set up the command line. |
|
52 static void Init(int argc, const char* const* argv); |
|
53 |
|
54 // Destroys the current process CommandLine singleton. This is necessary if |
|
55 // you want to reset the base library to its initial state (for example in an |
|
56 // outer library that needs to be able to terminate, and be re-initialized). |
|
57 // If Init is called only once, e.g. in main(), calling Terminate() is not |
|
58 // necessary. |
|
59 static void Terminate(); |
|
60 |
|
61 // Get the singleton CommandLine representing the current process's |
|
62 // command line. |
|
63 static const CommandLine* ForCurrentProcess() { |
|
64 DCHECK(current_process_commandline_); |
|
65 return current_process_commandline_; |
|
66 } |
|
67 |
|
68 static bool IsInitialized() { |
|
69 return !!current_process_commandline_; |
|
70 } |
|
71 |
|
72 // Returns true if this command line contains the given switch. |
|
73 // (Switch names are case-insensitive.) |
|
74 bool HasSwitch(const std::wstring& switch_string) const; |
|
75 |
|
76 // Returns the value associated with the given switch. If the |
|
77 // switch has no value or isn't present, this method returns |
|
78 // the empty string. |
|
79 std::wstring GetSwitchValue(const std::wstring& switch_string) const; |
|
80 |
|
81 // Get the remaining arguments to the command. |
|
82 // WARNING: this is incorrect on POSIX; we must do string conversions. |
|
83 std::vector<std::wstring> GetLooseValues() const; |
|
84 |
|
85 #if defined(OS_WIN) |
|
86 // Returns the original command line string. |
|
87 const std::wstring& command_line_string() const { |
|
88 return command_line_string_; |
|
89 } |
|
90 #elif defined(OS_POSIX) |
|
91 // Returns the original command line string as a vector of strings. |
|
92 const std::vector<std::string>& argv() const { |
|
93 return argv_; |
|
94 } |
|
95 #endif |
|
96 |
|
97 // Returns the program part of the command line string (the first item). |
|
98 std::wstring program() const; |
|
99 |
|
100 // Return a copy of the string prefixed with a switch prefix. |
|
101 // Used internally. |
|
102 static std::wstring PrefixedSwitchString(const std::wstring& switch_string); |
|
103 |
|
104 // Return a copy of the string prefixed with a switch prefix, |
|
105 // and appended with the given value. Used internally. |
|
106 static std::wstring PrefixedSwitchStringWithValue( |
|
107 const std::wstring& switch_string, |
|
108 const std::wstring& value_string); |
|
109 |
|
110 // Appends the given switch string (preceded by a space and a switch |
|
111 // prefix) to the given string. |
|
112 void AppendSwitch(const std::wstring& switch_string); |
|
113 |
|
114 // Appends the given switch string (preceded by a space and a switch |
|
115 // prefix) to the given string, with the given value attached. |
|
116 void AppendSwitchWithValue(const std::wstring& switch_string, |
|
117 const std::wstring& value_string); |
|
118 |
|
119 // Append a loose value to the command line. |
|
120 void AppendLooseValue(const std::wstring& value); |
|
121 |
|
122 // Append the arguments from another command line to this one. |
|
123 // If |include_program| is true, include |other|'s program as well. |
|
124 void AppendArguments(const CommandLine& other, |
|
125 bool include_program); |
|
126 |
|
127 // On POSIX systems it's common to run processes via a wrapper (like |
|
128 // "valgrind" or "gdb --args"). |
|
129 void PrependWrapper(const std::wstring& wrapper); |
|
130 |
|
131 private: |
|
132 friend class InProcessBrowserTest; |
|
133 |
|
134 CommandLine() {} |
|
135 |
|
136 // Used by InProcessBrowserTest. |
|
137 static CommandLine* ForCurrentProcessMutable() { |
|
138 DCHECK(current_process_commandline_); |
|
139 return current_process_commandline_; |
|
140 } |
|
141 |
|
142 // The singleton CommandLine instance representing the current process's |
|
143 // command line. |
|
144 static CommandLine* current_process_commandline_; |
|
145 |
|
146 // We store a platform-native version of the command line, used when building |
|
147 // up a new command line to be executed. This ifdef delimits that code. |
|
148 |
|
149 #if defined(OS_WIN) |
|
150 // The quoted, space-separated command-line string. |
|
151 std::wstring command_line_string_; |
|
152 |
|
153 // The name of the program. |
|
154 std::wstring program_; |
|
155 |
|
156 // The type of native command line arguments. |
|
157 typedef std::wstring StringType; |
|
158 |
|
159 #elif defined(OS_POSIX) |
|
160 // The argv array, with the program name in argv_[0]. |
|
161 std::vector<std::string> argv_; |
|
162 |
|
163 // The type of native command line arguments. |
|
164 typedef std::string StringType; |
|
165 |
|
166 // Shared by the two POSIX constructor forms. Initalize from argv_. |
|
167 void InitFromArgv(); |
|
168 #endif |
|
169 |
|
170 // Returns true and fills in |switch_string| and |switch_value| |
|
171 // if |parameter_string| represents a switch. |
|
172 static bool IsSwitch(const StringType& parameter_string, |
|
173 std::string* switch_string, |
|
174 StringType* switch_value); |
|
175 |
|
176 // Parsed-out values. |
|
177 std::map<std::string, StringType> switches_; |
|
178 |
|
179 // Non-switch command-line arguments. |
|
180 std::vector<StringType> loose_values_; |
|
181 |
|
182 // We allow copy constructors, because a common pattern is to grab a |
|
183 // copy of the current process's command line and then add some |
|
184 // flags to it. E.g.: |
|
185 // CommandLine cl(*CommandLine::ForCurrentProcess()); |
|
186 // cl.AppendSwitch(...); |
|
187 }; |
|
188 |
|
189 #endif // BASE_COMMAND_LINE_H_ |