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