blob: 78adb4675916d22c7715ad18dcfc95f650a89ffe [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// 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.
[email protected]02c87962008-10-06 10:25:354
[email protected]bb975362009-01-21 01:00:225// This class works with command lines: building and parsing.
initial.commitd7cae122008-07-26 21:49:386// Switches can optionally have a value attached using an equals sign,
7// as in "-switch=value". Arguments that aren't prefixed with a
[email protected]bb975362009-01-21 01:00:228// 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).
initial.commitd7cae122008-07-26 21:49:3816
[email protected]02c87962008-10-06 10:25:3517#ifndef BASE_COMMAND_LINE_H_
18#define BASE_COMMAND_LINE_H_
initial.commitd7cae122008-07-26 21:49:3819
[email protected]bb975362009-01-21 01:00:2220#include "build/build_config.h"
21
initial.commitd7cae122008-07-26 21:49:3822#include <map>
23#include <string>
24#include <vector>
25
26#include "base/basictypes.h"
[email protected]0189bbd2009-10-12 22:50:3927#include "base/file_path.h"
[email protected]bb975362009-01-21 01:00:2228#include "base/logging.h"
[email protected]b7e0a2a2009-10-13 02:07:2529#include "base/string_util.h"
initial.commitd7cae122008-07-26 21:49:3830
[email protected]d4515eb2009-01-30 00:40:4331class InProcessBrowserTest;
32
initial.commitd7cae122008-07-26 21:49:3833class CommandLine {
34 public:
[email protected]f3adb5c2008-08-07 20:07:3235#if defined(OS_WIN)
[email protected]0189bbd2009-10-12 22:50:3936 // Initialize by parsing the given command-line string.
[email protected]bb975362009-01-21 01:00:2237 // The program name is assumed to be the first item in the string.
38 void ParseFromString(const std::wstring& command_line);
[email protected]f3adb5c2008-08-07 20:07:3239#elif defined(OS_POSIX)
[email protected]0189bbd2009-10-12 22:50:3940 // Initialize from an argv vector.
41 void InitFromArgv(int argc, const char* const* argv);
42 void InitFromArgv(const std::vector<std::string>& argv);
43
44 CommandLine(int argc, const char* const* argv) {
45 InitFromArgv(argc, argv);
46 }
47 explicit CommandLine(const std::vector<std::string>& argv) {
48 InitFromArgv(argv);
49 }
[email protected]f3adb5c2008-08-07 20:07:3250#endif
initial.commitd7cae122008-07-26 21:49:3851
[email protected]bb975362009-01-21 01:00:2252 // Construct a new, empty command line.
53 // |program| is the name of the program to run (aka argv[0]).
[email protected]8f681e42009-10-09 20:37:5654 explicit CommandLine(const FilePath& program);
55
56 // Deprecated in favor of FilePath version.
[email protected]bb975362009-01-21 01:00:2257 explicit CommandLine(const std::wstring& program);
initial.commitd7cae122008-07-26 21:49:3858
[email protected]bb975362009-01-21 01:00:2259 // Initialize the current process CommandLine singleton. On Windows,
60 // ignores its arguments (we instead parse GetCommandLineW()
61 // directly) because we don't trust the CRT's parsing of the command
62 // line, but it still must be called to set up the command line.
63 static void Init(int argc, const char* const* argv);
64
[email protected]7f113f32009-09-10 18:02:1765#if defined(OS_LINUX) || defined(OS_FREEBSD)
66 // Sets the current process' arguments that show in "ps" etc. to those
67 // in |current_process_commandline_|. Used by the zygote host so that
68 // renderers show up with --type=renderer.
69 static void SetProcTitle();
[email protected]7f113f32009-09-10 18:02:1770#endif
71
[email protected]a2318cda2009-02-25 21:13:5372 // Destroys the current process CommandLine singleton. This is necessary if
73 // you want to reset the base library to its initial state (for example in an
74 // outer library that needs to be able to terminate, and be re-initialized).
[email protected]02fb75ab2009-10-12 16:11:4075 // If Init is called only once, e.g. in main(), calling Reset() is not
[email protected]a2318cda2009-02-25 21:13:5376 // necessary.
[email protected]02fb75ab2009-10-12 16:11:4077 static void Reset();
78 // The same function snuck into this class under two different names;
79 // this one remains for backwards compat with the older o3d build.
80 static void Terminate() { Reset(); }
[email protected]a2318cda2009-02-25 21:13:5381
[email protected]bb975362009-01-21 01:00:2282 // Get the singleton CommandLine representing the current process's
[email protected]0189bbd2009-10-12 22:50:3983 // command line. Note: returned value is mutable, but not thread safe;
84 // only mutate if you know what you're doing!
85 static CommandLine* ForCurrentProcess() {
[email protected]bb975362009-01-21 01:00:2286 DCHECK(current_process_commandline_);
87 return current_process_commandline_;
88 }
[email protected]1a48f312008-08-12 01:14:3789
initial.commitd7cae122008-07-26 21:49:3890 // Returns true if this command line contains the given switch.
91 // (Switch names are case-insensitive.)
[email protected]b7e0a2a2009-10-13 02:07:2592 bool HasSwitch(const std::string& switch_string) const;
93
94 // Deprecated version of the above.
95 bool HasSwitch(const std::wstring& switch_string) const {
96 return HasSwitch(WideToASCII(switch_string));
97 }
initial.commitd7cae122008-07-26 21:49:3898
99 // Returns the value associated with the given switch. If the
100 // switch has no value or isn't present, this method returns
101 // the empty string.
[email protected]b7e0a2a2009-10-13 02:07:25102 std::wstring GetSwitchValue(const std::string& switch_string) const;
103 std::string GetSwitchValueASCII(const std::string& switch_string) const {
104 return WideToASCII(GetSwitchValue(switch_string));
105 }
106
107 // Deprecated version of the above.
108 std::wstring GetSwitchValue(const std::wstring& switch_string) const {
109 return GetSwitchValue(WideToASCII(switch_string));
110 }
initial.commitd7cae122008-07-26 21:49:38111
[email protected]92745242009-06-12 16:52:21112 // Get the number of switches in this process.
113 size_t GetSwitchCount() const { return switches_.size(); }
114
[email protected]bb975362009-01-21 01:00:22115 // Get the remaining arguments to the command.
116 // WARNING: this is incorrect on POSIX; we must do string conversions.
117 std::vector<std::wstring> GetLooseValues() const;
initial.commitd7cae122008-07-26 21:49:38118
[email protected]bb975362009-01-21 01:00:22119#if defined(OS_WIN)
120 // Returns the original command line string.
121 const std::wstring& command_line_string() const {
122 return command_line_string_;
123 }
124#elif defined(OS_POSIX)
[email protected]10e42bf2008-10-15 21:59:08125 // Returns the original command line string as a vector of strings.
[email protected]bb975362009-01-21 01:00:22126 const std::vector<std::string>& argv() const {
127 return argv_;
128 }
[email protected]10e42bf2008-10-15 21:59:08129#endif
130
initial.commitd7cae122008-07-26 21:49:38131 // Returns the program part of the command line string (the first item).
[email protected]0189bbd2009-10-12 22:50:39132 FilePath GetProgram() const {
133 return FilePath::FromWStringHack(program());
134 }
135
136 // Returns the program part of the command line string (the first item).
137 // Deprecated version of the above.
initial.commitd7cae122008-07-26 21:49:38138 std::wstring program() const;
139
[email protected]10e42bf2008-10-15 21:59:08140 // Return a copy of the string prefixed with a switch prefix.
141 // Used internally.
[email protected]b7e0a2a2009-10-13 02:07:25142 static std::wstring PrefixedSwitchString(const std::string& switch_string);
[email protected]10e42bf2008-10-15 21:59:08143
144 // Return a copy of the string prefixed with a switch prefix,
145 // and appended with the given value. Used internally.
146 static std::wstring PrefixedSwitchStringWithValue(
[email protected]b7e0a2a2009-10-13 02:07:25147 const std::string& switch_string,
[email protected]10e42bf2008-10-15 21:59:08148 const std::wstring& value_string);
149
initial.commitd7cae122008-07-26 21:49:38150 // Appends the given switch string (preceded by a space and a switch
151 // prefix) to the given string.
[email protected]b7e0a2a2009-10-13 02:07:25152 void AppendSwitch(const std::string& switch_string);
initial.commitd7cae122008-07-26 21:49:38153
154 // Appends the given switch string (preceded by a space and a switch
155 // prefix) to the given string, with the given value attached.
[email protected]b7e0a2a2009-10-13 02:07:25156 void AppendSwitchWithValue(const std::string& switch_string,
[email protected]bb975362009-01-21 01:00:22157 const std::wstring& value_string);
[email protected]b7e0a2a2009-10-13 02:07:25158 void AppendSwitchWithValue(const std::string& switch_string,
159 const std::string& value_string) {
160 AppendSwitchWithValue(switch_string, ASCIIToWide(value_string));
161 }
[email protected]bb975362009-01-21 01:00:22162
163 // Append a loose value to the command line.
164 void AppendLooseValue(const std::wstring& value);
165
166 // Append the arguments from another command line to this one.
167 // If |include_program| is true, include |other|'s program as well.
168 void AppendArguments(const CommandLine& other,
169 bool include_program);
initial.commitd7cae122008-07-26 21:49:38170
[email protected]052f1d482009-02-10 00:52:14171 // On POSIX systems it's common to run processes via a wrapper (like
[email protected]e5e19c32009-04-20 22:10:02172 // "valgrind" or "gdb --args").
[email protected]052f1d482009-02-10 00:52:14173 void PrependWrapper(const std::wstring& wrapper);
174
initial.commitd7cae122008-07-26 21:49:38175 private:
[email protected]d4515eb2009-01-30 00:40:43176 friend class InProcessBrowserTest;
177
[email protected]bb975362009-01-21 01:00:22178 CommandLine() {}
initial.commitd7cae122008-07-26 21:49:38179
[email protected]d4515eb2009-01-30 00:40:43180 // Used by InProcessBrowserTest.
181 static CommandLine* ForCurrentProcessMutable() {
182 DCHECK(current_process_commandline_);
183 return current_process_commandline_;
184 }
185
[email protected]bb975362009-01-21 01:00:22186 // The singleton CommandLine instance representing the current process's
187 // command line.
188 static CommandLine* current_process_commandline_;
initial.commitd7cae122008-07-26 21:49:38189
[email protected]bb975362009-01-21 01:00:22190 // We store a platform-native version of the command line, used when building
191 // up a new command line to be executed. This ifdef delimits that code.
192
193#if defined(OS_WIN)
194 // The quoted, space-separated command-line string.
195 std::wstring command_line_string_;
196
197 // The name of the program.
198 std::wstring program_;
199
200 // The type of native command line arguments.
201 typedef std::wstring StringType;
202
203#elif defined(OS_POSIX)
204 // The argv array, with the program name in argv_[0].
205 std::vector<std::string> argv_;
206
207 // The type of native command line arguments.
208 typedef std::string StringType;
[email protected]bb975362009-01-21 01:00:22209#endif
210
211 // Returns true and fills in |switch_string| and |switch_value|
212 // if |parameter_string| represents a switch.
213 static bool IsSwitch(const StringType& parameter_string,
214 std::string* switch_string,
215 StringType* switch_value);
216
217 // Parsed-out values.
218 std::map<std::string, StringType> switches_;
219
220 // Non-switch command-line arguments.
221 std::vector<StringType> loose_values_;
222
223 // We allow copy constructors, because a common pattern is to grab a
224 // copy of the current process's command line and then add some
225 // flags to it. E.g.:
226 // CommandLine cl(*CommandLine::ForCurrentProcess());
227 // cl.AppendSwitch(...);
initial.commitd7cae122008-07-26 21:49:38228};
229
[email protected]02c87962008-10-06 10:25:35230#endif // BASE_COMMAND_LINE_H_