blob: 970e4a76f26da70363abcf8a4ae81f83228a6b06 [file] [log] [blame]
erg@google.comd5fffd42011-01-08 03:06:45 +09001// Copyright (c) 2011 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.org7b2af612011-03-01 11:28:42 +09006// 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.
evan@chromium.org4bbc6132009-01-21 10:00:22 +090010
msw@chromium.org7b2af612011-03-01 11:28:42 +090011// 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.commit3f4a7322008-07-27 06:49:38 +090013
deanm@chromium.orga638cc92008-10-06 19:25:35 +090014#ifndef BASE_COMMAND_LINE_H_
15#define BASE_COMMAND_LINE_H_
thakis@chromium.org01d14522010-07-27 08:08:24 +090016#pragma once
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
rvargas@google.com73eb5d02011-03-25 04:00:20 +090023#include "base/base_api.h"
brettw@chromium.org6a3802c2010-12-30 06:06:43 +090024#include "build/build_config.h"
initial.commit3f4a7322008-07-27 06:49:38 +090025
erg@google.com6e67c1d2010-07-29 02:25:28 +090026class FilePath;
sky@google.com87bdd752009-01-30 09:40:43 +090027
rvargas@google.com73eb5d02011-03-25 04:00:20 +090028class BASE_API CommandLine {
initial.commit3f4a7322008-07-27 06:49:38 +090029 public:
erg@google.comd5fffd42011-01-08 03:06:45 +090030#if defined(OS_WIN)
msw@chromium.org7b2af612011-03-01 11:28:42 +090031 // The native command line string type.
erg@google.comd5fffd42011-01-08 03:06:45 +090032 typedef std::wstring StringType;
33#elif defined(OS_POSIX)
erg@google.comd5fffd42011-01-08 03:06:45 +090034 typedef std::string StringType;
35#endif
36
msw@chromium.org7b2af612011-03-01 11:28:42 +090037 typedef std::vector<StringType> StringVector;
erg@google.comd5fffd42011-01-08 03:06:45 +090038 // The type of map for parsed-out switch key and values.
39 typedef std::map<std::string, StringType> SwitchMap;
40
msw@chromium.org7b2af612011-03-01 11:28:42 +090041 // A constructor for CommandLines that only carry switches and arguments.
mattm@chromium.orgaf4c7a02010-10-21 12:36:31 +090042 enum NoProgram { NO_PROGRAM };
43 explicit CommandLine(NoProgram no_program);
erg@google.comd5fffd42011-01-08 03:06:45 +090044
msw@chromium.org7b2af612011-03-01 11:28:42 +090045 // Construct a new command line with |program| as argv[0].
erg@google.comd5fffd42011-01-08 03:06:45 +090046 explicit CommandLine(const FilePath& program);
47
48#if defined(OS_POSIX)
49 CommandLine(int argc, const char* const* argv);
msw@chromium.org7b2af612011-03-01 11:28:42 +090050 explicit CommandLine(const StringVector& argv);
erg@google.comd5fffd42011-01-08 03:06:45 +090051#endif
52
msw@chromium.org96b695b2011-03-02 05:47:58 +090053 ~CommandLine();
54
msw@chromium.org7b2af612011-03-01 11:28:42 +090055 // Initialize the current process CommandLine singleton. On Windows, ignores
56 // its arguments (we instead parse GetCommandLineW() directly) because we
57 // don't trust the CRT's parsing of the command line, but it still must be
58 // called to set up the command line.
evan@chromium.org4bbc6132009-01-21 10:00:22 +090059 static void Init(int argc, const char* const* argv);
60
evan@chromium.orgfee6d862009-02-26 06:13:53 +090061 // Destroys the current process CommandLine singleton. This is necessary if
msw@chromium.org7b2af612011-03-01 11:28:42 +090062 // you want to reset the base library to its initial state (for example, in an
evan@chromium.orgfee6d862009-02-26 06:13:53 +090063 // outer library that needs to be able to terminate, and be re-initialized).
msw@chromium.org7b2af612011-03-01 11:28:42 +090064 // If Init is called only once, as in main(), Reset() is not necessary.
evan@chromium.orgb6e7c2b2009-10-13 01:11:40 +090065 static void Reset();
evan@chromium.orgfee6d862009-02-26 06:13:53 +090066
evan@chromium.org4bbc6132009-01-21 10:00:22 +090067 // Get the singleton CommandLine representing the current process's
msw@chromium.org7b2af612011-03-01 11:28:42 +090068 // command line. Note: returned value is mutable, but not thread safe;
evan@chromium.org757621d2009-10-13 07:50:39 +090069 // only mutate if you know what you're doing!
erg@google.comcb8b2cb2010-08-31 05:15:25 +090070 static CommandLine* ForCurrentProcess();
evanm@google.com638e9fb2008-08-12 10:14:37 +090071
evan@chromium.org4bbc6132009-01-21 10:00:22 +090072#if defined(OS_WIN)
msw@chromium.org7b2af612011-03-01 11:28:42 +090073 static CommandLine FromString(const std::wstring& command_line);
74#endif
75
76#if defined(OS_POSIX)
77 // Initialize from an argv vector.
78 void InitFromArgv(int argc, const char* const* argv);
79 void InitFromArgv(const StringVector& argv);
80#endif
81
82 // Returns the represented command line string.
83 // CAUTION! This should be avoided because quoting behavior is unclear.
84 StringType command_line_string() const;
85
86#if defined(OS_POSIX)
estade@chromium.org92c59f22008-10-16 06:59:08 +090087 // Returns the original command line string as a vector of strings.
msw@chromium.org7b2af612011-03-01 11:28:42 +090088 const StringVector& argv() const { return argv_; }
estade@chromium.org92c59f22008-10-16 06:59:08 +090089#endif
90
initial.commit3f4a7322008-07-27 06:49:38 +090091 // Returns the program part of the command line string (the first item).
erg@google.com6e67c1d2010-07-29 02:25:28 +090092 FilePath GetProgram() const;
evan@chromium.org757621d2009-10-13 07:50:39 +090093
msw@chromium.org7b2af612011-03-01 11:28:42 +090094 // Returns true if this command line contains the given switch.
95 // (Switch names are case-insensitive).
96 bool HasSwitch(const std::string& switch_string) const;
initial.commit3f4a7322008-07-27 06:49:38 +090097
msw@chromium.org7b2af612011-03-01 11:28:42 +090098 // Returns the value associated with the given switch. If the switch has no
99 // value or isn't present, this method returns the empty string.
100 std::string GetSwitchValueASCII(const std::string& switch_string) const;
101 FilePath GetSwitchValuePath(const std::string& switch_string) const;
102 StringType GetSwitchValueNative(const std::string& switch_string) const;
103
104 // Get the number of switches in this process.
105 // TODO(msw): Remove unnecessary API.
106 size_t GetSwitchCount() const;
107
108 // Get a copy of all switches, along with their values.
109 const SwitchMap& GetSwitches() const { return switches_; }
110
111 // Append a switch [with optional value] to the command line.
112 // CAUTION! Appending a switch after the "--" switch terminator is futile!
113 void AppendSwitch(const std::string& switch_string);
evan@chromium.org0b90a5c2010-07-30 08:02:34 +0900114 void AppendSwitchPath(const std::string& switch_string, const FilePath& path);
115 void AppendSwitchNative(const std::string& switch_string,
116 const StringType& value);
evan@chromium.org3f985142010-07-30 11:14:22 +0900117 void AppendSwitchASCII(const std::string& switch_string,
118 const std::string& value);
msw@chromium.orgda7a5482011-02-20 15:33:04 +0900119 void AppendSwitches(const CommandLine& other);
evan@chromium.org0b90a5c2010-07-30 08:02:34 +0900120
msw@chromium.org7b2af612011-03-01 11:28:42 +0900121 // Copy a set of switches (and any values) from another command line.
122 // Commonly used when launching a subprocess.
123 void CopySwitchesFrom(const CommandLine& source, const char* const switches[],
124 size_t count);
125
126 // Get the remaining arguments to the command.
127 const StringVector& args() const { return args_; }
128
129 // Append an argument to the command line. Note that the argument is quoted
130 // properly such that it is interpreted as one argument to the target command.
131 // AppendArg is primarily for ASCII; non-ASCII input is interpreted as UTF-8.
evan@chromium.org580ab7f2010-08-14 07:10:30 +0900132 void AppendArg(const std::string& value);
133 void AppendArgPath(const FilePath& value);
134 void AppendArgNative(const StringType& value);
msw@chromium.orgda7a5482011-02-20 15:33:04 +0900135 void AppendArgs(const CommandLine& other);
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900136
137 // Append the arguments from another command line to this one.
138 // If |include_program| is true, include |other|'s program as well.
139 void AppendArguments(const CommandLine& other,
140 bool include_program);
initial.commit3f4a7322008-07-27 06:49:38 +0900141
msw@chromium.org7b2af612011-03-01 11:28:42 +0900142 // Insert a command before the current command.
143 // Common for debuggers, like "valgrind" or "gdb --args".
evan@chromium.org65dcdf92010-08-05 03:24:38 +0900144 void PrependWrapper(const StringType& wrapper);
agl@chromium.orgc60ca402009-02-10 09:52:14 +0900145
msw@chromium.org7b2af612011-03-01 11:28:42 +0900146#if defined(OS_WIN)
147 // Initialize by parsing the given command line string.
148 // The program name is assumed to be the first item in the string.
149 void ParseFromString(const std::wstring& command_line);
150#endif
evan@chromium.org0b90a5c2010-07-30 08:02:34 +0900151
initial.commit3f4a7322008-07-27 06:49:38 +0900152 private:
msw@chromium.org96b695b2011-03-02 05:47:58 +0900153 // Disallow public default construction; a program name must be specified.
154 CommandLine();
sky@google.com87bdd752009-01-30 09:40:43 +0900155
msw@chromium.org7b2af612011-03-01 11:28:42 +0900156 // The singleton CommandLine representing the current process's command line.
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900157 static CommandLine* current_process_commandline_;
initial.commit3f4a7322008-07-27 06:49:38 +0900158
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900159 // We store a platform-native version of the command line, used when building
msw@chromium.org7b2af612011-03-01 11:28:42 +0900160 // up a new command line to be executed. This ifdef delimits that code.
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900161#if defined(OS_WIN)
msw@chromium.org7b2af612011-03-01 11:28:42 +0900162 // The quoted, space-separated command line string.
163 StringType command_line_string_;
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900164 // The name of the program.
msw@chromium.org7b2af612011-03-01 11:28:42 +0900165 StringType program_;
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900166#elif defined(OS_POSIX)
167 // The argv array, with the program name in argv_[0].
msw@chromium.org7b2af612011-03-01 11:28:42 +0900168 StringVector argv_;
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900169#endif
170
msw@chromium.org7b2af612011-03-01 11:28:42 +0900171 // Parsed-out switch keys and values.
kinuko@chromium.org16013dc2010-05-24 19:39:59 +0900172 SwitchMap switches_;
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900173
msw@chromium.org7b2af612011-03-01 11:28:42 +0900174 // Non-switch command line arguments.
175 StringVector args_;
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900176
msw@chromium.org7b2af612011-03-01 11:28:42 +0900177 // Allow the copy constructor. A common pattern is to copy the current
178 // process's command line and then add some flags to it. For example:
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900179 // CommandLine cl(*CommandLine::ForCurrentProcess());
180 // cl.AppendSwitch(...);
initial.commit3f4a7322008-07-27 06:49:38 +0900181};
182
deanm@chromium.orga638cc92008-10-06 19:25:35 +0900183#endif // BASE_COMMAND_LINE_H_