erikwright@chromium.org | 506c72b | 2012-02-28 03:38:12 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
deanm@chromium.org | a638cc9 | 2008-10-06 19:25:35 +0900 | [diff] [blame] | 4 | |
evan@chromium.org | 4bbc613 | 2009-01-21 10:00:22 +0900 | [diff] [blame] | 5 | // This class works with command lines: building and parsing. |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 6 | // 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.org | 4bbc613 | 2009-01-21 10:00:22 +0900 | [diff] [blame] | 11 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 12 | // 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.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 14 | |
deanm@chromium.org | a638cc9 | 2008-10-06 19:25:35 +0900 | [diff] [blame] | 15 | #ifndef BASE_COMMAND_LINE_H_ |
| 16 | #define BASE_COMMAND_LINE_H_ |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 17 | |
jhawkins@chromium.org | 8e73d06 | 2011-04-05 03:04:37 +0900 | [diff] [blame] | 18 | #include <stddef.h> |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 19 | #include <map> |
| 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 23 | #include "base/base_export.h" |
brettw@chromium.org | 6a3802c | 2010-12-30 06:06:43 +0900 | [diff] [blame] | 24 | #include "build/build_config.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 25 | |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 26 | namespace base { |
brettw@chromium.org | 3727389 | 2014-03-18 08:07:15 +0900 | [diff] [blame] | 27 | |
erg@google.com | 6e67c1d | 2010-07-29 02:25:28 +0900 | [diff] [blame] | 28 | class FilePath; |
sky@google.com | 87bdd75 | 2009-01-30 09:40:43 +0900 | [diff] [blame] | 29 | |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 30 | class BASE_EXPORT CommandLine { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 31 | public: |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 32 | #if defined(OS_WIN) |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 33 | // The native command line string type. |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 34 | typedef std::wstring StringType; |
| 35 | #elif defined(OS_POSIX) |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 36 | typedef std::string StringType; |
| 37 | #endif |
| 38 | |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 39 | typedef StringType::value_type CharType; |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 40 | typedef std::vector<StringType> StringVector; |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 41 | typedef std::map<std::string, StringType> SwitchMap; |
| 42 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 43 | // A constructor for CommandLines that only carry switches and arguments. |
mattm@chromium.org | af4c7a0 | 2010-10-21 12:36:31 +0900 | [diff] [blame] | 44 | enum NoProgram { NO_PROGRAM }; |
| 45 | explicit CommandLine(NoProgram no_program); |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 46 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 47 | // Construct a new command line with |program| as argv[0]. |
brettw@chromium.org | 3727389 | 2014-03-18 08:07:15 +0900 | [diff] [blame] | 48 | explicit CommandLine(const FilePath& program); |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 49 | |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 50 | // Construct a new command line from an argument list. |
| 51 | CommandLine(int argc, const CharType* const* argv); |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 52 | explicit CommandLine(const StringVector& argv); |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 53 | |
msw@chromium.org | 96b695b | 2011-03-02 05:47:58 +0900 | [diff] [blame] | 54 | ~CommandLine(); |
| 55 | |
brettw@chromium.org | 40b2d58 | 2013-09-26 08:36:00 +0900 | [diff] [blame] | 56 | #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.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 67 | // 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.org | 506c72b | 2012-02-28 03:38:12 +0900 | [diff] [blame] | 70 | // 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.org | 4bbc613 | 2009-01-21 10:00:22 +0900 | [diff] [blame] | 74 | |
evan@chromium.org | fee6d86 | 2009-02-26 06:13:53 +0900 | [diff] [blame] | 75 | // Destroys the current process CommandLine singleton. This is necessary if |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 76 | // you want to reset the base library to its initial state (for example, in an |
evan@chromium.org | fee6d86 | 2009-02-26 06:13:53 +0900 | [diff] [blame] | 77 | // outer library that needs to be able to terminate, and be re-initialized). |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 78 | // If Init is called only once, as in main(), Reset() is not necessary. |
evan@chromium.org | b6e7c2b | 2009-10-13 01:11:40 +0900 | [diff] [blame] | 79 | static void Reset(); |
evan@chromium.org | fee6d86 | 2009-02-26 06:13:53 +0900 | [diff] [blame] | 80 | |
evan@chromium.org | 4bbc613 | 2009-01-21 10:00:22 +0900 | [diff] [blame] | 81 | // Get the singleton CommandLine representing the current process's |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 82 | // command line. Note: returned value is mutable, but not thread safe; |
evan@chromium.org | 757621d | 2009-10-13 07:50:39 +0900 | [diff] [blame] | 83 | // only mutate if you know what you're doing! |
erg@google.com | cb8b2cb | 2010-08-31 05:15:25 +0900 | [diff] [blame] | 84 | static CommandLine* ForCurrentProcess(); |
evanm@google.com | 638e9fb | 2008-08-12 10:14:37 +0900 | [diff] [blame] | 85 | |
vollick@chromium.org | 4ff9adc | 2013-07-12 08:10:40 +0900 | [diff] [blame] | 86 | // Returns true if the CommandLine has been initialized for the given process. |
| 87 | static bool InitializedForCurrentProcess(); |
| 88 | |
evan@chromium.org | 4bbc613 | 2009-01-21 10:00:22 +0900 | [diff] [blame] | 89 | #if defined(OS_WIN) |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 90 | static CommandLine FromString(const std::wstring& command_line); |
| 91 | #endif |
| 92 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 93 | // Initialize from an argv vector. |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 94 | void InitFromArgv(int argc, const CharType* const* argv); |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 95 | void InitFromArgv(const StringVector& argv); |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 96 | |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 97 | // Constructs and returns the represented command line string. |
gab@chromium.org | e399ff4 | 2012-10-30 06:31:31 +0900 | [diff] [blame] | 98 | // CAUTION! This should be avoided on POSIX because quoting behavior is |
| 99 | // unclear. |
msw@chromium.org | c57622c | 2011-07-20 13:54:52 +0900 | [diff] [blame] | 100 | StringType GetCommandLineString() const; |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 101 | |
gab@chromium.org | e399ff4 | 2012-10-30 06:31:31 +0900 | [diff] [blame] | 102 | // 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.org | 92c59f2 | 2008-10-16 06:59:08 +0900 | [diff] [blame] | 107 | // Returns the original command line string as a vector of strings. |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 108 | const StringVector& argv() const { return argv_; } |
estade@chromium.org | 92c59f2 | 2008-10-16 06:59:08 +0900 | [diff] [blame] | 109 | |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 110 | // Get and Set the program part of the command line string (the first item). |
brettw@chromium.org | 3727389 | 2014-03-18 08:07:15 +0900 | [diff] [blame] | 111 | FilePath GetProgram() const; |
| 112 | void SetProgram(const FilePath& program); |
evan@chromium.org | 757621d | 2009-10-13 07:50:39 +0900 | [diff] [blame] | 113 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 114 | // 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.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 117 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 118 | // 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.org | 3727389 | 2014-03-18 08:07:15 +0900 | [diff] [blame] | 121 | FilePath GetSwitchValuePath(const std::string& switch_string) const; |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 122 | StringType GetSwitchValueNative(const std::string& switch_string) const; |
| 123 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 124 | // 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.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 128 | // Note: Switches will precede arguments regardless of appending order. |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 129 | void AppendSwitch(const std::string& switch_string); |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 130 | void AppendSwitchPath(const std::string& switch_string, |
brettw@chromium.org | 3727389 | 2014-03-18 08:07:15 +0900 | [diff] [blame] | 131 | const FilePath& path); |
evan@chromium.org | 0b90a5c | 2010-07-30 08:02:34 +0900 | [diff] [blame] | 132 | void AppendSwitchNative(const std::string& switch_string, |
| 133 | const StringType& value); |
evan@chromium.org | 3f98514 | 2010-07-30 11:14:22 +0900 | [diff] [blame] | 134 | void AppendSwitchASCII(const std::string& switch_string, |
| 135 | const std::string& value); |
evan@chromium.org | 0b90a5c | 2010-07-30 08:02:34 +0900 | [diff] [blame] | 136 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 137 | // Copy a set of switches (and any values) from another command line. |
| 138 | // Commonly used when launching a subprocess. |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 139 | void CopySwitchesFrom(const CommandLine& source, |
| 140 | const char* const switches[], |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 141 | size_t count); |
| 142 | |
| 143 | // Get the remaining arguments to the command. |
msw@chromium.org | a25adf4 | 2011-07-14 08:41:22 +0900 | [diff] [blame] | 144 | StringVector GetArgs() const; |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 145 | |
| 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.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 149 | // Note: Switches will precede arguments regardless of appending order. |
evan@chromium.org | 580ab7f | 2010-08-14 07:10:30 +0900 | [diff] [blame] | 150 | void AppendArg(const std::string& value); |
brettw@chromium.org | 3727389 | 2014-03-18 08:07:15 +0900 | [diff] [blame] | 151 | void AppendArgPath(const FilePath& value); |
evan@chromium.org | 580ab7f | 2010-08-14 07:10:30 +0900 | [diff] [blame] | 152 | void AppendArgNative(const StringType& value); |
evan@chromium.org | 4bbc613 | 2009-01-21 10:00:22 +0900 | [diff] [blame] | 153 | |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 154 | // Append the switches and arguments from another command line to this one. |
evan@chromium.org | 4bbc613 | 2009-01-21 10:00:22 +0900 | [diff] [blame] | 155 | // If |include_program| is true, include |other|'s program as well. |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 156 | void AppendArguments(const CommandLine& other, bool include_program); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 157 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 158 | // Insert a command before the current command. |
| 159 | // Common for debuggers, like "valgrind" or "gdb --args". |
evan@chromium.org | 65dcdf9 | 2010-08-05 03:24:38 +0900 | [diff] [blame] | 160 | void PrependWrapper(const StringType& wrapper); |
agl@chromium.org | c60ca40 | 2009-02-10 09:52:14 +0900 | [diff] [blame] | 161 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 162 | #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.org | 0b90a5c | 2010-07-30 08:02:34 +0900 | [diff] [blame] | 167 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 168 | private: |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 169 | // Disallow default constructor; a program name must be explicitly specified. |
msw@chromium.org | 96b695b | 2011-03-02 05:47:58 +0900 | [diff] [blame] | 170 | CommandLine(); |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 171 | // 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.com | 87bdd75 | 2009-01-30 09:40:43 +0900 | [diff] [blame] | 175 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 176 | // The singleton CommandLine representing the current process's command line. |
evan@chromium.org | 4bbc613 | 2009-01-21 10:00:22 +0900 | [diff] [blame] | 177 | static CommandLine* current_process_commandline_; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 178 | |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 179 | // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* } |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 180 | StringVector argv_; |
evan@chromium.org | 4bbc613 | 2009-01-21 10:00:22 +0900 | [diff] [blame] | 181 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 182 | // Parsed-out switch keys and values. |
kinuko@chromium.org | 16013dc | 2010-05-24 19:39:59 +0900 | [diff] [blame] | 183 | SwitchMap switches_; |
evan@chromium.org | 4bbc613 | 2009-01-21 10:00:22 +0900 | [diff] [blame] | 184 | |
msw@chromium.org | 53fbac1 | 2011-05-14 10:10:24 +0900 | [diff] [blame] | 185 | // The index after the program and switches, any arguments start here. |
| 186 | size_t begin_args_; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 187 | }; |
| 188 | |
brettw@chromium.org | 3727389 | 2014-03-18 08:07:15 +0900 | [diff] [blame] | 189 | } // namespace base |
| 190 | |
| 191 | // TODO(brettw) remove once all callers specify the namespace properly. |
| 192 | using base::CommandLine; |
| 193 | |
deanm@chromium.org | a638cc9 | 2008-10-06 19:25:35 +0900 | [diff] [blame] | 194 | #endif // BASE_COMMAND_LINE_H_ |