Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // Arguments with prefixes ('--', '-', and on Windows, '/') are switches. | 6 // Arguments with prefixes ('--', '-', and on Windows, '/') are switches. |
| 7 // Switches will precede all other arguments without switch prefixes. | 7 // Switches will precede all other arguments without switch prefixes. |
| 8 // Switches can optionally have values, delimited by '=', e.g., "-switch=value". | 8 // Switches can optionally have values, delimited by '=', e.g., "-switch=value". |
| 9 // An argument of "--" will terminate switch parsing during initialization, | 9 // An argument of "--" will terminate switch parsing during initialization, |
| 10 // interpreting subsequent tokens as non-switch arguments, regardless of prefix. | 10 // interpreting subsequent tokens as non-switch arguments, regardless of prefix. |
| 11 | 11 |
| 12 // 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 |
| 13 // 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(). |
| 14 | 14 |
| 15 #ifndef BASE_COMMAND_LINE_H_ | 15 #ifndef BASE_COMMAND_LINE_H_ |
| 16 #define BASE_COMMAND_LINE_H_ | 16 #define BASE_COMMAND_LINE_H_ |
| 17 | 17 |
| 18 #include <stddef.h> | 18 #include <stddef.h> |
| 19 #include <map> | 19 #include <map> |
| 20 #include <string> | 20 #include <string> |
| 21 #include <vector> | 21 #include <vector> |
| 22 | 22 |
| 23 #include "base/base_export.h" | 23 #include "base/base_export.h" |
| 24 #include "base/strings/string16.h" | 24 #include "base/strings/string16.h" |
| 25 #include "base/strings/string_piece.h" | |
| 25 #include "build/build_config.h" | 26 #include "build/build_config.h" |
| 26 | 27 |
| 27 namespace base { | 28 namespace base { |
| 28 | 29 |
| 29 class FilePath; | 30 class FilePath; |
| 30 | 31 |
| 31 class BASE_EXPORT CommandLine { | 32 class BASE_EXPORT CommandLine { |
| 32 public: | 33 public: |
| 33 #if defined(OS_WIN) | 34 #if defined(OS_WIN) |
| 34 // The native command line string type. | 35 // The native command line string type. |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 45 enum NoProgram { NO_PROGRAM }; | 46 enum NoProgram { NO_PROGRAM }; |
| 46 explicit CommandLine(NoProgram no_program); | 47 explicit CommandLine(NoProgram no_program); |
| 47 | 48 |
| 48 // Construct a new command line with |program| as argv[0]. | 49 // Construct a new command line with |program| as argv[0]. |
| 49 explicit CommandLine(const FilePath& program); | 50 explicit CommandLine(const FilePath& program); |
| 50 | 51 |
| 51 // Construct a new command line from an argument list. | 52 // Construct a new command line from an argument list. |
| 52 CommandLine(int argc, const CharType* const* argv); | 53 CommandLine(int argc, const CharType* const* argv); |
| 53 explicit CommandLine(const StringVector& argv); | 54 explicit CommandLine(const StringVector& argv); |
| 54 | 55 |
| 56 // Override copy and assign to ensure |switches_by_stringpiece_| is valid. | |
| 57 CommandLine(const CommandLine& other); | |
|
tapted
2015/04/15 00:07:53
it's tempting to make this explicit... I bet a lot
| |
| 58 CommandLine& operator=(const CommandLine& other); | |
| 59 | |
| 55 ~CommandLine(); | 60 ~CommandLine(); |
| 56 | 61 |
| 57 #if defined(OS_WIN) | 62 #if defined(OS_WIN) |
| 58 // By default this class will treat command-line arguments beginning with | 63 // By default this class will treat command-line arguments beginning with |
| 59 // slashes as switches on Windows, but not other platforms. | 64 // slashes as switches on Windows, but not other platforms. |
| 60 // | 65 // |
| 61 // If this behavior is inappropriate for your application, you can call this | 66 // If this behavior is inappropriate for your application, you can call this |
| 62 // function BEFORE initializing the current process' global command line | 67 // function BEFORE initializing the current process' global command line |
| 63 // object and the behavior will be the same as Posix systems (only hyphens | 68 // object and the behavior will be the same as Posix systems (only hyphens |
| 64 // begin switches, everything else will be an arg). | 69 // begin switches, everything else will be an arg). |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 135 #endif | 140 #endif |
| 136 | 141 |
| 137 // Returns the original command line string as a vector of strings. | 142 // Returns the original command line string as a vector of strings. |
| 138 const StringVector& argv() const { return argv_; } | 143 const StringVector& argv() const { return argv_; } |
| 139 | 144 |
| 140 // Get and Set the program part of the command line string (the first item). | 145 // Get and Set the program part of the command line string (the first item). |
| 141 FilePath GetProgram() const; | 146 FilePath GetProgram() const; |
| 142 void SetProgram(const FilePath& program); | 147 void SetProgram(const FilePath& program); |
| 143 | 148 |
| 144 // Returns true if this command line contains the given switch. | 149 // Returns true if this command line contains the given switch. |
| 145 // (Switch names are case-insensitive). | 150 // Switch names should only be lowercase. |
|
tapted
2015/04/15 00:07:53
nit: should only -> must
jackhou1
2015/04/17 00:56:10
Done.
| |
| 146 // The second override provides an optimized version to avoid inlining the | 151 // The second override provides an optimized version to avoid inlining the |
| 147 // codegen for the string allocation. | 152 // codegen for the string allocation. |
|
tapted
2015/04/15 00:07:53
This last sentence in the comment is no longer val
jackhou1
2015/04/17 00:56:10
Done.
| |
| 148 bool HasSwitch(const std::string& switch_string) const; | 153 bool HasSwitch(const base::StringPiece& switch_string) const; |
| 149 bool HasSwitch(const char switch_constant[]) const; | |
| 150 | 154 |
| 151 // Returns the value associated with the given switch. If the switch has no | 155 // Returns the value associated with the given switch. If the switch has no |
| 152 // value or isn't present, this method returns the empty string. | 156 // value or isn't present, this method returns the empty string. |
| 153 std::string GetSwitchValueASCII(const std::string& switch_string) const; | 157 // Switch names should only be lowercase. |
| 154 FilePath GetSwitchValuePath(const std::string& switch_string) const; | 158 std::string GetSwitchValueASCII(const base::StringPiece& switch_string) const; |
| 155 StringType GetSwitchValueNative(const std::string& switch_string) const; | 159 FilePath GetSwitchValuePath(const base::StringPiece& switch_string) const; |
| 160 StringType GetSwitchValueNative(const base::StringPiece& switch_string) const; | |
| 156 | 161 |
| 157 // Get a copy of all switches, along with their values. | 162 // Get a reference to the internal switch map. |
| 158 const SwitchMap& GetSwitches() const { return switches_; } | 163 const SwitchMap& GetSwitches() const { return switches_; } |
| 159 | 164 |
| 160 // Append a switch [with optional value] to the command line. | 165 // Append a switch [with optional value] to the command line. |
| 161 // Note: Switches will precede arguments regardless of appending order. | 166 // Note: Switches will precede arguments regardless of appending order. |
| 162 void AppendSwitch(const std::string& switch_string); | 167 void AppendSwitch(const std::string& switch_string); |
| 163 void AppendSwitchPath(const std::string& switch_string, | 168 void AppendSwitchPath(const std::string& switch_string, |
| 164 const FilePath& path); | 169 const FilePath& path); |
| 165 void AppendSwitchNative(const std::string& switch_string, | 170 void AppendSwitchNative(const std::string& switch_string, |
| 166 const StringType& value); | 171 const StringType& value); |
| 167 void AppendSwitchASCII(const std::string& switch_string, | 172 void AppendSwitchASCII(const std::string& switch_string, |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 216 | 221 |
| 217 // The singleton CommandLine representing the current process's command line. | 222 // The singleton CommandLine representing the current process's command line. |
| 218 static CommandLine* current_process_commandline_; | 223 static CommandLine* current_process_commandline_; |
| 219 | 224 |
| 220 // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* } | 225 // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* } |
| 221 StringVector argv_; | 226 StringVector argv_; |
| 222 | 227 |
| 223 // Parsed-out switch keys and values. | 228 // Parsed-out switch keys and values. |
| 224 SwitchMap switches_; | 229 SwitchMap switches_; |
| 225 | 230 |
| 231 // A mirror of |switches_| with only references to the actual strings. | |
| 232 // Used for allocation-free lookups. | |
|
tapted
2015/04/15 00:07:53
nit: mention mapped_type points to an object in |s
jackhou1
2015/04/17 00:56:10
Done.
| |
| 233 std::map<base::StringPiece, const StringType*> switches_by_stringpiece_; | |
|
tapted
2015/04/15 00:07:53
It might be less confusing to just store a value f
jackhou1
2015/04/17 00:56:10
I'm think the memory/performance impact would be n
| |
| 234 | |
| 226 // The index after the program and switches, any arguments start here. | 235 // The index after the program and switches, any arguments start here. |
| 227 size_t begin_args_; | 236 size_t begin_args_; |
| 228 }; | 237 }; |
| 229 | 238 |
| 230 } // namespace base | 239 } // namespace base |
| 231 | 240 |
| 232 #endif // BASE_COMMAND_LINE_H_ | 241 #endif // BASE_COMMAND_LINE_H_ |
| OLD | NEW |