OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This class works with command lines: building and parsing. | 5 // This class works with command lines: building and parsing. |
6 // Switches can optionally have a value attached using an equals sign, as in | 6 // Arguments with prefixes ('--', '-', and on Windows, '/') are switches. |
7 // "-switch=value". Arguments that aren't prefixed with a switch prefix are | 7 // Switches will precede all other arguments without switch prefixes. |
8 // saved as extra arguments. An argument of "--" will terminate switch parsing, | 8 // Switches can optionally have values, delimited by '=', e.g., "-switch=value". |
9 // causing everything after to be considered as extra arguments. | 9 // An argument of "--" will terminate switch parsing during initialization, |
10 // interpreting subsequent tokens as non-switch arguments, regardless of prefix. | |
10 | 11 |
11 // There is a singleton read-only CommandLine that represents the command line | 12 // 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(). | 13 // that the current process was started with. It must be initialized in main(). |
13 | 14 |
14 #ifndef BASE_COMMAND_LINE_H_ | 15 #ifndef BASE_COMMAND_LINE_H_ |
15 #define BASE_COMMAND_LINE_H_ | 16 #define BASE_COMMAND_LINE_H_ |
16 #pragma once | 17 #pragma once |
17 | 18 |
18 #include <stddef.h> | 19 #include <stddef.h> |
19 #include <map> | 20 #include <map> |
20 #include <string> | 21 #include <string> |
21 #include <vector> | 22 #include <vector> |
22 | 23 |
23 #include "base/base_api.h" | 24 #include "base/base_api.h" |
24 #include "build/build_config.h" | 25 #include "build/build_config.h" |
25 | 26 |
26 class FilePath; | 27 class FilePath; |
27 | 28 |
28 class BASE_API CommandLine { | 29 class BASE_API CommandLine { |
29 public: | 30 public: |
30 #if defined(OS_WIN) | 31 #if defined(OS_WIN) |
31 // The native command line string type. | 32 // The native command line string type. |
32 typedef std::wstring StringType; | 33 typedef std::wstring StringType; |
33 #elif defined(OS_POSIX) | 34 #elif defined(OS_POSIX) |
34 typedef std::string StringType; | 35 typedef std::string StringType; |
35 #endif | 36 #endif |
36 | 37 |
38 typedef StringType::value_type CharType; | |
37 typedef std::vector<StringType> StringVector; | 39 typedef std::vector<StringType> StringVector; |
38 // The type of map for parsed-out switch key and values. | |
39 typedef std::map<std::string, StringType> SwitchMap; | 40 typedef std::map<std::string, StringType> SwitchMap; |
40 | 41 |
41 // A constructor for CommandLines that only carry switches and arguments. | 42 // A constructor for CommandLines that only carry switches and arguments. |
42 enum NoProgram { NO_PROGRAM }; | 43 enum NoProgram { NO_PROGRAM }; |
43 explicit CommandLine(NoProgram no_program); | 44 explicit CommandLine(NoProgram no_program); |
44 | 45 |
45 // Construct a new command line with |program| as argv[0]. | 46 // Construct a new command line with |program| as argv[0]. |
46 explicit CommandLine(const FilePath& program); | 47 explicit CommandLine(const FilePath& program); |
47 | 48 |
48 #if defined(OS_POSIX) | 49 // Construct a new command line from an argument list. |
49 CommandLine(int argc, const char* const* argv); | 50 CommandLine(int argc, const CharType* const* argv); |
50 explicit CommandLine(const StringVector& argv); | 51 explicit CommandLine(const StringVector& argv); |
51 #endif | |
52 | 52 |
53 ~CommandLine(); | 53 ~CommandLine(); |
54 | 54 |
55 // Initialize the current process CommandLine singleton. On Windows, ignores | 55 // Initialize the current process CommandLine singleton. On Windows, ignores |
56 // its arguments (we instead parse GetCommandLineW() directly) because we | 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 | 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. | 58 // called to set up the command line. |
59 static void Init(int argc, const char* const* argv); | 59 static void Init(int argc, const char* const* argv); |
60 | 60 |
61 // Destroys the current process CommandLine singleton. This is necessary if | 61 // Destroys the current process CommandLine singleton. This is necessary if |
62 // you want to reset the base library to its initial state (for example, in an | 62 // you want to reset the base library to its initial state (for example, in an |
63 // outer library that needs to be able to terminate, and be re-initialized). | 63 // outer library that needs to be able to terminate, and be re-initialized). |
64 // If Init is called only once, as in main(), Reset() is not necessary. | 64 // If Init is called only once, as in main(), Reset() is not necessary. |
65 static void Reset(); | 65 static void Reset(); |
66 | 66 |
67 // Get the singleton CommandLine representing the current process's | 67 // Get the singleton CommandLine representing the current process's |
68 // command line. Note: returned value is mutable, but not thread safe; | 68 // command line. Note: returned value is mutable, but not thread safe; |
69 // only mutate if you know what you're doing! | 69 // only mutate if you know what you're doing! |
70 static CommandLine* ForCurrentProcess(); | 70 static CommandLine* ForCurrentProcess(); |
71 | 71 |
72 #if defined(OS_WIN) | 72 #if defined(OS_WIN) |
73 static CommandLine FromString(const std::wstring& command_line); | 73 static CommandLine FromString(const std::wstring& command_line); |
74 #endif | 74 #endif |
75 | 75 |
76 #if defined(OS_POSIX) | |
77 // Initialize from an argv vector. | 76 // Initialize from an argv vector. |
78 void InitFromArgv(int argc, const char* const* argv); | 77 void InitFromArgv(int argc, const CharType* const* argv); |
79 void InitFromArgv(const StringVector& argv); | 78 void InitFromArgv(const StringVector& argv); |
80 #endif | |
81 | 79 |
82 // Returns the represented command line string. | 80 // Constructs and returns the represented command line string. |
83 // CAUTION! This should be avoided because quoting behavior is unclear. | 81 // CAUTION! This should be avoided because quoting behavior is unclear. |
82 // TODO(msw): Rename GetCommandLineString. | |
84 StringType command_line_string() const; | 83 StringType command_line_string() const; |
85 | 84 |
86 #if defined(OS_POSIX) | |
87 // Returns the original command line string as a vector of strings. | 85 // Returns the original command line string as a vector of strings. |
88 const StringVector& argv() const { return argv_; } | 86 const StringVector& argv() const { return argv_; } |
89 #endif | |
90 | 87 |
91 // Returns the program part of the command line string (the first item). | 88 // Get and Set the program part of the command line string (the first item). |
92 FilePath GetProgram() const; | 89 FilePath GetProgram() const; |
90 void SetProgram(const FilePath& program); | |
93 | 91 |
94 // Returns true if this command line contains the given switch. | 92 // Returns true if this command line contains the given switch. |
95 // (Switch names are case-insensitive). | 93 // (Switch names are case-insensitive). |
96 bool HasSwitch(const std::string& switch_string) const; | 94 bool HasSwitch(const std::string& switch_string) const; |
97 | 95 |
98 // Returns the value associated with the given switch. If the switch has no | 96 // 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. | 97 // value or isn't present, this method returns the empty string. |
100 std::string GetSwitchValueASCII(const std::string& switch_string) const; | 98 std::string GetSwitchValueASCII(const std::string& switch_string) const; |
101 FilePath GetSwitchValuePath(const std::string& switch_string) const; | 99 FilePath GetSwitchValuePath(const std::string& switch_string) const; |
102 StringType GetSwitchValueNative(const std::string& switch_string) const; | 100 StringType GetSwitchValueNative(const std::string& switch_string) const; |
103 | 101 |
104 // Get the number of switches in this process. | 102 // Get the number of switches in this process. |
105 // TODO(msw): Remove unnecessary API. | 103 // TODO(msw): Remove unnecessary API. |
106 size_t GetSwitchCount() const; | 104 size_t GetSwitchCount() const; |
107 | 105 |
108 // Get a copy of all switches, along with their values. | 106 // Get a copy of all switches, along with their values. |
109 const SwitchMap& GetSwitches() const { return switches_; } | 107 const SwitchMap& GetSwitches() const { return switches_; } |
110 | 108 |
111 // Append a switch [with optional value] to the command line. | 109 // Append a switch [with optional value] to the command line. |
112 // CAUTION! Appending a switch after the "--" switch terminator is futile! | 110 // Note: Switches will precede arguments regardless of appending order. |
113 void AppendSwitch(const std::string& switch_string); | 111 void AppendSwitch(const std::string& switch_string); |
114 void AppendSwitchPath(const std::string& switch_string, const FilePath& path); | 112 void AppendSwitchPath(const std::string& switch_string, const FilePath& path); |
115 void AppendSwitchNative(const std::string& switch_string, | 113 void AppendSwitchNative(const std::string& switch_string, |
116 const StringType& value); | 114 const StringType& value); |
117 void AppendSwitchASCII(const std::string& switch_string, | 115 void AppendSwitchASCII(const std::string& switch_string, |
118 const std::string& value); | 116 const std::string& value); |
119 void AppendSwitches(const CommandLine& other); | |
120 | 117 |
121 // Copy a set of switches (and any values) from another command line. | 118 // Copy a set of switches (and any values) from another command line. |
122 // Commonly used when launching a subprocess. | 119 // Commonly used when launching a subprocess. |
123 void CopySwitchesFrom(const CommandLine& source, const char* const switches[], | 120 void CopySwitchesFrom(const CommandLine& source, |
121 const char* const switches[], | |
124 size_t count); | 122 size_t count); |
125 | 123 |
126 // Get the remaining arguments to the command. | 124 // Get the remaining arguments to the command. |
127 const StringVector& args() const { return args_; } | 125 // TODO(msw): Rename GetArgs. |
126 StringVector args() const; | |
128 | 127 |
129 // Append an argument to the command line. Note that the argument is quoted | 128 // 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. | 129 // 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. | 130 // AppendArg is primarily for ASCII; non-ASCII input is interpreted as UTF-8. |
131 // Note: Switches will precede arguments regardless of appending order. | |
132 void AppendArg(const std::string& value); | 132 void AppendArg(const std::string& value); |
133 void AppendArgPath(const FilePath& value); | 133 void AppendArgPath(const FilePath& value); |
134 void AppendArgNative(const StringType& value); | 134 void AppendArgNative(const StringType& value); |
135 void AppendArgs(const CommandLine& other); | |
136 | 135 |
137 // Append the arguments from another command line to this one. | 136 // Append the switches and arguments from another command line to this one. |
138 // If |include_program| is true, include |other|'s program as well. | 137 // If |include_program| is true, include |other|'s program as well. |
139 void AppendArguments(const CommandLine& other, | 138 void AppendArguments(const CommandLine& other, bool include_program); |
140 bool include_program); | |
141 | 139 |
142 // Insert a command before the current command. | 140 // Insert a command before the current command. |
143 // Common for debuggers, like "valgrind" or "gdb --args". | 141 // Common for debuggers, like "valgrind" or "gdb --args". |
144 void PrependWrapper(const StringType& wrapper); | 142 void PrependWrapper(const StringType& wrapper); |
145 | 143 |
146 #if defined(OS_WIN) | 144 #if defined(OS_WIN) |
147 // Initialize by parsing the given command line string. | 145 // Initialize by parsing the given command line string. |
148 // The program name is assumed to be the first item in the string. | 146 // The program name is assumed to be the first item in the string. |
149 void ParseFromString(const std::wstring& command_line); | 147 void ParseFromString(const std::wstring& command_line); |
150 #endif | 148 #endif |
151 | 149 |
152 private: | 150 private: |
153 // Disallow public default construction; a program name must be specified. | 151 // Disallow default constructor; a program name must be explicitly specified. |
154 CommandLine(); | 152 CommandLine(); |
153 // Allow the copy constructor. A common pattern is to copy of the current | |
154 // process's command line and then add some flags to it. For example: | |
155 // CommandLine cl(*CommandLine::ForCurrentProcess()); | |
156 // cl.AppendSwitch(...); | |
155 | 157 |
156 // The singleton CommandLine representing the current process's command line. | 158 // The singleton CommandLine representing the current process's command line. |
157 static CommandLine* current_process_commandline_; | 159 static CommandLine* current_process_commandline_; |
158 | 160 |
159 // We store a platform-native version of the command line, used when building | 161 // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* } |
160 // up a new command line to be executed. This ifdef delimits that code. | |
161 #if defined(OS_WIN) | |
162 // The quoted, space-separated command line string. | |
163 StringType command_line_string_; | |
164 // The name of the program. | |
165 StringType program_; | |
166 #elif defined(OS_POSIX) | |
167 // The argv array, with the program name in argv_[0]. | |
168 StringVector argv_; | 162 StringVector argv_; |
169 #endif | |
170 | 163 |
171 // Parsed-out switch keys and values. | 164 // Parsed-out switch keys and values. |
172 SwitchMap switches_; | 165 SwitchMap switches_; |
173 | 166 |
174 // Non-switch command line arguments. | 167 // The index after the program and switches, any agruments start here. |
Evan Martin
2011/05/13 19:09:41
typo: arguments
msw
2011/05/13 20:28:02
Done.
| |
175 StringVector args_; | 168 size_t begin_args_; |
176 | |
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: | |
179 // CommandLine cl(*CommandLine::ForCurrentProcess()); | |
180 // cl.AppendSwitch(...); | |
181 }; | 169 }; |
182 | 170 |
183 #endif // BASE_COMMAND_LINE_H_ | 171 #endif // BASE_COMMAND_LINE_H_ |
OLD | NEW |