blob: b8bbba92e224ee8934f3df6d030b54980207596a [file] [log] [blame]
[email protected]a502bbe72011-01-07 18:06:451// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// 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
20#include <map>
21#include <string>
22#include <vector>
23
24#include "base/basictypes.h"
[email protected]74e9fa22010-12-29 21:06:4325#include "build/build_config.h"
initial.commitd7cae122008-07-26 21:49:3826
[email protected]5d91c9e2010-07-28 17:25:2827class FilePath;
[email protected]d4515eb2009-01-30 00:40:4328class InProcessBrowserTest;
29
initial.commitd7cae122008-07-26 21:49:3830class CommandLine {
31 public:
[email protected]a502bbe72011-01-07 18:06:4532#if defined(OS_WIN)
33 // The type of native command line arguments.
34 typedef std::wstring StringType;
35#elif defined(OS_POSIX)
36 // The type of native command line arguments.
37 typedef std::string StringType;
38#endif
39
40 // The type of map for parsed-out switch key and values.
41 typedef std::map<std::string, StringType> SwitchMap;
42
[email protected]947446b2010-10-21 03:36:3143 // A constructor for CommandLines that are used only to carry switches and
44 // arguments.
45 enum NoProgram { NO_PROGRAM };
46 explicit CommandLine(NoProgram no_program);
[email protected]a502bbe72011-01-07 18:06:4547
48 // Construct a new, empty command line.
49 // |program| is the name of the program to run (aka argv[0]).
50 explicit CommandLine(const FilePath& program);
51
52#if defined(OS_POSIX)
53 CommandLine(int argc, const char* const* argv);
54 explicit CommandLine(const std::vector<std::string>& argv);
55#endif
56
[email protected]3a3d47472010-07-15 21:03:5457 ~CommandLine();
[email protected]51343d5a2009-10-26 22:39:3358
[email protected]f3adb5c2008-08-07 20:07:3259#if defined(OS_WIN)
[email protected]0189bbd2009-10-12 22:50:3960 // Initialize by parsing the given command-line string.
[email protected]bb975362009-01-21 01:00:2261 // The program name is assumed to be the first item in the string.
62 void ParseFromString(const std::wstring& command_line);
[email protected]5d91c9e2010-07-28 17:25:2863 static CommandLine FromString(const std::wstring& command_line);
[email protected]f3adb5c2008-08-07 20:07:3264#elif defined(OS_POSIX)
[email protected]0189bbd2009-10-12 22:50:3965 // Initialize from an argv vector.
66 void InitFromArgv(int argc, const char* const* argv);
67 void InitFromArgv(const std::vector<std::string>& argv);
[email protected]f3adb5c2008-08-07 20:07:3268#endif
initial.commitd7cae122008-07-26 21:49:3869
[email protected]bb975362009-01-21 01:00:2270 // Initialize the current process CommandLine singleton. On Windows,
71 // ignores its arguments (we instead parse GetCommandLineW()
72 // directly) because we don't trust the CRT's parsing of the command
73 // line, but it still must be called to set up the command line.
74 static void Init(int argc, const char* const* argv);
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();
[email protected]a2318cda2009-02-25 21:13:5382
[email protected]bb975362009-01-21 01:00:2283 // Get the singleton CommandLine representing the current process's
[email protected]0189bbd2009-10-12 22:50:3984 // command line. Note: returned value is mutable, but not thread safe;
85 // only mutate if you know what you're doing!
[email protected]dcd869c2010-08-30 20:15:2586 static CommandLine* ForCurrentProcess();
[email protected]1a48f312008-08-12 01:14:3787
initial.commitd7cae122008-07-26 21:49:3888 // Returns true if this command line contains the given switch.
89 // (Switch names are case-insensitive.)
[email protected]b7e0a2a2009-10-13 02:07:2590 bool HasSwitch(const std::string& switch_string) const;
91
initial.commitd7cae122008-07-26 21:49:3892 // Returns the value associated with the given switch. If the
93 // switch has no value or isn't present, this method returns
94 // the empty string.
[email protected]5d91c9e2010-07-28 17:25:2895 std::string GetSwitchValueASCII(const std::string& switch_string) const;
96 FilePath GetSwitchValuePath(const std::string& switch_string) const;
[email protected]4f08c83f2010-07-29 23:02:3497 StringType GetSwitchValueNative(const std::string& switch_string) const;
[email protected]b7e0a2a2009-10-13 02:07:2598
[email protected]92745242009-06-12 16:52:2199 // Get the number of switches in this process.
100 size_t GetSwitchCount() const { return switches_.size(); }
101
[email protected]bd48c2b02010-04-09 20:32:42102 // Get a copy of all switches, along with their values
[email protected]5d91c9e2010-07-28 17:25:28103 const SwitchMap& GetSwitches() const {
[email protected]bd48c2b02010-04-09 20:32:42104 return switches_;
105 }
106
[email protected]bb975362009-01-21 01:00:22107 // Get the remaining arguments to the command.
[email protected]2e4c50c2010-07-21 15:57:23108 const std::vector<StringType>& args() const { return args_; }
initial.commitd7cae122008-07-26 21:49:38109
[email protected]bb975362009-01-21 01:00:22110#if defined(OS_WIN)
111 // Returns the original command line string.
112 const std::wstring& command_line_string() const {
113 return command_line_string_;
114 }
115#elif defined(OS_POSIX)
[email protected]10e42bf2008-10-15 21:59:08116 // Returns the original command line string as a vector of strings.
[email protected]bb975362009-01-21 01:00:22117 const std::vector<std::string>& argv() const {
118 return argv_;
119 }
[email protected]d3b04ab2010-05-21 17:14:57120 // Try to match the same result as command_line_string() would get you
121 // on windows.
122 std::string command_line_string() const;
[email protected]10e42bf2008-10-15 21:59:08123#endif
124
initial.commitd7cae122008-07-26 21:49:38125 // Returns the program part of the command line string (the first item).
[email protected]5d91c9e2010-07-28 17:25:28126 FilePath GetProgram() const;
[email protected]0189bbd2009-10-12 22:50:39127
[email protected]4f08c83f2010-07-29 23:02:34128 // Append a switch to the command line.
[email protected]b7e0a2a2009-10-13 02:07:25129 void AppendSwitch(const std::string& switch_string);
initial.commitd7cae122008-07-26 21:49:38130
[email protected]4f08c83f2010-07-29 23:02:34131 // Append a switch and value to the command line.
132 void AppendSwitchPath(const std::string& switch_string, const FilePath& path);
133 void AppendSwitchNative(const std::string& switch_string,
134 const StringType& value);
[email protected]d420c31e2010-07-30 02:14:22135 void AppendSwitchASCII(const std::string& switch_string,
136 const std::string& value);
[email protected]4f08c83f2010-07-29 23:02:34137
[email protected]0445eb42010-08-13 22:10:30138 // Append an argument to the command line.
139 // Note on quoting: the argument will be quoted properly such that it is
140 // interpreted as one argument to the target command.
141 // AppendArg is primarily for ASCII; non-ASCII input will be
142 // interpreted as UTF-8.
143 void AppendArg(const std::string& value);
144 void AppendArgPath(const FilePath& value);
145 void AppendArgNative(const StringType& value);
[email protected]bb975362009-01-21 01:00:22146
147 // Append the arguments from another command line to this one.
148 // If |include_program| is true, include |other|'s program as well.
149 void AppendArguments(const CommandLine& other,
150 bool include_program);
initial.commitd7cae122008-07-26 21:49:38151
[email protected]13081fc2010-08-04 18:24:38152 // Insert a command before the current command. Common for debuggers,
153 // like "valgrind" or "gdb --args".
154 void PrependWrapper(const StringType& wrapper);
[email protected]052f1d482009-02-10 00:52:14155
[email protected]4f08c83f2010-07-29 23:02:34156 // Copy a set of switches (and their values, if any) from another command
157 // line. Commonly used when launching a subprocess.
158 void CopySwitchesFrom(const CommandLine& source, const char* const switches[],
159 size_t count);
160
initial.commitd7cae122008-07-26 21:49:38161 private:
[email protected]d4515eb2009-01-30 00:40:43162 friend class InProcessBrowserTest;
163
[email protected]3a3d47472010-07-15 21:03:54164 CommandLine();
initial.commitd7cae122008-07-26 21:49:38165
[email protected]d4515eb2009-01-30 00:40:43166 // Used by InProcessBrowserTest.
[email protected]dcd869c2010-08-30 20:15:25167 static CommandLine* ForCurrentProcessMutable();
[email protected]d4515eb2009-01-30 00:40:43168
[email protected]a502bbe72011-01-07 18:06:45169 // Returns true and fills in |switch_string| and |switch_value|
170 // if |parameter_string| represents a switch.
171 static bool IsSwitch(const StringType& parameter_string,
172 std::string* switch_string,
173 StringType* switch_value);
174
[email protected]bb975362009-01-21 01:00:22175 // The singleton CommandLine instance representing the current process's
176 // command line.
177 static CommandLine* current_process_commandline_;
initial.commitd7cae122008-07-26 21:49:38178
[email protected]bb975362009-01-21 01:00:22179 // We store a platform-native version of the command line, used when building
180 // up a new command line to be executed. This ifdef delimits that code.
181
182#if defined(OS_WIN)
183 // The quoted, space-separated command-line string.
184 std::wstring command_line_string_;
[email protected]bb975362009-01-21 01:00:22185 // The name of the program.
186 std::wstring program_;
[email protected]bb975362009-01-21 01:00:22187#elif defined(OS_POSIX)
188 // The argv array, with the program name in argv_[0].
189 std::vector<std::string> argv_;
[email protected]bb975362009-01-21 01:00:22190#endif
191
[email protected]bb975362009-01-21 01:00:22192 // Parsed-out values.
[email protected]f6c34832010-05-24 10:39:59193 SwitchMap switches_;
[email protected]bb975362009-01-21 01:00:22194
195 // Non-switch command-line arguments.
[email protected]2e4c50c2010-07-21 15:57:23196 std::vector<StringType> args_;
[email protected]bb975362009-01-21 01:00:22197
198 // We allow copy constructors, because a common pattern is to grab a
199 // copy of the current process's command line and then add some
200 // flags to it. E.g.:
201 // CommandLine cl(*CommandLine::ForCurrentProcess());
202 // cl.AppendSwitch(...);
initial.commitd7cae122008-07-26 21:49:38203};
204
[email protected]02c87962008-10-06 10:25:35205#endif // BASE_COMMAND_LINE_H_