blob: fd06e61c774ce0c13a2ef03817a5a8d7ee0561dc [file] [log] [blame]
erikwright@chromium.org506c72b2012-02-28 03:38:12 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botf003cfe2008-08-24 09:55:55 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
deanm@chromium.orga638cc92008-10-06 19:25:35 +09004
evan@chromium.org4bbc6132009-01-21 10:00:22 +09005// This class works with command lines: building and parsing.
msw@chromium.org53fbac12011-05-14 10:10:24 +09006// Arguments with prefixes ('--', '-', and on Windows, '/') are switches.
7// Switches will precede all other arguments without switch prefixes.
8// Switches can optionally have values, delimited by '=', e.g., "-switch=value".
9// An argument of "--" will terminate switch parsing during initialization,
10// interpreting subsequent tokens as non-switch arguments, regardless of prefix.
evan@chromium.org4bbc6132009-01-21 10:00:22 +090011
msw@chromium.org7b2af612011-03-01 11:28:42 +090012// There is a singleton read-only CommandLine that represents the command line
13// that the current process was started with. It must be initialized in main().
initial.commit3f4a7322008-07-27 06:49:38 +090014
deanm@chromium.orga638cc92008-10-06 19:25:35 +090015#ifndef BASE_COMMAND_LINE_H_
16#define BASE_COMMAND_LINE_H_
initial.commit3f4a7322008-07-27 06:49:38 +090017
jhawkins@chromium.org8e73d062011-04-05 03:04:37 +090018#include <stddef.h>
initial.commit3f4a7322008-07-27 06:49:38 +090019#include <map>
20#include <string>
21#include <vector>
22
darin@chromium.orge585bed2011-08-06 00:34:00 +090023#include "base/base_export.h"
brettw@chromium.org6a3802c2010-12-30 06:06:43 +090024#include "build/build_config.h"
initial.commit3f4a7322008-07-27 06:49:38 +090025
brettw@chromium.orga7086942013-02-02 14:12:33 +090026namespace base {
brettw@chromium.org37273892014-03-18 08:07:15 +090027
erg@google.com6e67c1d2010-07-29 02:25:28 +090028class FilePath;
sky@google.com87bdd752009-01-30 09:40:43 +090029
darin@chromium.orge585bed2011-08-06 00:34:00 +090030class BASE_EXPORT CommandLine {
initial.commit3f4a7322008-07-27 06:49:38 +090031 public:
erg@google.comd5fffd42011-01-08 03:06:45 +090032#if defined(OS_WIN)
msw@chromium.org7b2af612011-03-01 11:28:42 +090033 // The native command line string type.
erg@google.comd5fffd42011-01-08 03:06:45 +090034 typedef std::wstring StringType;
35#elif defined(OS_POSIX)
erg@google.comd5fffd42011-01-08 03:06:45 +090036 typedef std::string StringType;
37#endif
38
msw@chromium.org53fbac12011-05-14 10:10:24 +090039 typedef StringType::value_type CharType;
msw@chromium.org7b2af612011-03-01 11:28:42 +090040 typedef std::vector<StringType> StringVector;
erg@google.comd5fffd42011-01-08 03:06:45 +090041 typedef std::map<std::string, StringType> SwitchMap;
42
msw@chromium.org7b2af612011-03-01 11:28:42 +090043 // A constructor for CommandLines that only carry switches and arguments.
mattm@chromium.orgaf4c7a02010-10-21 12:36:31 +090044 enum NoProgram { NO_PROGRAM };
45 explicit CommandLine(NoProgram no_program);
erg@google.comd5fffd42011-01-08 03:06:45 +090046
msw@chromium.org7b2af612011-03-01 11:28:42 +090047 // Construct a new command line with |program| as argv[0].
brettw@chromium.org37273892014-03-18 08:07:15 +090048 explicit CommandLine(const FilePath& program);
erg@google.comd5fffd42011-01-08 03:06:45 +090049
msw@chromium.org53fbac12011-05-14 10:10:24 +090050 // Construct a new command line from an argument list.
51 CommandLine(int argc, const CharType* const* argv);
msw@chromium.org7b2af612011-03-01 11:28:42 +090052 explicit CommandLine(const StringVector& argv);
erg@google.comd5fffd42011-01-08 03:06:45 +090053
msw@chromium.org96b695b2011-03-02 05:47:58 +090054 ~CommandLine();
55
brettw@chromium.org40b2d582013-09-26 08:36:00 +090056#if defined(OS_WIN)
57 // By default this class will treat command-line arguments beginning with
58 // slashes as switches on Windows, but not other platforms.
59 //
60 // If this behavior is inappropriate for your application, you can call this
61 // function BEFORE initializing the current process' global command line
62 // object and the behavior will be the same as Posix systems (only hyphens
63 // begin switches, everything else will be an arg).
64 static void set_slash_is_not_a_switch();
65#endif
66
msw@chromium.org7b2af612011-03-01 11:28:42 +090067 // Initialize the current process CommandLine singleton. On Windows, ignores
68 // its arguments (we instead parse GetCommandLineW() directly) because we
69 // don't trust the CRT's parsing of the command line, but it still must be
erikwright@chromium.org506c72b2012-02-28 03:38:12 +090070 // called to set up the command line. Returns false if initialization has
71 // already occurred, and true otherwise. Only the caller receiving a 'true'
72 // return value should take responsibility for calling Reset.
73 static bool Init(int argc, const char* const* argv);
evan@chromium.org4bbc6132009-01-21 10:00:22 +090074
evan@chromium.orgfee6d862009-02-26 06:13:53 +090075 // Destroys the current process CommandLine singleton. This is necessary if
msw@chromium.org7b2af612011-03-01 11:28:42 +090076 // you want to reset the base library to its initial state (for example, in an
evan@chromium.orgfee6d862009-02-26 06:13:53 +090077 // outer library that needs to be able to terminate, and be re-initialized).
msw@chromium.org7b2af612011-03-01 11:28:42 +090078 // If Init is called only once, as in main(), Reset() is not necessary.
evan@chromium.orgb6e7c2b2009-10-13 01:11:40 +090079 static void Reset();
evan@chromium.orgfee6d862009-02-26 06:13:53 +090080
evan@chromium.org4bbc6132009-01-21 10:00:22 +090081 // Get the singleton CommandLine representing the current process's
msw@chromium.org7b2af612011-03-01 11:28:42 +090082 // command line. Note: returned value is mutable, but not thread safe;
evan@chromium.org757621d2009-10-13 07:50:39 +090083 // only mutate if you know what you're doing!
erg@google.comcb8b2cb2010-08-31 05:15:25 +090084 static CommandLine* ForCurrentProcess();
evanm@google.com638e9fb2008-08-12 10:14:37 +090085
vollick@chromium.org4ff9adc2013-07-12 08:10:40 +090086 // Returns true if the CommandLine has been initialized for the given process.
87 static bool InitializedForCurrentProcess();
88
evan@chromium.org4bbc6132009-01-21 10:00:22 +090089#if defined(OS_WIN)
msw@chromium.org7b2af612011-03-01 11:28:42 +090090 static CommandLine FromString(const std::wstring& command_line);
91#endif
92
msw@chromium.org7b2af612011-03-01 11:28:42 +090093 // Initialize from an argv vector.
msw@chromium.org53fbac12011-05-14 10:10:24 +090094 void InitFromArgv(int argc, const CharType* const* argv);
msw@chromium.org7b2af612011-03-01 11:28:42 +090095 void InitFromArgv(const StringVector& argv);
msw@chromium.org7b2af612011-03-01 11:28:42 +090096
msw@chromium.org53fbac12011-05-14 10:10:24 +090097 // Constructs and returns the represented command line string.
gab@chromium.orge399ff42012-10-30 06:31:31 +090098 // CAUTION! This should be avoided on POSIX because quoting behavior is
99 // unclear.
msw@chromium.orgc57622c2011-07-20 13:54:52 +0900100 StringType GetCommandLineString() const;
msw@chromium.org7b2af612011-03-01 11:28:42 +0900101
gab@chromium.orge399ff42012-10-30 06:31:31 +0900102 // Constructs and returns the represented arguments string.
103 // CAUTION! This should be avoided on POSIX because quoting behavior is
104 // unclear.
105 StringType GetArgumentsString() const;
106
estade@chromium.org92c59f22008-10-16 06:59:08 +0900107 // Returns the original command line string as a vector of strings.
msw@chromium.org7b2af612011-03-01 11:28:42 +0900108 const StringVector& argv() const { return argv_; }
estade@chromium.org92c59f22008-10-16 06:59:08 +0900109
msw@chromium.org53fbac12011-05-14 10:10:24 +0900110 // Get and Set the program part of the command line string (the first item).
brettw@chromium.org37273892014-03-18 08:07:15 +0900111 FilePath GetProgram() const;
112 void SetProgram(const FilePath& program);
evan@chromium.org757621d2009-10-13 07:50:39 +0900113
msw@chromium.org7b2af612011-03-01 11:28:42 +0900114 // Returns true if this command line contains the given switch.
115 // (Switch names are case-insensitive).
116 bool HasSwitch(const std::string& switch_string) const;
initial.commit3f4a7322008-07-27 06:49:38 +0900117
msw@chromium.org7b2af612011-03-01 11:28:42 +0900118 // Returns the value associated with the given switch. If the switch has no
119 // value or isn't present, this method returns the empty string.
120 std::string GetSwitchValueASCII(const std::string& switch_string) const;
brettw@chromium.org37273892014-03-18 08:07:15 +0900121 FilePath GetSwitchValuePath(const std::string& switch_string) const;
msw@chromium.org7b2af612011-03-01 11:28:42 +0900122 StringType GetSwitchValueNative(const std::string& switch_string) const;
123
msw@chromium.org7b2af612011-03-01 11:28:42 +0900124 // Get a copy of all switches, along with their values.
125 const SwitchMap& GetSwitches() const { return switches_; }
126
127 // Append a switch [with optional value] to the command line.
msw@chromium.org53fbac12011-05-14 10:10:24 +0900128 // Note: Switches will precede arguments regardless of appending order.
msw@chromium.org7b2af612011-03-01 11:28:42 +0900129 void AppendSwitch(const std::string& switch_string);
brettw@chromium.orga7086942013-02-02 14:12:33 +0900130 void AppendSwitchPath(const std::string& switch_string,
brettw@chromium.org37273892014-03-18 08:07:15 +0900131 const FilePath& path);
evan@chromium.org0b90a5c2010-07-30 08:02:34 +0900132 void AppendSwitchNative(const std::string& switch_string,
133 const StringType& value);
evan@chromium.org3f985142010-07-30 11:14:22 +0900134 void AppendSwitchASCII(const std::string& switch_string,
135 const std::string& value);
evan@chromium.org0b90a5c2010-07-30 08:02:34 +0900136
msw@chromium.org7b2af612011-03-01 11:28:42 +0900137 // Copy a set of switches (and any values) from another command line.
138 // Commonly used when launching a subprocess.
msw@chromium.org53fbac12011-05-14 10:10:24 +0900139 void CopySwitchesFrom(const CommandLine& source,
140 const char* const switches[],
msw@chromium.org7b2af612011-03-01 11:28:42 +0900141 size_t count);
142
143 // Get the remaining arguments to the command.
msw@chromium.orga25adf42011-07-14 08:41:22 +0900144 StringVector GetArgs() const;
msw@chromium.org7b2af612011-03-01 11:28:42 +0900145
146 // Append an argument to the command line. Note that the argument is quoted
147 // properly such that it is interpreted as one argument to the target command.
148 // AppendArg is primarily for ASCII; non-ASCII input is interpreted as UTF-8.
msw@chromium.org53fbac12011-05-14 10:10:24 +0900149 // Note: Switches will precede arguments regardless of appending order.
evan@chromium.org580ab7f2010-08-14 07:10:30 +0900150 void AppendArg(const std::string& value);
brettw@chromium.org37273892014-03-18 08:07:15 +0900151 void AppendArgPath(const FilePath& value);
evan@chromium.org580ab7f2010-08-14 07:10:30 +0900152 void AppendArgNative(const StringType& value);
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900153
msw@chromium.org53fbac12011-05-14 10:10:24 +0900154 // Append the switches and arguments from another command line to this one.
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900155 // If |include_program| is true, include |other|'s program as well.
msw@chromium.org53fbac12011-05-14 10:10:24 +0900156 void AppendArguments(const CommandLine& other, bool include_program);
initial.commit3f4a7322008-07-27 06:49:38 +0900157
msw@chromium.org7b2af612011-03-01 11:28:42 +0900158 // Insert a command before the current command.
159 // Common for debuggers, like "valgrind" or "gdb --args".
evan@chromium.org65dcdf92010-08-05 03:24:38 +0900160 void PrependWrapper(const StringType& wrapper);
agl@chromium.orgc60ca402009-02-10 09:52:14 +0900161
msw@chromium.org7b2af612011-03-01 11:28:42 +0900162#if defined(OS_WIN)
163 // Initialize by parsing the given command line string.
164 // The program name is assumed to be the first item in the string.
165 void ParseFromString(const std::wstring& command_line);
166#endif
evan@chromium.org0b90a5c2010-07-30 08:02:34 +0900167
initial.commit3f4a7322008-07-27 06:49:38 +0900168 private:
msw@chromium.org53fbac12011-05-14 10:10:24 +0900169 // Disallow default constructor; a program name must be explicitly specified.
msw@chromium.org96b695b2011-03-02 05:47:58 +0900170 CommandLine();
msw@chromium.org53fbac12011-05-14 10:10:24 +0900171 // Allow the copy constructor. A common pattern is to copy of the current
172 // process's command line and then add some flags to it. For example:
173 // CommandLine cl(*CommandLine::ForCurrentProcess());
174 // cl.AppendSwitch(...);
sky@google.com87bdd752009-01-30 09:40:43 +0900175
msw@chromium.org7b2af612011-03-01 11:28:42 +0900176 // The singleton CommandLine representing the current process's command line.
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900177 static CommandLine* current_process_commandline_;
initial.commit3f4a7322008-07-27 06:49:38 +0900178
msw@chromium.org53fbac12011-05-14 10:10:24 +0900179 // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* }
msw@chromium.org7b2af612011-03-01 11:28:42 +0900180 StringVector argv_;
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900181
msw@chromium.org7b2af612011-03-01 11:28:42 +0900182 // Parsed-out switch keys and values.
kinuko@chromium.org16013dc2010-05-24 19:39:59 +0900183 SwitchMap switches_;
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900184
msw@chromium.org53fbac12011-05-14 10:10:24 +0900185 // The index after the program and switches, any arguments start here.
186 size_t begin_args_;
initial.commit3f4a7322008-07-27 06:49:38 +0900187};
188
brettw@chromium.org37273892014-03-18 08:07:15 +0900189} // namespace base
190
191// TODO(brettw) remove once all callers specify the namespace properly.
192using base::CommandLine;
193
deanm@chromium.orga638cc92008-10-06 19:25:35 +0900194#endif // BASE_COMMAND_LINE_H_