blob: 6b7d1f4d4ad8a9e139dd532c0100a15b497a0efd [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"
initial.commitd7cae122008-07-26 21:49:3829
[email protected]d4515eb2009-01-30 00:40:4330class InProcessBrowserTest;
31
initial.commitd7cae122008-07-26 21:49:3832class CommandLine {
33 public:
[email protected]f3adb5c2008-08-07 20:07:3234#if defined(OS_WIN)
[email protected]0189bbd2009-10-12 22:50:3935 // Initialize by parsing the given command-line string.
[email protected]bb975362009-01-21 01:00:2236 // The program name is assumed to be the first item in the string.
37 void ParseFromString(const std::wstring& command_line);
[email protected]f3adb5c2008-08-07 20:07:3238#elif defined(OS_POSIX)
[email protected]0189bbd2009-10-12 22:50:3939 // Initialize from an argv vector.
40 void InitFromArgv(int argc, const char* const* argv);
41 void InitFromArgv(const std::vector<std::string>& argv);
42
43 CommandLine(int argc, const char* const* argv) {
44 InitFromArgv(argc, argv);
45 }
46 explicit CommandLine(const std::vector<std::string>& argv) {
47 InitFromArgv(argv);
48 }
[email protected]f3adb5c2008-08-07 20:07:3249#endif
initial.commitd7cae122008-07-26 21:49:3850
[email protected]bb975362009-01-21 01:00:2251 // Construct a new, empty command line.
52 // |program| is the name of the program to run (aka argv[0]).
[email protected]8f681e42009-10-09 20:37:5653 explicit CommandLine(const FilePath& program);
54
55 // Deprecated in favor of FilePath version.
[email protected]bb975362009-01-21 01:00:2256 explicit CommandLine(const std::wstring& program);
initial.commitd7cae122008-07-26 21:49:3857
[email protected]bb975362009-01-21 01:00:2258 // Initialize the current process CommandLine singleton. On Windows,
59 // ignores its arguments (we instead parse GetCommandLineW()
60 // directly) because we don't trust the CRT's parsing of the command
61 // line, but it still must be called to set up the command line.
62 static void Init(int argc, const char* const* argv);
63
[email protected]7f113f32009-09-10 18:02:1764#if defined(OS_LINUX) || defined(OS_FREEBSD)
65 // Sets the current process' arguments that show in "ps" etc. to those
66 // in |current_process_commandline_|. Used by the zygote host so that
67 // renderers show up with --type=renderer.
68 static void SetProcTitle();
69
70 // Needed to support SetProcTitle() on Linux. Should be called by main().
71 static void SetTrueArgv(char** argv);
72#endif
73
[email protected]a2318cda2009-02-25 21:13:5374 // Destroys the current process CommandLine singleton. This is necessary if
75 // you want to reset the base library to its initial state (for example in an
76 // outer library that needs to be able to terminate, and be re-initialized).
[email protected]02fb75ab2009-10-12 16:11:4077 // If Init is called only once, e.g. in main(), calling Reset() is not
[email protected]a2318cda2009-02-25 21:13:5378 // necessary.
[email protected]02fb75ab2009-10-12 16:11:4079 static void Reset();
80 // The same function snuck into this class under two different names;
81 // this one remains for backwards compat with the older o3d build.
82 static void Terminate() { Reset(); }
[email protected]a2318cda2009-02-25 21:13:5383
[email protected]bb975362009-01-21 01:00:2284 // Get the singleton CommandLine representing the current process's
[email protected]0189bbd2009-10-12 22:50:3985 // command line. Note: returned value is mutable, but not thread safe;
86 // only mutate if you know what you're doing!
87 static CommandLine* ForCurrentProcess() {
[email protected]bb975362009-01-21 01:00:2288 DCHECK(current_process_commandline_);
89 return current_process_commandline_;
90 }
[email protected]1a48f312008-08-12 01:14:3791
initial.commitd7cae122008-07-26 21:49:3892 // Returns true if this command line contains the given switch.
93 // (Switch names are case-insensitive.)
94 bool HasSwitch(const std::wstring& switch_string) const;
95
96 // Returns the value associated with the given switch. If the
97 // switch has no value or isn't present, this method returns
98 // the empty string.
99 std::wstring GetSwitchValue(const std::wstring& switch_string) const;
100
[email protected]92745242009-06-12 16:52:21101 // Get the number of switches in this process.
102 size_t GetSwitchCount() const { return switches_.size(); }
103
[email protected]bb975362009-01-21 01:00:22104 // Get the remaining arguments to the command.
105 // WARNING: this is incorrect on POSIX; we must do string conversions.
106 std::vector<std::wstring> GetLooseValues() const;
initial.commitd7cae122008-07-26 21:49:38107
[email protected]bb975362009-01-21 01:00:22108#if defined(OS_WIN)
109 // Returns the original command line string.
110 const std::wstring& command_line_string() const {
111 return command_line_string_;
112 }
113#elif defined(OS_POSIX)
[email protected]10e42bf2008-10-15 21:59:08114 // Returns the original command line string as a vector of strings.
[email protected]bb975362009-01-21 01:00:22115 const std::vector<std::string>& argv() const {
116 return argv_;
117 }
[email protected]10e42bf2008-10-15 21:59:08118#endif
119
initial.commitd7cae122008-07-26 21:49:38120 // Returns the program part of the command line string (the first item).
[email protected]0189bbd2009-10-12 22:50:39121 FilePath GetProgram() const {
122 return FilePath::FromWStringHack(program());
123 }
124
125 // Returns the program part of the command line string (the first item).
126 // Deprecated version of the above.
initial.commitd7cae122008-07-26 21:49:38127 std::wstring program() const;
128
[email protected]10e42bf2008-10-15 21:59:08129 // Return a copy of the string prefixed with a switch prefix.
130 // Used internally.
131 static std::wstring PrefixedSwitchString(const std::wstring& switch_string);
132
133 // Return a copy of the string prefixed with a switch prefix,
134 // and appended with the given value. Used internally.
135 static std::wstring PrefixedSwitchStringWithValue(
136 const std::wstring& switch_string,
137 const std::wstring& value_string);
138
initial.commitd7cae122008-07-26 21:49:38139 // Appends the given switch string (preceded by a space and a switch
140 // prefix) to the given string.
[email protected]bb975362009-01-21 01:00:22141 void AppendSwitch(const std::wstring& switch_string);
initial.commitd7cae122008-07-26 21:49:38142
143 // Appends the given switch string (preceded by a space and a switch
144 // prefix) to the given string, with the given value attached.
[email protected]bb975362009-01-21 01:00:22145 void AppendSwitchWithValue(const std::wstring& switch_string,
146 const std::wstring& value_string);
147
148 // Append a loose value to the command line.
149 void AppendLooseValue(const std::wstring& value);
150
151 // Append the arguments from another command line to this one.
152 // If |include_program| is true, include |other|'s program as well.
153 void AppendArguments(const CommandLine& other,
154 bool include_program);
initial.commitd7cae122008-07-26 21:49:38155
[email protected]052f1d482009-02-10 00:52:14156 // On POSIX systems it's common to run processes via a wrapper (like
[email protected]e5e19c32009-04-20 22:10:02157 // "valgrind" or "gdb --args").
[email protected]052f1d482009-02-10 00:52:14158 void PrependWrapper(const std::wstring& wrapper);
159
initial.commitd7cae122008-07-26 21:49:38160 private:
[email protected]d4515eb2009-01-30 00:40:43161 friend class InProcessBrowserTest;
162
[email protected]bb975362009-01-21 01:00:22163 CommandLine() {}
initial.commitd7cae122008-07-26 21:49:38164
[email protected]d4515eb2009-01-30 00:40:43165 // Used by InProcessBrowserTest.
166 static CommandLine* ForCurrentProcessMutable() {
167 DCHECK(current_process_commandline_);
168 return current_process_commandline_;
169 }
170
[email protected]bb975362009-01-21 01:00:22171 // The singleton CommandLine instance representing the current process's
172 // command line.
173 static CommandLine* current_process_commandline_;
initial.commitd7cae122008-07-26 21:49:38174
[email protected]bb975362009-01-21 01:00:22175 // We store a platform-native version of the command line, used when building
176 // up a new command line to be executed. This ifdef delimits that code.
177
178#if defined(OS_WIN)
179 // The quoted, space-separated command-line string.
180 std::wstring command_line_string_;
181
182 // The name of the program.
183 std::wstring program_;
184
185 // The type of native command line arguments.
186 typedef std::wstring StringType;
187
188#elif defined(OS_POSIX)
189 // The argv array, with the program name in argv_[0].
190 std::vector<std::string> argv_;
191
192 // The type of native command line arguments.
193 typedef std::string StringType;
[email protected]bb975362009-01-21 01:00:22194#endif
195
196 // Returns true and fills in |switch_string| and |switch_value|
197 // if |parameter_string| represents a switch.
198 static bool IsSwitch(const StringType& parameter_string,
199 std::string* switch_string,
200 StringType* switch_value);
201
202 // Parsed-out values.
203 std::map<std::string, StringType> switches_;
204
205 // Non-switch command-line arguments.
206 std::vector<StringType> loose_values_;
207
208 // We allow copy constructors, because a common pattern is to grab a
209 // copy of the current process's command line and then add some
210 // flags to it. E.g.:
211 // CommandLine cl(*CommandLine::ForCurrentProcess());
212 // cl.AppendSwitch(...);
initial.commitd7cae122008-07-26 21:49:38213};
214
[email protected]02c87962008-10-06 10:25:35215#endif // BASE_COMMAND_LINE_H_