Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(139)

Side by Side Diff: base/command_line.h

Issue 19925003: WIP - command line experiment Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/base_switches.h ('k') | base/command_line.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <stdint.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_export.h" 24 #include "base/base_export.h"
24 #include "build/build_config.h" 25 #include "build/build_config.h"
25 26
26 namespace base { 27 namespace base {
27 class FilePath; 28 class FilePath;
28 } 29 }
29 30
30 class BASE_EXPORT CommandLine { 31 class BASE_EXPORT CommandLine {
31 public: 32 public:
32 #if defined(OS_WIN) 33 #if defined(OS_WIN)
33 // The native command line string type. 34 // The native command line string type.
34 typedef std::wstring StringType; 35 typedef std::wstring StringType;
35 #elif defined(OS_POSIX) 36 #elif defined(OS_POSIX)
36 typedef std::string StringType; 37 typedef std::string StringType;
37 #endif 38 #endif
38 39
39 typedef StringType::value_type CharType; 40 typedef StringType::value_type CharType;
40 typedef std::vector<StringType> StringVector; 41 typedef std::vector<StringType> StringVector;
41 typedef std::map<std::string, StringType> SwitchMap; 42 typedef std::map<std::string, StringType> SwitchMap;
42 43
44 struct ExplicitStdString {
45 ExplicitStdString(const std::string& str) : s(str) {}
46 const std::string& s;
47 };
48
43 // A constructor for CommandLines that only carry switches and arguments. 49 // A constructor for CommandLines that only carry switches and arguments.
44 enum NoProgram { NO_PROGRAM }; 50 enum NoProgram { NO_PROGRAM };
45 explicit CommandLine(NoProgram no_program); 51 explicit CommandLine(NoProgram no_program);
46 52
47 // Construct a new command line with |program| as argv[0]. 53 // Construct a new command line with |program| as argv[0].
48 explicit CommandLine(const base::FilePath& program); 54 explicit CommandLine(const base::FilePath& program);
49 55
50 // Construct a new command line from an argument list. 56 // Construct a new command line from an argument list.
51 CommandLine(int argc, const CharType* const* argv); 57 CommandLine(int argc, const CharType* const* argv);
52 explicit CommandLine(const StringVector& argv); 58 explicit CommandLine(const StringVector& argv);
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 101
96 // Returns the original command line string as a vector of strings. 102 // Returns the original command line string as a vector of strings.
97 const StringVector& argv() const { return argv_; } 103 const StringVector& argv() const { return argv_; }
98 104
99 // Get and Set the program part of the command line string (the first item). 105 // Get and Set the program part of the command line string (the first item).
100 base::FilePath GetProgram() const; 106 base::FilePath GetProgram() const;
101 void SetProgram(const base::FilePath& program); 107 void SetProgram(const base::FilePath& program);
102 108
103 // Returns true if this command line contains the given switch. 109 // Returns true if this command line contains the given switch.
104 // (Switch names are case-insensitive). 110 // (Switch names are case-insensitive).
105 bool HasSwitch(const std::string& switch_string) const; 111 bool HasStringSwitch(const std::string& switch_string) const;
112 bool HasSwitch(const ExplicitStdString& switch_string) const {
113 return HasStringSwitch(switch_string.s);
114 }
115 bool HasSwitch(const char* switch_string, size_t string_length) const;
116
117 template <size_t N>
118 bool HasSwitch(const char (&constant_switch_string)[N]) const {
119 // Note that N includes a null terminator, so this will generate a compile
120 // error if called with an empty string constant.
121 if (N <= 65 &&
122 (switch_lengths_mask_ & (static_cast<uint64_t>(1) << (N - 2))) == 0) {
123 return false;
124 }
125
126 return HasSwitch(constant_switch_string, N - 1);
127 }
106 128
107 // Returns the value associated with the given switch. If the switch has no 129 // Returns the value associated with the given switch. If the switch has no
108 // value or isn't present, this method returns the empty string. 130 // value or isn't present, this method returns the empty string.
109 std::string GetSwitchValueASCII(const std::string& switch_string) const; 131 std::string GetSwitchValueASCII(const std::string& switch_string) const;
110 base::FilePath GetSwitchValuePath(const std::string& switch_string) const; 132 base::FilePath GetSwitchValuePath(const std::string& switch_string) const;
111 StringType GetSwitchValueNative(const std::string& switch_string) const; 133 StringType GetSwitchValueNative(const std::string& switch_string) const;
112 134
113 // Get a copy of all switches, along with their values. 135 // Get a copy of all switches, along with their values.
114 const SwitchMap& GetSwitches() const { return switches_; } 136 const SwitchMap& GetSwitches() const { return switches_; }
115 137
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 static CommandLine* current_process_commandline_; 188 static CommandLine* current_process_commandline_;
167 189
168 // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* } 190 // The argv array: { program, [(--|-|/)switch[=value]]*, [--], [argument]* }
169 StringVector argv_; 191 StringVector argv_;
170 192
171 // Parsed-out switch keys and values. 193 // Parsed-out switch keys and values.
172 SwitchMap switches_; 194 SwitchMap switches_;
173 195
174 // The index after the program and switches, any arguments start here. 196 // The index after the program and switches, any arguments start here.
175 size_t begin_args_; 197 size_t begin_args_;
198
199 // A bitset, with a 1 at bit postion |i| indicating that the command line
200 // includes a switch of string length |i| + 1 ascii characters.
201 uint64_t switch_lengths_mask_;
176 }; 202 };
177 203
178 #endif // BASE_COMMAND_LINE_H_ 204 #endif // BASE_COMMAND_LINE_H_
OLDNEW
« no previous file with comments | « base/base_switches.h ('k') | base/command_line.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698