license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 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 | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 4 | |
evan@chromium.org | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 5 | // This class works with command lines: building and parsing. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 6 | // Switches can optionally have a value attached using an equals sign, |
| 7 | // as in "-switch=value". Arguments that aren't prefixed with a |
evan@chromium.org | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 8 | // switch prefix are considered "loose parameters". Switch names are |
| 9 | // case-insensitive. An argument of "--" will terminate switch |
| 10 | // parsing, causing everything after to be considered as loose |
| 11 | // parameters. |
| 12 | |
| 13 | // There is a singleton read-only CommandLine that represents the command |
| 14 | // line that the current process was started with. It must be initialized |
| 15 | // in main() (or whatever the platform's equivalent function is). |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 16 | |
deanm@chromium.org | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 17 | #ifndef BASE_COMMAND_LINE_H_ |
| 18 | #define BASE_COMMAND_LINE_H_ |
evan@chromium.org | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 19 | |
| 20 | #include "build/build_config.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 21 | |
| 22 | #include <map> |
| 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
| 26 | #include "base/basictypes.h" |
evan@chromium.org | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 27 | #include "base/logging.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 28 | #include "base/scoped_ptr.h" |
| 29 | |
sky@google.com | d4515eb | 2009-01-30 00:40:43 | [diff] [blame^] | 30 | class InProcessBrowserTest; |
| 31 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 32 | class CommandLine { |
| 33 | public: |
evanm@google.com | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 34 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 35 | // Creates a parsed version of the given command-line string. |
evan@chromium.org | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 36 | // The program name is assumed to be the first item in the string. |
| 37 | void ParseFromString(const std::wstring& command_line); |
evanm@google.com | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 38 | #elif defined(OS_POSIX) |
evan@chromium.org | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 39 | // Initialize from an argv vector (or directly from main()'s argv). |
tc@google.com | e63d598 | 2008-08-14 22:09:39 | [diff] [blame] | 40 | CommandLine(int argc, const char* const* argv); |
evan@chromium.org | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 41 | explicit CommandLine(const std::vector<std::string>& argv); |
evanm@google.com | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 42 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 43 | |
evan@chromium.org | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 44 | // Construct a new, empty command line. |
| 45 | // |program| is the name of the program to run (aka argv[0]). |
| 46 | // TODO(port): should be a FilePath. |
| 47 | explicit CommandLine(const std::wstring& program); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 48 | |
evan@chromium.org | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 49 | // Initialize the current process CommandLine singleton. On Windows, |
| 50 | // ignores its arguments (we instead parse GetCommandLineW() |
| 51 | // directly) because we don't trust the CRT's parsing of the command |
| 52 | // line, but it still must be called to set up the command line. |
| 53 | static void Init(int argc, const char* const* argv); |
| 54 | |
| 55 | // Get the singleton CommandLine representing the current process's |
| 56 | // command line. |
| 57 | static const CommandLine* ForCurrentProcess() { |
| 58 | DCHECK(current_process_commandline_); |
| 59 | return current_process_commandline_; |
| 60 | } |
evanm@google.com | 1a48f31 | 2008-08-12 01:14:37 | [diff] [blame] | 61 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 62 | // Returns true if this command line contains the given switch. |
| 63 | // (Switch names are case-insensitive.) |
| 64 | bool HasSwitch(const std::wstring& switch_string) const; |
| 65 | |
| 66 | // Returns the value associated with the given switch. If the |
| 67 | // switch has no value or isn't present, this method returns |
| 68 | // the empty string. |
| 69 | std::wstring GetSwitchValue(const std::wstring& switch_string) const; |
| 70 | |
evan@chromium.org | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 71 | // Get the remaining arguments to the command. |
| 72 | // WARNING: this is incorrect on POSIX; we must do string conversions. |
| 73 | std::vector<std::wstring> GetLooseValues() const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 74 | |
evan@chromium.org | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 75 | #if defined(OS_WIN) |
| 76 | // Returns the original command line string. |
| 77 | const std::wstring& command_line_string() const { |
| 78 | return command_line_string_; |
| 79 | } |
| 80 | #elif defined(OS_POSIX) |
estade@chromium.org | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 81 | // Returns the original command line string as a vector of strings. |
evan@chromium.org | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 82 | const std::vector<std::string>& argv() const { |
| 83 | return argv_; |
| 84 | } |
estade@chromium.org | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 85 | #endif |
| 86 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 87 | // Returns the program part of the command line string (the first item). |
| 88 | std::wstring program() const; |
deanm@chromium.org | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 89 | |
estade@chromium.org | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 90 | // Return a copy of the string prefixed with a switch prefix. |
| 91 | // Used internally. |
| 92 | static std::wstring PrefixedSwitchString(const std::wstring& switch_string); |
| 93 | |
| 94 | // Return a copy of the string prefixed with a switch prefix, |
| 95 | // and appended with the given value. Used internally. |
| 96 | static std::wstring PrefixedSwitchStringWithValue( |
| 97 | const std::wstring& switch_string, |
| 98 | const std::wstring& value_string); |
| 99 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 100 | // Appends the given switch string (preceded by a space and a switch |
| 101 | // prefix) to the given string. |
evan@chromium.org | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 102 | void AppendSwitch(const std::wstring& switch_string); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 103 | |
| 104 | // Appends the given switch string (preceded by a space and a switch |
| 105 | // prefix) to the given string, with the given value attached. |
evan@chromium.org | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 106 | void AppendSwitchWithValue(const std::wstring& switch_string, |
| 107 | const std::wstring& value_string); |
| 108 | |
| 109 | // Append a loose value to the command line. |
| 110 | void AppendLooseValue(const std::wstring& value); |
| 111 | |
| 112 | // Append the arguments from another command line to this one. |
| 113 | // If |include_program| is true, include |other|'s program as well. |
| 114 | void AppendArguments(const CommandLine& other, |
| 115 | bool include_program); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 116 | |
| 117 | private: |
sky@google.com | d4515eb | 2009-01-30 00:40:43 | [diff] [blame^] | 118 | friend class InProcessBrowserTest; |
| 119 | |
evan@chromium.org | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 120 | CommandLine() {} |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 121 | |
sky@google.com | d4515eb | 2009-01-30 00:40:43 | [diff] [blame^] | 122 | // Used by InProcessBrowserTest. |
| 123 | static CommandLine* ForCurrentProcessMutable() { |
| 124 | DCHECK(current_process_commandline_); |
| 125 | return current_process_commandline_; |
| 126 | } |
| 127 | |
evan@chromium.org | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 128 | // The singleton CommandLine instance representing the current process's |
| 129 | // command line. |
| 130 | static CommandLine* current_process_commandline_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 131 | |
evan@chromium.org | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 132 | // We store a platform-native version of the command line, used when building |
| 133 | // up a new command line to be executed. This ifdef delimits that code. |
| 134 | |
| 135 | #if defined(OS_WIN) |
| 136 | // The quoted, space-separated command-line string. |
| 137 | std::wstring command_line_string_; |
| 138 | |
| 139 | // The name of the program. |
| 140 | std::wstring program_; |
| 141 | |
| 142 | // The type of native command line arguments. |
| 143 | typedef std::wstring StringType; |
| 144 | |
| 145 | #elif defined(OS_POSIX) |
| 146 | // The argv array, with the program name in argv_[0]. |
| 147 | std::vector<std::string> argv_; |
| 148 | |
| 149 | // The type of native command line arguments. |
| 150 | typedef std::string StringType; |
| 151 | |
| 152 | // Shared by the two POSIX constructor forms. Initalize from argv_. |
| 153 | void InitFromArgv(); |
| 154 | #endif |
| 155 | |
| 156 | // Returns true and fills in |switch_string| and |switch_value| |
| 157 | // if |parameter_string| represents a switch. |
| 158 | static bool IsSwitch(const StringType& parameter_string, |
| 159 | std::string* switch_string, |
| 160 | StringType* switch_value); |
| 161 | |
| 162 | // Parsed-out values. |
| 163 | std::map<std::string, StringType> switches_; |
| 164 | |
| 165 | // Non-switch command-line arguments. |
| 166 | std::vector<StringType> loose_values_; |
| 167 | |
| 168 | // We allow copy constructors, because a common pattern is to grab a |
| 169 | // copy of the current process's command line and then add some |
| 170 | // flags to it. E.g.: |
| 171 | // CommandLine cl(*CommandLine::ForCurrentProcess()); |
| 172 | // cl.AppendSwitch(...); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 173 | }; |
| 174 | |
deanm@chromium.org | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 175 | #endif // BASE_COMMAND_LINE_H_ |