blob: 8271206da97094281bba30d99715817ad02e280a [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]bb975362009-01-21 01:00:2227#include "base/logging.h"
initial.commitd7cae122008-07-26 21:49:3828
[email protected]d4515eb2009-01-30 00:40:4329class InProcessBrowserTest;
30
initial.commitd7cae122008-07-26 21:49:3831class CommandLine {
32 public:
[email protected]f3adb5c2008-08-07 20:07:3233#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:3834 // Creates a parsed version of the given command-line string.
[email protected]bb975362009-01-21 01:00:2235 // The program name is assumed to be the first item in the string.
36 void ParseFromString(const std::wstring& command_line);
[email protected]f3adb5c2008-08-07 20:07:3237#elif defined(OS_POSIX)
[email protected]bb975362009-01-21 01:00:2238 // Initialize from an argv vector (or directly from main()'s argv).
[email protected]e63d5982008-08-14 22:09:3939 CommandLine(int argc, const char* const* argv);
[email protected]bb975362009-01-21 01:00:2240 explicit CommandLine(const std::vector<std::string>& argv);
[email protected]f3adb5c2008-08-07 20:07:3241#endif
initial.commitd7cae122008-07-26 21:49:3842
[email protected]bb975362009-01-21 01:00:2243 // 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);
initial.commitd7cae122008-07-26 21:49:3847
[email protected]bb975362009-01-21 01:00:2248 // 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
[email protected]a2318cda2009-02-25 21:13:5354 // 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
[email protected]bb975362009-01-21 01:00:2261 // 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 }
[email protected]1a48f312008-08-12 01:14:3767
initial.commitd7cae122008-07-26 21:49:3868 // Returns true if this command line contains the given switch.
69 // (Switch names are case-insensitive.)
70 bool HasSwitch(const std::wstring& switch_string) const;
71
72 // Returns the value associated with the given switch. If the
73 // switch has no value or isn't present, this method returns
74 // the empty string.
75 std::wstring GetSwitchValue(const std::wstring& switch_string) const;
76
[email protected]bb975362009-01-21 01:00:2277 // Get the remaining arguments to the command.
78 // WARNING: this is incorrect on POSIX; we must do string conversions.
79 std::vector<std::wstring> GetLooseValues() const;
initial.commitd7cae122008-07-26 21:49:3880
[email protected]bb975362009-01-21 01:00:2281#if defined(OS_WIN)
82 // Returns the original command line string.
83 const std::wstring& command_line_string() const {
84 return command_line_string_;
85 }
86#elif defined(OS_POSIX)
[email protected]10e42bf2008-10-15 21:59:0887 // Returns the original command line string as a vector of strings.
[email protected]bb975362009-01-21 01:00:2288 const std::vector<std::string>& argv() const {
89 return argv_;
90 }
[email protected]10e42bf2008-10-15 21:59:0891#endif
92
initial.commitd7cae122008-07-26 21:49:3893 // Returns the program part of the command line string (the first item).
94 std::wstring program() const;
95
[email protected]10e42bf2008-10-15 21:59:0896 // Return a copy of the string prefixed with a switch prefix.
97 // Used internally.
98 static std::wstring PrefixedSwitchString(const std::wstring& switch_string);
99
100 // Return a copy of the string prefixed with a switch prefix,
101 // and appended with the given value. Used internally.
102 static std::wstring PrefixedSwitchStringWithValue(
103 const std::wstring& switch_string,
104 const std::wstring& value_string);
105
initial.commitd7cae122008-07-26 21:49:38106 // Appends the given switch string (preceded by a space and a switch
107 // prefix) to the given string.
[email protected]bb975362009-01-21 01:00:22108 void AppendSwitch(const std::wstring& switch_string);
initial.commitd7cae122008-07-26 21:49:38109
110 // Appends the given switch string (preceded by a space and a switch
111 // prefix) to the given string, with the given value attached.
[email protected]bb975362009-01-21 01:00:22112 void AppendSwitchWithValue(const std::wstring& switch_string,
113 const std::wstring& value_string);
114
115 // Append a loose value to the command line.
116 void AppendLooseValue(const std::wstring& value);
117
118 // Append the arguments from another command line to this one.
119 // If |include_program| is true, include |other|'s program as well.
120 void AppendArguments(const CommandLine& other,
121 bool include_program);
initial.commitd7cae122008-07-26 21:49:38122
[email protected]052f1d482009-02-10 00:52:14123 // On POSIX systems it's common to run processes via a wrapper (like
[email protected]e5e19c32009-04-20 22:10:02124 // "valgrind" or "gdb --args").
[email protected]052f1d482009-02-10 00:52:14125 void PrependWrapper(const std::wstring& wrapper);
126
initial.commitd7cae122008-07-26 21:49:38127 private:
[email protected]d4515eb2009-01-30 00:40:43128 friend class InProcessBrowserTest;
129
[email protected]bb975362009-01-21 01:00:22130 CommandLine() {}
initial.commitd7cae122008-07-26 21:49:38131
[email protected]d4515eb2009-01-30 00:40:43132 // Used by InProcessBrowserTest.
133 static CommandLine* ForCurrentProcessMutable() {
134 DCHECK(current_process_commandline_);
135 return current_process_commandline_;
136 }
137
[email protected]bb975362009-01-21 01:00:22138 // The singleton CommandLine instance representing the current process's
139 // command line.
140 static CommandLine* current_process_commandline_;
initial.commitd7cae122008-07-26 21:49:38141
[email protected]bb975362009-01-21 01:00:22142 // We store a platform-native version of the command line, used when building
143 // up a new command line to be executed. This ifdef delimits that code.
144
145#if defined(OS_WIN)
146 // The quoted, space-separated command-line string.
147 std::wstring command_line_string_;
148
149 // The name of the program.
150 std::wstring program_;
151
152 // The type of native command line arguments.
153 typedef std::wstring StringType;
154
155#elif defined(OS_POSIX)
156 // The argv array, with the program name in argv_[0].
157 std::vector<std::string> argv_;
158
159 // The type of native command line arguments.
160 typedef std::string StringType;
161
162 // Shared by the two POSIX constructor forms. Initalize from argv_.
163 void InitFromArgv();
164#endif
165
166 // Returns true and fills in |switch_string| and |switch_value|
167 // if |parameter_string| represents a switch.
168 static bool IsSwitch(const StringType& parameter_string,
169 std::string* switch_string,
170 StringType* switch_value);
171
172 // Parsed-out values.
173 std::map<std::string, StringType> switches_;
174
175 // Non-switch command-line arguments.
176 std::vector<StringType> loose_values_;
177
178 // We allow copy constructors, because a common pattern is to grab a
179 // copy of the current process's command line and then add some
180 // flags to it. E.g.:
181 // CommandLine cl(*CommandLine::ForCurrentProcess());
182 // cl.AppendSwitch(...);
initial.commitd7cae122008-07-26 21:49:38183};
184
[email protected]02c87962008-10-06 10:25:35185#endif // BASE_COMMAND_LINE_H_