1 | // |
---|
2 | // OptionProcessor.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Util/include/Poco/Util/OptionProcessor.h#1 $ |
---|
5 | // |
---|
6 | // Library: Util |
---|
7 | // Package: Options |
---|
8 | // Module: OptionProcessor |
---|
9 | // |
---|
10 | // Definition of the OptionProcessor class. |
---|
11 | // |
---|
12 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Util_OptionProcessor_INCLUDED |
---|
40 | #define Util_OptionProcessor_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Util/Util.h" |
---|
44 | #include <set> |
---|
45 | |
---|
46 | |
---|
47 | namespace Poco { |
---|
48 | namespace Util { |
---|
49 | |
---|
50 | |
---|
51 | class OptionSet; |
---|
52 | |
---|
53 | |
---|
54 | class Util_API OptionProcessor |
---|
55 | /// An OptionProcessor is used to process the command line |
---|
56 | /// arguments of an application. |
---|
57 | /// |
---|
58 | /// The process() method takes an argument from the command line. |
---|
59 | /// If that argument starts with an option prefix, the argument |
---|
60 | /// is further processed. Otherwise, the argument is ignored and |
---|
61 | /// false is ignored. The argument must match one of the options |
---|
62 | /// given in the OptionSet that is passed to the OptionProcessor |
---|
63 | /// with the constructor. If an option is part of a group, at most |
---|
64 | /// one option of the group can be passed to the OptionProcessor. |
---|
65 | /// Otherwise an IncompatibleOptionsException is thrown. |
---|
66 | /// If the same option is given multiple times, but the option |
---|
67 | /// is not repeatable, a DuplicateOptionException is thrown. |
---|
68 | /// If the option is not recognized, a UnexpectedArgumentException |
---|
69 | /// is thrown. |
---|
70 | /// If the option requires an argument, but none is given, an |
---|
71 | /// MissingArgumentException is thrown. |
---|
72 | /// If no argument is expected, but one is present, a |
---|
73 | /// UnexpectedArgumentException is thrown. |
---|
74 | /// If a partial option name is ambiguous, an AmbiguousOptionException |
---|
75 | /// is thrown. |
---|
76 | /// |
---|
77 | /// The OptionProcessor supports two modes: Unix mode and default mode. |
---|
78 | /// In Unix mode, the option prefix is a dash '-'. A dash must be followed |
---|
79 | /// by a short option name, or another dash, followed by a (partial) |
---|
80 | /// long option name. |
---|
81 | /// In default mode, the option prefix is a slash '/', followed by |
---|
82 | /// a (partial) long option name. |
---|
83 | /// If the special option '--' is encountered in Unix mode, all following |
---|
84 | /// options are ignored. |
---|
85 | { |
---|
86 | public: |
---|
87 | OptionProcessor(const OptionSet& options); |
---|
88 | /// Creates the OptionProcessor, using the given OptionSet. |
---|
89 | |
---|
90 | ~OptionProcessor(); |
---|
91 | /// Destroys the OptionProcessor. |
---|
92 | |
---|
93 | void setUnixStyle(bool flag); |
---|
94 | /// Enables (flag == true) or disables (flag == false) Unix-style |
---|
95 | /// option processing. |
---|
96 | /// |
---|
97 | /// If Unix-style processing is enabled, options are expected to |
---|
98 | /// begin with a single or a double dash ('-' or '--', respectively). |
---|
99 | /// A single dash must be followed by a short option name. A double |
---|
100 | /// dash must be followed by a (partial) full option name. |
---|
101 | /// |
---|
102 | /// If Unix-style processing is disabled, options are expected to |
---|
103 | /// begin with a slash ('/'), followed by a (partial) full option name. |
---|
104 | |
---|
105 | bool isUnixStyle() const; |
---|
106 | /// Returns true iff Unix-style option processing is enabled. |
---|
107 | |
---|
108 | bool process(const std::string& argument, std::string& optionName, std::string& optionArg); |
---|
109 | /// Examines and processes the given command line argument. |
---|
110 | /// |
---|
111 | /// If the argument begins with an option prefix, the option is processed |
---|
112 | /// and true is returned. The full option name is stored in optionName and the |
---|
113 | /// option argument, if present, is stored in optionArg. |
---|
114 | /// |
---|
115 | /// If the option does not begin with an option prefix, false is returned. |
---|
116 | |
---|
117 | void checkRequired() const; |
---|
118 | /// Checks if all required options have been processed. |
---|
119 | /// |
---|
120 | /// Does nothing if all required options have been processed. |
---|
121 | /// Throws a MissingOptionException otherwise. |
---|
122 | |
---|
123 | private: |
---|
124 | bool processUnix(const std::string& argument, std::string& optionName, std::string& optionArg); |
---|
125 | bool processDefault(const std::string& argument, std::string& optionName, std::string& optionArg); |
---|
126 | bool processCommon(const std::string& option, bool isShort, std::string& optionName, std::string& optionArg); |
---|
127 | |
---|
128 | const OptionSet& _options; |
---|
129 | bool _unixStyle; |
---|
130 | bool _ignore; |
---|
131 | std::set<std::string> _groups; |
---|
132 | std::set<std::string> _specifiedOptions; |
---|
133 | }; |
---|
134 | |
---|
135 | |
---|
136 | // |
---|
137 | // inlines |
---|
138 | // |
---|
139 | inline bool OptionProcessor::isUnixStyle() const |
---|
140 | { |
---|
141 | return _unixStyle; |
---|
142 | } |
---|
143 | |
---|
144 | |
---|
145 | } } // namespace Poco::Util |
---|
146 | |
---|
147 | |
---|
148 | #endif // Util_OptionProcessor_INCLUDED |
---|