OLD | NEW |
| (Empty) |
1 // Copyright 2008-2009 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 | |
16 #ifndef OMAHA_BASE_COMMAND_LINE_PARSER_H__ | |
17 #define OMAHA_BASE_COMMAND_LINE_PARSER_H__ | |
18 | |
19 #include <windows.h> | |
20 #include <atlstr.h> | |
21 #include <map> | |
22 #include <vector> | |
23 #include "base/basictypes.h" | |
24 #include "base/scoped_ptr.h" | |
25 | |
26 namespace omaha { | |
27 | |
28 namespace internal { | |
29 | |
30 class CommandLineParserArgs; | |
31 | |
32 } // namespace internal | |
33 | |
34 // This class will parse a command line either from a string or in argc/argv | |
35 // format. It then provides information about the parsed command line. | |
36 // When passing the string, make sure it includes the program name as the first | |
37 // argument. | |
38 // A "switch" is an argument preceded by "/". Each switch can take 0..n | |
39 // arguments. | |
40 // Example: foo.exe /sw a "b b" /sw2 /sw3 | |
41 // * foo.exe is the program name | |
42 // * sw, sw2, and sw3 are switches. | |
43 // * a and 'b b' (without the quotes) are the arguments to the sw switch. | |
44 // * sw has 2 arguments and sw2 and sw3 have no arguments. | |
45 class CommandLineParser { | |
46 public: | |
47 CommandLineParser(); | |
48 ~CommandLineParser(); | |
49 | |
50 // Parses the command line from a string. Must include the program name (e.g. | |
51 // foo.exe) as the first value in the command line. | |
52 HRESULT ParseFromString(const wchar_t* command_line); | |
53 | |
54 // Parses the command line form argc/argv syntax. Makes the assumption that | |
55 // argv[0] is the program name (e.g. foo.exe). | |
56 HRESULT ParseFromArgv(int argc, wchar_t** argv); | |
57 | |
58 // TODO(Omaha): Name these methods "Required". | |
59 // Gets the number of required switches in the parsed command line. | |
60 int GetSwitchCount() const; | |
61 | |
62 // Returns the required switch at a particular index. | |
63 HRESULT GetSwitchNameAtIndex(int index, CString* switch_name) const; | |
64 | |
65 // Returns true if a required switch with the name switch_name is found. | |
66 bool HasSwitch(const CString& switch_name) const; | |
67 | |
68 // Returns the number of required arguments for required switch switch_name. | |
69 HRESULT GetSwitchArgumentCount(const CString& switch_name, | |
70 int* count) const; | |
71 | |
72 // Returns the value of a required switch argument at the specified offset. | |
73 HRESULT GetSwitchArgumentValue(const CString& switch_name, | |
74 int argument_index, | |
75 CString* argument_value) const; | |
76 | |
77 // Functions that have the same functionality as the above functions, | |
78 // except they operate on the optional switches. | |
79 int GetOptionalSwitchCount() const; | |
80 bool HasOptionalSwitch(const CString& switch_name) const; | |
81 HRESULT GetOptionalSwitchNameAtIndex(int index, CString* switch_name) const; | |
82 HRESULT GetOptionalSwitchArgumentCount(const CString& switch_name, | |
83 int* count) const; | |
84 HRESULT GetOptionalSwitchArgumentValue( | |
85 const CString& switch_name, | |
86 int argument_index, | |
87 CString* argument_value) const; | |
88 | |
89 private: | |
90 bool IsSwitch(const CString& param) const; | |
91 HRESULT StripSwitchNameFromArgv(const CString& param, CString* switch_name); | |
92 bool IsOptionalSwitch(const CString& param) const; | |
93 HRESULT StripOptionalSwitchNameFromArgv(const CString& param, CString* name); | |
94 | |
95 void Reset(); | |
96 | |
97 HRESULT AddSwitch(const CString& switch_name); | |
98 HRESULT AddSwitchArgument(const CString& switch_name, | |
99 const CString& argument_value); | |
100 HRESULT AddOptionalSwitch(const CString& switch_name); | |
101 HRESULT AddOptionalSwitchArgument(const CString& switch_name, | |
102 const CString& argument_value); | |
103 | |
104 scoped_ptr<internal::CommandLineParserArgs> required_args_; | |
105 scoped_ptr<internal::CommandLineParserArgs> optional_args_; | |
106 | |
107 DISALLOW_EVIL_CONSTRUCTORS(CommandLineParser); | |
108 }; | |
109 | |
110 } // namespace omaha | |
111 | |
112 #endif // OMAHA_BASE_COMMAND_LINE_PARSER_H__ | |
113 | |
OLD | NEW |