blob: 7ff5ab080938121646879a4e474f7d3bc2ca7e1d [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.
[email protected]06cc083a2011-03-01 02:28:426// Switches can optionally have a value attached using an equals sign, as in
7// "-switch=value". Arguments that aren't prefixed with a switch prefix are
8// saved as extra arguments. An argument of "--" will terminate switch parsing,
9// causing everything after to be considered as extra arguments.
[email protected]bb975362009-01-21 01:00:2210
[email protected]06cc083a2011-03-01 02:28:4211// There is a singleton read-only CommandLine that represents the command line
12// that the current process was started with. It must be initialized in main().
initial.commitd7cae122008-07-26 21:49:3813
[email protected]02c87962008-10-06 10:25:3514#ifndef BASE_COMMAND_LINE_H_
15#define BASE_COMMAND_LINE_H_
[email protected]32b76ef2010-07-26 23:08:2416#pragma once
initial.commitd7cae122008-07-26 21:49:3817
18#include <map>
19#include <string>
20#include <vector>
21
22#include "base/basictypes.h"
[email protected]74e9fa22010-12-29 21:06:4323#include "build/build_config.h"
initial.commitd7cae122008-07-26 21:49:3824
[email protected]5d91c9e2010-07-28 17:25:2825class FilePath;
[email protected]d4515eb2009-01-30 00:40:4326
initial.commitd7cae122008-07-26 21:49:3827class CommandLine {
28 public:
[email protected]a502bbe72011-01-07 18:06:4529#if defined(OS_WIN)
[email protected]06cc083a2011-03-01 02:28:4230 // The native command line string type.
[email protected]a502bbe72011-01-07 18:06:4531 typedef std::wstring StringType;
32#elif defined(OS_POSIX)
[email protected]a502bbe72011-01-07 18:06:4533 typedef std::string StringType;
34#endif
35
[email protected]06cc083a2011-03-01 02:28:4236 typedef std::vector<StringType> StringVector;
[email protected]a502bbe72011-01-07 18:06:4537 // The type of map for parsed-out switch key and values.
38 typedef std::map<std::string, StringType> SwitchMap;
39
[email protected]06cc083a2011-03-01 02:28:4240 // A constructor for CommandLines that only carry switches and arguments.
[email protected]947446b2010-10-21 03:36:3141 enum NoProgram { NO_PROGRAM };
42 explicit CommandLine(NoProgram no_program);
[email protected]a502bbe72011-01-07 18:06:4543
[email protected]06cc083a2011-03-01 02:28:4244 // Construct a new command line with |program| as argv[0].
[email protected]a502bbe72011-01-07 18:06:4545 explicit CommandLine(const FilePath& program);
46
47#if defined(OS_POSIX)
48 CommandLine(int argc, const char* const* argv);
[email protected]06cc083a2011-03-01 02:28:4249 explicit CommandLine(const StringVector& argv);
[email protected]a502bbe72011-01-07 18:06:4550#endif
51
[email protected]acbeb3d2011-03-01 20:47:5852 ~CommandLine();
53
[email protected]06cc083a2011-03-01 02:28:4254 // Initialize the current process CommandLine singleton. On Windows, ignores
55 // its arguments (we instead parse GetCommandLineW() directly) because we
56 // don't trust the CRT's parsing of the command line, but it still must be
57 // called to set up the command line.
[email protected]bb975362009-01-21 01:00:2258 static void Init(int argc, const char* const* argv);
59
[email protected]a2318cda2009-02-25 21:13:5360 // Destroys the current process CommandLine singleton. This is necessary if
[email protected]06cc083a2011-03-01 02:28:4261 // you want to reset the base library to its initial state (for example, in an
[email protected]a2318cda2009-02-25 21:13:5362 // outer library that needs to be able to terminate, and be re-initialized).
[email protected]06cc083a2011-03-01 02:28:4263 // If Init is called only once, as in main(), Reset() is not necessary.
[email protected]02fb75ab2009-10-12 16:11:4064 static void Reset();
[email protected]a2318cda2009-02-25 21:13:5365
[email protected]bb975362009-01-21 01:00:2266 // Get the singleton CommandLine representing the current process's
[email protected]06cc083a2011-03-01 02:28:4267 // command line. Note: returned value is mutable, but not thread safe;
[email protected]0189bbd2009-10-12 22:50:3968 // only mutate if you know what you're doing!
[email protected]dcd869c2010-08-30 20:15:2569 static CommandLine* ForCurrentProcess();
[email protected]1a48f312008-08-12 01:14:3770
[email protected]bb975362009-01-21 01:00:2271#if defined(OS_WIN)
[email protected]06cc083a2011-03-01 02:28:4272 static CommandLine FromString(const std::wstring& command_line);
73#endif
74
75#if defined(OS_POSIX)
76 // Initialize from an argv vector.
77 void InitFromArgv(int argc, const char* const* argv);
78 void InitFromArgv(const StringVector& argv);
79#endif
80
81 // Returns the represented command line string.
82 // CAUTION! This should be avoided because quoting behavior is unclear.
83 StringType command_line_string() const;
84
85#if defined(OS_POSIX)
[email protected]10e42bf2008-10-15 21:59:0886 // Returns the original command line string as a vector of strings.
[email protected]06cc083a2011-03-01 02:28:4287 const StringVector& argv() const { return argv_; }
[email protected]10e42bf2008-10-15 21:59:0888#endif
89
initial.commitd7cae122008-07-26 21:49:3890 // Returns the program part of the command line string (the first item).
[email protected]5d91c9e2010-07-28 17:25:2891 FilePath GetProgram() const;
[email protected]0189bbd2009-10-12 22:50:3992
[email protected]06cc083a2011-03-01 02:28:4293 // Returns true if this command line contains the given switch.
94 // (Switch names are case-insensitive).
95 bool HasSwitch(const std::string& switch_string) const;
initial.commitd7cae122008-07-26 21:49:3896
[email protected]06cc083a2011-03-01 02:28:4297 // Returns the value associated with the given switch. If the switch has no
98 // value or isn't present, this method returns the empty string.
99 std::string GetSwitchValueASCII(const std::string& switch_string) const;
100 FilePath GetSwitchValuePath(const std::string& switch_string) const;
101 StringType GetSwitchValueNative(const std::string& switch_string) const;
102
103 // Get the number of switches in this process.
104 // TODO(msw): Remove unnecessary API.
105 size_t GetSwitchCount() const;
106
107 // Get a copy of all switches, along with their values.
108 const SwitchMap& GetSwitches() const { return switches_; }
109
110 // Append a switch [with optional value] to the command line.
111 // CAUTION! Appending a switch after the "--" switch terminator is futile!
112 void AppendSwitch(const std::string& switch_string);
[email protected]4f08c83f2010-07-29 23:02:34113 void AppendSwitchPath(const std::string& switch_string, const FilePath& path);
114 void AppendSwitchNative(const std::string& switch_string,
115 const StringType& value);
[email protected]d420c31e2010-07-30 02:14:22116 void AppendSwitchASCII(const std::string& switch_string,
117 const std::string& value);
[email protected]0fd23af2011-02-20 06:33:04118 void AppendSwitches(const CommandLine& other);
[email protected]4f08c83f2010-07-29 23:02:34119
[email protected]06cc083a2011-03-01 02:28:42120 // Copy a set of switches (and any values) from another command line.
121 // Commonly used when launching a subprocess.
122 void CopySwitchesFrom(const CommandLine& source, const char* const switches[],
123 size_t count);
124
125 // Get the remaining arguments to the command.
126 const StringVector& args() const { return args_; }
127
128 // Append an argument to the command line. Note that the argument is quoted
129 // properly such that it is interpreted as one argument to the target command.
130 // AppendArg is primarily for ASCII; non-ASCII input is interpreted as UTF-8.
[email protected]0445eb42010-08-13 22:10:30131 void AppendArg(const std::string& value);
132 void AppendArgPath(const FilePath& value);
133 void AppendArgNative(const StringType& value);
[email protected]0fd23af2011-02-20 06:33:04134 void AppendArgs(const CommandLine& other);
[email protected]bb975362009-01-21 01:00:22135
136 // Append the arguments from another command line to this one.
137 // If |include_program| is true, include |other|'s program as well.
138 void AppendArguments(const CommandLine& other,
139 bool include_program);
initial.commitd7cae122008-07-26 21:49:38140
[email protected]06cc083a2011-03-01 02:28:42141 // Insert a command before the current command.
142 // Common for debuggers, like "valgrind" or "gdb --args".
[email protected]13081fc2010-08-04 18:24:38143 void PrependWrapper(const StringType& wrapper);
[email protected]052f1d482009-02-10 00:52:14144
[email protected]06cc083a2011-03-01 02:28:42145#if defined(OS_WIN)
146 // Initialize by parsing the given command line string.
147 // The program name is assumed to be the first item in the string.
148 void ParseFromString(const std::wstring& command_line);
149#endif
[email protected]4f08c83f2010-07-29 23:02:34150
initial.commitd7cae122008-07-26 21:49:38151 private:
[email protected]acbeb3d2011-03-01 20:47:58152 // Disallow public default construction; a program name must be specified.
153 CommandLine();
[email protected]d4515eb2009-01-30 00:40:43154
[email protected]06cc083a2011-03-01 02:28:42155 // The singleton CommandLine representing the current process's command line.
[email protected]bb975362009-01-21 01:00:22156 static CommandLine* current_process_commandline_;
initial.commitd7cae122008-07-26 21:49:38157
[email protected]bb975362009-01-21 01:00:22158 // We store a platform-native version of the command line, used when building
[email protected]06cc083a2011-03-01 02:28:42159 // up a new command line to be executed. This ifdef delimits that code.
[email protected]bb975362009-01-21 01:00:22160#if defined(OS_WIN)
[email protected]06cc083a2011-03-01 02:28:42161 // The quoted, space-separated command line string.
162 StringType command_line_string_;
[email protected]bb975362009-01-21 01:00:22163 // The name of the program.
[email protected]06cc083a2011-03-01 02:28:42164 StringType program_;
[email protected]bb975362009-01-21 01:00:22165#elif defined(OS_POSIX)
166 // The argv array, with the program name in argv_[0].
[email protected]06cc083a2011-03-01 02:28:42167 StringVector argv_;
[email protected]bb975362009-01-21 01:00:22168#endif
169
[email protected]06cc083a2011-03-01 02:28:42170 // Parsed-out switch keys and values.
[email protected]f6c34832010-05-24 10:39:59171 SwitchMap switches_;
[email protected]bb975362009-01-21 01:00:22172
[email protected]06cc083a2011-03-01 02:28:42173 // Non-switch command line arguments.
174 StringVector args_;
[email protected]bb975362009-01-21 01:00:22175
[email protected]06cc083a2011-03-01 02:28:42176 // Allow the copy constructor. A common pattern is to copy the current
177 // process's command line and then add some flags to it. For example:
[email protected]bb975362009-01-21 01:00:22178 // CommandLine cl(*CommandLine::ForCurrentProcess());
179 // cl.AppendSwitch(...);
initial.commitd7cae122008-07-26 21:49:38180};
181
[email protected]02c87962008-10-06 10:25:35182#endif // BASE_COMMAND_LINE_H_