blob: 7ef83bc171f686dc4a83930681c1a17433349266 [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]2e4c50c2010-07-21 15:57:238// switch prefix are saved as extra arguments. An argument of "--"
9// will terminate switch parsing, causing everything after to be
10// considered as extra arguments.
[email protected]bb975362009-01-21 01:00:2211
12// There is a singleton read-only CommandLine that represents the command
13// line that the current process was started with. It must be initialized
14// in main() (or whatever the platform's equivalent function is).
initial.commitd7cae122008-07-26 21:49:3815
[email protected]02c87962008-10-06 10:25:3516#ifndef BASE_COMMAND_LINE_H_
17#define BASE_COMMAND_LINE_H_
[email protected]32b76ef2010-07-26 23:08:2418#pragma once
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]bb975362009-01-21 01:00:2227#include "base/logging.h"
initial.commitd7cae122008-07-26 21:49:3828
[email protected]5d91c9e2010-07-28 17:25:2829class FilePath;
[email protected]d4515eb2009-01-30 00:40:4330class InProcessBrowserTest;
31
initial.commitd7cae122008-07-26 21:49:3832class CommandLine {
33 public:
[email protected]51343d5a2009-10-26 22:39:3334 // A constructor for CommandLines that are used only to carry arguments.
35 enum ArgumentsOnly { ARGUMENTS_ONLY };
36 explicit CommandLine(ArgumentsOnly args_only);
[email protected]3a3d47472010-07-15 21:03:5437 ~CommandLine();
[email protected]51343d5a2009-10-26 22:39:3338
[email protected]f3adb5c2008-08-07 20:07:3239#if defined(OS_WIN)
[email protected]bd48c2b02010-04-09 20:32:4240 // The type of native command line arguments.
41 typedef std::wstring StringType;
42
[email protected]0189bbd2009-10-12 22:50:3943 // Initialize by parsing the given command-line string.
[email protected]bb975362009-01-21 01:00:2244 // The program name is assumed to be the first item in the string.
45 void ParseFromString(const std::wstring& command_line);
[email protected]5d91c9e2010-07-28 17:25:2846 static CommandLine FromString(const std::wstring& command_line);
[email protected]f3adb5c2008-08-07 20:07:3247#elif defined(OS_POSIX)
[email protected]bd48c2b02010-04-09 20:32:4248 // The type of native command line arguments.
49 typedef std::string StringType;
50
[email protected]0189bbd2009-10-12 22:50:3951 // Initialize from an argv vector.
52 void InitFromArgv(int argc, const char* const* argv);
53 void InitFromArgv(const std::vector<std::string>& argv);
54
[email protected]5d91c9e2010-07-28 17:25:2855 CommandLine(int argc, const char* const* argv);
56 explicit CommandLine(const std::vector<std::string>& argv);
[email protected]f3adb5c2008-08-07 20:07:3257#endif
initial.commitd7cae122008-07-26 21:49:3858
[email protected]bb975362009-01-21 01:00:2259 // Construct a new, empty command line.
60 // |program| is the name of the program to run (aka argv[0]).
[email protected]8f681e42009-10-09 20:37:5661 explicit CommandLine(const FilePath& program);
62
[email protected]bb975362009-01-21 01:00:2263 // Initialize the current process CommandLine singleton. On Windows,
64 // ignores its arguments (we instead parse GetCommandLineW()
65 // directly) because we don't trust the CRT's parsing of the command
66 // line, but it still must be called to set up the command line.
67 static void Init(int argc, const char* const* argv);
68
[email protected]e43eddf12009-12-29 00:32:5269#if defined(OS_POSIX) && !defined(OS_MACOSX)
[email protected]7f113f32009-09-10 18:02:1770 // Sets the current process' arguments that show in "ps" etc. to those
71 // in |current_process_commandline_|. Used by the zygote host so that
72 // renderers show up with --type=renderer.
73 static void SetProcTitle();
[email protected]7f113f32009-09-10 18:02:1774#endif
75
[email protected]a2318cda2009-02-25 21:13:5376 // Destroys the current process CommandLine singleton. This is necessary if
77 // you want to reset the base library to its initial state (for example in an
78 // outer library that needs to be able to terminate, and be re-initialized).
[email protected]02fb75ab2009-10-12 16:11:4079 // If Init is called only once, e.g. in main(), calling Reset() is not
[email protected]a2318cda2009-02-25 21:13:5380 // necessary.
[email protected]02fb75ab2009-10-12 16:11:4081 static void Reset();
82 // The same function snuck into this class under two different names;
83 // this one remains for backwards compat with the older o3d build.
84 static void Terminate() { Reset(); }
[email protected]a2318cda2009-02-25 21:13:5385
[email protected]bb975362009-01-21 01:00:2286 // Get the singleton CommandLine representing the current process's
[email protected]0189bbd2009-10-12 22:50:3987 // command line. Note: returned value is mutable, but not thread safe;
88 // only mutate if you know what you're doing!
89 static CommandLine* ForCurrentProcess() {
[email protected]bb975362009-01-21 01:00:2290 DCHECK(current_process_commandline_);
91 return current_process_commandline_;
92 }
[email protected]1a48f312008-08-12 01:14:3793
initial.commitd7cae122008-07-26 21:49:3894 // Returns true if this command line contains the given switch.
95 // (Switch names are case-insensitive.)
[email protected]b7e0a2a2009-10-13 02:07:2596 bool HasSwitch(const std::string& switch_string) const;
97
98 // Deprecated version of the above.
[email protected]5d91c9e2010-07-28 17:25:2899 bool HasSwitch(const std::wstring& switch_string) const;
initial.commitd7cae122008-07-26 21:49:38100
101 // Returns the value associated with the given switch. If the
102 // switch has no value or isn't present, this method returns
103 // the empty string.
[email protected]0380184c2009-10-13 20:36:21104 // TODO(evanm): move these into command_line.cpp once we've fixed the
105 // wstringness.
[email protected]5d91c9e2010-07-28 17:25:28106 std::string GetSwitchValueASCII(const std::string& switch_string) const;
107 FilePath GetSwitchValuePath(const std::string& switch_string) const;
[email protected]b7e0a2a2009-10-13 02:07:25108
[email protected]0380184c2009-10-13 20:36:21109 // Deprecated versions of the above.
110 std::wstring GetSwitchValue(const std::string& switch_string) const;
[email protected]5d91c9e2010-07-28 17:25:28111 std::wstring GetSwitchValue(const std::wstring& switch_string) const;
initial.commitd7cae122008-07-26 21:49:38112
[email protected]92745242009-06-12 16:52:21113 // Get the number of switches in this process.
114 size_t GetSwitchCount() const { return switches_.size(); }
115
[email protected]f6c34832010-05-24 10:39:59116 // The type of map for parsed-out switch key and values.
117 typedef std::map<std::string, StringType> SwitchMap;
118
[email protected]bd48c2b02010-04-09 20:32:42119 // Get a copy of all switches, along with their values
[email protected]5d91c9e2010-07-28 17:25:28120 const SwitchMap& GetSwitches() const {
[email protected]bd48c2b02010-04-09 20:32:42121 return switches_;
122 }
123
[email protected]bb975362009-01-21 01:00:22124 // Get the remaining arguments to the command.
[email protected]2e4c50c2010-07-21 15:57:23125 const std::vector<StringType>& args() const { return args_; }
initial.commitd7cae122008-07-26 21:49:38126
[email protected]bb975362009-01-21 01:00:22127#if defined(OS_WIN)
128 // Returns the original command line string.
129 const std::wstring& command_line_string() const {
130 return command_line_string_;
131 }
132#elif defined(OS_POSIX)
[email protected]10e42bf2008-10-15 21:59:08133 // Returns the original command line string as a vector of strings.
[email protected]bb975362009-01-21 01:00:22134 const std::vector<std::string>& argv() const {
135 return argv_;
136 }
[email protected]d3b04ab2010-05-21 17:14:57137 // Try to match the same result as command_line_string() would get you
138 // on windows.
139 std::string command_line_string() const;
[email protected]10e42bf2008-10-15 21:59:08140#endif
141
initial.commitd7cae122008-07-26 21:49:38142 // Returns the program part of the command line string (the first item).
[email protected]5d91c9e2010-07-28 17:25:28143 FilePath GetProgram() const;
[email protected]0189bbd2009-10-12 22:50:39144
145 // Returns the program part of the command line string (the first item).
146 // Deprecated version of the above.
initial.commitd7cae122008-07-26 21:49:38147 std::wstring program() const;
148
[email protected]10e42bf2008-10-15 21:59:08149 // Return a copy of the string prefixed with a switch prefix.
150 // Used internally.
[email protected]b7e0a2a2009-10-13 02:07:25151 static std::wstring PrefixedSwitchString(const std::string& switch_string);
[email protected]10e42bf2008-10-15 21:59:08152
153 // Return a copy of the string prefixed with a switch prefix,
154 // and appended with the given value. Used internally.
155 static std::wstring PrefixedSwitchStringWithValue(
[email protected]b7e0a2a2009-10-13 02:07:25156 const std::string& switch_string,
[email protected]10e42bf2008-10-15 21:59:08157 const std::wstring& value_string);
158
initial.commitd7cae122008-07-26 21:49:38159 // Appends the given switch string (preceded by a space and a switch
160 // prefix) to the given string.
[email protected]b7e0a2a2009-10-13 02:07:25161 void AppendSwitch(const std::string& switch_string);
initial.commitd7cae122008-07-26 21:49:38162
163 // Appends the given switch string (preceded by a space and a switch
164 // prefix) to the given string, with the given value attached.
[email protected]b7e0a2a2009-10-13 02:07:25165 void AppendSwitchWithValue(const std::string& switch_string,
[email protected]bb975362009-01-21 01:00:22166 const std::wstring& value_string);
[email protected]b7e0a2a2009-10-13 02:07:25167 void AppendSwitchWithValue(const std::string& switch_string,
[email protected]5d91c9e2010-07-28 17:25:28168 const std::string& value_string);
[email protected]bb975362009-01-21 01:00:22169
170 // Append a loose value to the command line.
171 void AppendLooseValue(const std::wstring& value);
172
173 // Append the arguments from another command line to this one.
174 // If |include_program| is true, include |other|'s program as well.
175 void AppendArguments(const CommandLine& other,
176 bool include_program);
initial.commitd7cae122008-07-26 21:49:38177
[email protected]052f1d482009-02-10 00:52:14178 // On POSIX systems it's common to run processes via a wrapper (like
[email protected]e5e19c32009-04-20 22:10:02179 // "valgrind" or "gdb --args").
[email protected]052f1d482009-02-10 00:52:14180 void PrependWrapper(const std::wstring& wrapper);
181
initial.commitd7cae122008-07-26 21:49:38182 private:
[email protected]d4515eb2009-01-30 00:40:43183 friend class InProcessBrowserTest;
184
[email protected]3a3d47472010-07-15 21:03:54185 CommandLine();
initial.commitd7cae122008-07-26 21:49:38186
[email protected]d4515eb2009-01-30 00:40:43187 // Used by InProcessBrowserTest.
188 static CommandLine* ForCurrentProcessMutable() {
189 DCHECK(current_process_commandline_);
190 return current_process_commandline_;
191 }
192
[email protected]bb975362009-01-21 01:00:22193 // The singleton CommandLine instance representing the current process's
194 // command line.
195 static CommandLine* current_process_commandline_;
initial.commitd7cae122008-07-26 21:49:38196
[email protected]bb975362009-01-21 01:00:22197 // We store a platform-native version of the command line, used when building
198 // up a new command line to be executed. This ifdef delimits that code.
199
200#if defined(OS_WIN)
201 // The quoted, space-separated command-line string.
202 std::wstring command_line_string_;
[email protected]bb975362009-01-21 01:00:22203 // The name of the program.
204 std::wstring program_;
[email protected]bb975362009-01-21 01:00:22205#elif defined(OS_POSIX)
206 // The argv array, with the program name in argv_[0].
207 std::vector<std::string> argv_;
[email protected]bb975362009-01-21 01:00:22208#endif
209
210 // Returns true and fills in |switch_string| and |switch_value|
211 // if |parameter_string| represents a switch.
212 static bool IsSwitch(const StringType& parameter_string,
213 std::string* switch_string,
214 StringType* switch_value);
215
216 // Parsed-out values.
[email protected]f6c34832010-05-24 10:39:59217 SwitchMap switches_;
[email protected]bb975362009-01-21 01:00:22218
219 // Non-switch command-line arguments.
[email protected]2e4c50c2010-07-21 15:57:23220 std::vector<StringType> args_;
[email protected]bb975362009-01-21 01:00:22221
222 // We allow copy constructors, because a common pattern is to grab a
223 // copy of the current process's command line and then add some
224 // flags to it. E.g.:
225 // CommandLine cl(*CommandLine::ForCurrentProcess());
226 // cl.AppendSwitch(...);
initial.commitd7cae122008-07-26 21:49:38227};
228
[email protected]02c87962008-10-06 10:25:35229#endif // BASE_COMMAND_LINE_H_