erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 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 | 7b2af61 | 2011-03-01 11:28:42 +0900 | [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. |
evan@chromium.org | 4bbc613 | 2009-01-21 10:00:22 +0900 | [diff] [blame] | 10 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [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 | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 13 | |
deanm@chromium.org | a638cc9 | 2008-10-06 19:25:35 +0900 | [diff] [blame] | 14 | #ifndef BASE_COMMAND_LINE_H_ |
| 15 | #define BASE_COMMAND_LINE_H_ |
thakis@chromium.org | 01d1452 | 2010-07-27 08:08:24 +0900 | [diff] [blame] | 16 | #pragma once |
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 | |
rvargas@google.com | 73eb5d0 | 2011-03-25 04:00:20 +0900 | [diff] [blame] | 23 | #include "base/base_api.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 | |
erg@google.com | 6e67c1d | 2010-07-29 02:25:28 +0900 | [diff] [blame] | 26 | class FilePath; |
sky@google.com | 87bdd75 | 2009-01-30 09:40:43 +0900 | [diff] [blame] | 27 | |
rvargas@google.com | 73eb5d0 | 2011-03-25 04:00:20 +0900 | [diff] [blame] | 28 | class BASE_API CommandLine { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 29 | public: |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 30 | #if defined(OS_WIN) |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 31 | // The native command line string type. |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 32 | typedef std::wstring StringType; |
| 33 | #elif defined(OS_POSIX) |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 34 | typedef std::string StringType; |
| 35 | #endif |
| 36 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 37 | typedef std::vector<StringType> StringVector; |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 38 | // The type of map for parsed-out switch key and values. |
| 39 | typedef std::map<std::string, StringType> SwitchMap; |
| 40 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 41 | // A constructor for CommandLines that only carry switches and arguments. |
mattm@chromium.org | af4c7a0 | 2010-10-21 12:36:31 +0900 | [diff] [blame] | 42 | enum NoProgram { NO_PROGRAM }; |
| 43 | explicit CommandLine(NoProgram no_program); |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 44 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 45 | // Construct a new command line with |program| as argv[0]. |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 46 | explicit CommandLine(const FilePath& program); |
| 47 | |
| 48 | #if defined(OS_POSIX) |
| 49 | CommandLine(int argc, const char* const* argv); |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 50 | explicit CommandLine(const StringVector& argv); |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 51 | #endif |
| 52 | |
msw@chromium.org | 96b695b | 2011-03-02 05:47:58 +0900 | [diff] [blame] | 53 | ~CommandLine(); |
| 54 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 55 | // 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.org | 4bbc613 | 2009-01-21 10:00:22 +0900 | [diff] [blame] | 59 | static void Init(int argc, const char* const* argv); |
| 60 | |
evan@chromium.org | fee6d86 | 2009-02-26 06:13:53 +0900 | [diff] [blame] | 61 | // Destroys the current process CommandLine singleton. This is necessary if |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 62 | // 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] | 63 | // 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] | 64 | // 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] | 65 | static void Reset(); |
evan@chromium.org | fee6d86 | 2009-02-26 06:13:53 +0900 | [diff] [blame] | 66 | |
evan@chromium.org | 4bbc613 | 2009-01-21 10:00:22 +0900 | [diff] [blame] | 67 | // Get the singleton CommandLine representing the current process's |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 68 | // command line. Note: returned value is mutable, but not thread safe; |
evan@chromium.org | 757621d | 2009-10-13 07:50:39 +0900 | [diff] [blame] | 69 | // only mutate if you know what you're doing! |
erg@google.com | cb8b2cb | 2010-08-31 05:15:25 +0900 | [diff] [blame] | 70 | static CommandLine* ForCurrentProcess(); |
evanm@google.com | 638e9fb | 2008-08-12 10:14:37 +0900 | [diff] [blame] | 71 | |
evan@chromium.org | 4bbc613 | 2009-01-21 10:00:22 +0900 | [diff] [blame] | 72 | #if defined(OS_WIN) |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 73 | 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.org | 92c59f2 | 2008-10-16 06:59:08 +0900 | [diff] [blame] | 87 | // Returns the original command line string as a vector of strings. |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 88 | const StringVector& argv() const { return argv_; } |
estade@chromium.org | 92c59f2 | 2008-10-16 06:59:08 +0900 | [diff] [blame] | 89 | #endif |
| 90 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 91 | // Returns the program part of the command line string (the first item). |
erg@google.com | 6e67c1d | 2010-07-29 02:25:28 +0900 | [diff] [blame] | 92 | FilePath GetProgram() const; |
evan@chromium.org | 757621d | 2009-10-13 07:50:39 +0900 | [diff] [blame] | 93 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 94 | // 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.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 97 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 98 | // 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.org | 0b90a5c | 2010-07-30 08:02:34 +0900 | [diff] [blame] | 114 | void AppendSwitchPath(const std::string& switch_string, const FilePath& path); |
| 115 | void AppendSwitchNative(const std::string& switch_string, |
| 116 | const StringType& value); |
evan@chromium.org | 3f98514 | 2010-07-30 11:14:22 +0900 | [diff] [blame] | 117 | void AppendSwitchASCII(const std::string& switch_string, |
| 118 | const std::string& value); |
msw@chromium.org | da7a548 | 2011-02-20 15:33:04 +0900 | [diff] [blame] | 119 | void AppendSwitches(const CommandLine& other); |
evan@chromium.org | 0b90a5c | 2010-07-30 08:02:34 +0900 | [diff] [blame] | 120 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 121 | // 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.org | 580ab7f | 2010-08-14 07:10:30 +0900 | [diff] [blame] | 132 | void AppendArg(const std::string& value); |
| 133 | void AppendArgPath(const FilePath& value); |
| 134 | void AppendArgNative(const StringType& value); |
msw@chromium.org | da7a548 | 2011-02-20 15:33:04 +0900 | [diff] [blame] | 135 | void AppendArgs(const CommandLine& other); |
evan@chromium.org | 4bbc613 | 2009-01-21 10:00:22 +0900 | [diff] [blame] | 136 | |
| 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.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 141 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 142 | // Insert a command before the current command. |
| 143 | // Common for debuggers, like "valgrind" or "gdb --args". |
evan@chromium.org | 65dcdf9 | 2010-08-05 03:24:38 +0900 | [diff] [blame] | 144 | void PrependWrapper(const StringType& wrapper); |
agl@chromium.org | c60ca40 | 2009-02-10 09:52:14 +0900 | [diff] [blame] | 145 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 146 | #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.org | 0b90a5c | 2010-07-30 08:02:34 +0900 | [diff] [blame] | 151 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 152 | private: |
msw@chromium.org | 96b695b | 2011-03-02 05:47:58 +0900 | [diff] [blame] | 153 | // Disallow public default construction; a program name must be specified. |
| 154 | CommandLine(); |
sky@google.com | 87bdd75 | 2009-01-30 09:40:43 +0900 | [diff] [blame] | 155 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 156 | // The singleton CommandLine representing the current process's command line. |
evan@chromium.org | 4bbc613 | 2009-01-21 10:00:22 +0900 | [diff] [blame] | 157 | static CommandLine* current_process_commandline_; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 158 | |
evan@chromium.org | 4bbc613 | 2009-01-21 10:00:22 +0900 | [diff] [blame] | 159 | // We store a platform-native version of the command line, used when building |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 160 | // up a new command line to be executed. This ifdef delimits that code. |
evan@chromium.org | 4bbc613 | 2009-01-21 10:00:22 +0900 | [diff] [blame] | 161 | #if defined(OS_WIN) |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 162 | // The quoted, space-separated command line string. |
| 163 | StringType command_line_string_; |
evan@chromium.org | 4bbc613 | 2009-01-21 10:00:22 +0900 | [diff] [blame] | 164 | // The name of the program. |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 165 | StringType program_; |
evan@chromium.org | 4bbc613 | 2009-01-21 10:00:22 +0900 | [diff] [blame] | 166 | #elif defined(OS_POSIX) |
| 167 | // The argv array, with the program name in argv_[0]. |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 168 | StringVector argv_; |
evan@chromium.org | 4bbc613 | 2009-01-21 10:00:22 +0900 | [diff] [blame] | 169 | #endif |
| 170 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 171 | // Parsed-out switch keys and values. |
kinuko@chromium.org | 16013dc | 2010-05-24 19:39:59 +0900 | [diff] [blame] | 172 | SwitchMap switches_; |
evan@chromium.org | 4bbc613 | 2009-01-21 10:00:22 +0900 | [diff] [blame] | 173 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 174 | // Non-switch command line arguments. |
| 175 | StringVector args_; |
evan@chromium.org | 4bbc613 | 2009-01-21 10:00:22 +0900 | [diff] [blame] | 176 | |
msw@chromium.org | 7b2af61 | 2011-03-01 11:28:42 +0900 | [diff] [blame] | 177 | // 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.org | 4bbc613 | 2009-01-21 10:00:22 +0900 | [diff] [blame] | 179 | // CommandLine cl(*CommandLine::ForCurrentProcess()); |
| 180 | // cl.AppendSwitch(...); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 181 | }; |
| 182 | |
deanm@chromium.org | a638cc9 | 2008-10-06 19:25:35 +0900 | [diff] [blame] | 183 | #endif // BASE_COMMAND_LINE_H_ |