[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
[email protected] | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 4 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 5 | // This class works with command lines: building and parsing. |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 6 | // 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] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 10 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 11 | // 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.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 13 | |
[email protected] | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 14 | #ifndef BASE_COMMAND_LINE_H_ |
| 15 | #define BASE_COMMAND_LINE_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 16 | #pragma once |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 17 | |
| 18 | #include <map> |
| 19 | #include <string> |
| 20 | #include <vector> |
| 21 | |
| 22 | #include "base/basictypes.h" |
[email protected] | 74e9fa2 | 2010-12-29 21:06:43 | [diff] [blame] | 23 | #include "build/build_config.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 24 | |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 25 | class FilePath; |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 26 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 27 | class CommandLine { |
| 28 | public: |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 29 | #if defined(OS_WIN) |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 30 | // The native command line string type. |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 31 | typedef std::wstring StringType; |
| 32 | #elif defined(OS_POSIX) |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 33 | typedef std::string StringType; |
| 34 | #endif |
| 35 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 36 | typedef std::vector<StringType> StringVector; |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 37 | // The type of map for parsed-out switch key and values. |
| 38 | typedef std::map<std::string, StringType> SwitchMap; |
| 39 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 40 | // A constructor for CommandLines that only carry switches and arguments. |
[email protected] | 947446b | 2010-10-21 03:36:31 | [diff] [blame] | 41 | enum NoProgram { NO_PROGRAM }; |
| 42 | explicit CommandLine(NoProgram no_program); |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 43 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 44 | // Construct a new command line with |program| as argv[0]. |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 45 | explicit CommandLine(const FilePath& program); |
| 46 | |
| 47 | #if defined(OS_POSIX) |
| 48 | CommandLine(int argc, const char* const* argv); |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 49 | explicit CommandLine(const StringVector& argv); |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 50 | #endif |
| 51 | |
[email protected] | acbeb3d | 2011-03-01 20:47:58 | [diff] [blame] | 52 | ~CommandLine(); |
| 53 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 54 | // 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] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 58 | static void Init(int argc, const char* const* argv); |
| 59 | |
[email protected] | a2318cda | 2009-02-25 21:13:53 | [diff] [blame] | 60 | // Destroys the current process CommandLine singleton. This is necessary if |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 61 | // you want to reset the base library to its initial state (for example, in an |
[email protected] | a2318cda | 2009-02-25 21:13:53 | [diff] [blame] | 62 | // outer library that needs to be able to terminate, and be re-initialized). |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 63 | // If Init is called only once, as in main(), Reset() is not necessary. |
[email protected] | 02fb75ab | 2009-10-12 16:11:40 | [diff] [blame] | 64 | static void Reset(); |
[email protected] | a2318cda | 2009-02-25 21:13:53 | [diff] [blame] | 65 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 66 | // Get the singleton CommandLine representing the current process's |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 67 | // command line. Note: returned value is mutable, but not thread safe; |
[email protected] | 0189bbd | 2009-10-12 22:50:39 | [diff] [blame] | 68 | // only mutate if you know what you're doing! |
[email protected] | dcd869c | 2010-08-30 20:15:25 | [diff] [blame] | 69 | static CommandLine* ForCurrentProcess(); |
[email protected] | 1a48f31 | 2008-08-12 01:14:37 | [diff] [blame] | 70 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 71 | #if defined(OS_WIN) |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 72 | 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] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 86 | // Returns the original command line string as a vector of strings. |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 87 | const StringVector& argv() const { return argv_; } |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 88 | #endif |
| 89 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 90 | // Returns the program part of the command line string (the first item). |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 91 | FilePath GetProgram() const; |
[email protected] | 0189bbd | 2009-10-12 22:50:39 | [diff] [blame] | 92 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 93 | // 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.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 96 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 97 | // 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] | 4f08c83f | 2010-07-29 23:02:34 | [diff] [blame] | 113 | void AppendSwitchPath(const std::string& switch_string, const FilePath& path); |
| 114 | void AppendSwitchNative(const std::string& switch_string, |
| 115 | const StringType& value); |
[email protected] | d420c31e | 2010-07-30 02:14:22 | [diff] [blame] | 116 | void AppendSwitchASCII(const std::string& switch_string, |
| 117 | const std::string& value); |
[email protected] | 0fd23af | 2011-02-20 06:33:04 | [diff] [blame] | 118 | void AppendSwitches(const CommandLine& other); |
[email protected] | 4f08c83f | 2010-07-29 23:02:34 | [diff] [blame] | 119 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 120 | // 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] | 0445eb4 | 2010-08-13 22:10:30 | [diff] [blame] | 131 | void AppendArg(const std::string& value); |
| 132 | void AppendArgPath(const FilePath& value); |
| 133 | void AppendArgNative(const StringType& value); |
[email protected] | 0fd23af | 2011-02-20 06:33:04 | [diff] [blame] | 134 | void AppendArgs(const CommandLine& other); |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 135 | |
| 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.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 140 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 141 | // Insert a command before the current command. |
| 142 | // Common for debuggers, like "valgrind" or "gdb --args". |
[email protected] | 13081fc | 2010-08-04 18:24:38 | [diff] [blame] | 143 | void PrependWrapper(const StringType& wrapper); |
[email protected] | 052f1d48 | 2009-02-10 00:52:14 | [diff] [blame] | 144 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 145 | #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] | 4f08c83f | 2010-07-29 23:02:34 | [diff] [blame] | 150 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 151 | private: |
[email protected] | acbeb3d | 2011-03-01 20:47:58 | [diff] [blame] | 152 | // Disallow public default construction; a program name must be specified. |
| 153 | CommandLine(); |
[email protected] | d4515eb | 2009-01-30 00:40:43 | [diff] [blame] | 154 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 155 | // The singleton CommandLine representing the current process's command line. |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 156 | static CommandLine* current_process_commandline_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 157 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 158 | // We store a platform-native version of the command line, used when building |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 159 | // up a new command line to be executed. This ifdef delimits that code. |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 160 | #if defined(OS_WIN) |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 161 | // The quoted, space-separated command line string. |
| 162 | StringType command_line_string_; |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 163 | // The name of the program. |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 164 | StringType program_; |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 165 | #elif defined(OS_POSIX) |
| 166 | // The argv array, with the program name in argv_[0]. |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 167 | StringVector argv_; |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 168 | #endif |
| 169 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 170 | // Parsed-out switch keys and values. |
[email protected] | f6c3483 | 2010-05-24 10:39:59 | [diff] [blame] | 171 | SwitchMap switches_; |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 172 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 173 | // Non-switch command line arguments. |
| 174 | StringVector args_; |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 175 | |
[email protected] | 06cc083a | 2011-03-01 02:28:42 | [diff] [blame] | 176 | // 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] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 178 | // CommandLine cl(*CommandLine::ForCurrentProcess()); |
| 179 | // cl.AppendSwitch(...); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 180 | }; |
| 181 | |
[email protected] | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 182 | #endif // BASE_COMMAND_LINE_H_ |