1 | // |
---|---|
2 | // OptionSet.cpp |
3 | // |
4 | // Library: Util |
5 | // Package: Options |
6 | // Module: OptionSet |
7 | // |
8 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/Util/OptionSet.h" |
16 | #include "Poco/Util/OptionException.h" |
17 | #include "Poco/Exception.h" |
18 | |
19 | |
20 | namespace Poco { |
21 | namespace Util { |
22 | |
23 | |
24 | OptionSet::OptionSet() |
25 | { |
26 | } |
27 | |
28 | |
29 | OptionSet::OptionSet(const OptionSet& options): |
30 | _options(options._options) |
31 | { |
32 | } |
33 | |
34 | |
35 | OptionSet::~OptionSet() |
36 | { |
37 | } |
38 | |
39 | |
40 | OptionSet& OptionSet::operator = (const OptionSet& options) |
41 | { |
42 | if (&options != this) |
43 | _options = options._options; |
44 | return *this; |
45 | } |
46 | |
47 | |
48 | void OptionSet::addOption(const Option& option) |
49 | { |
50 | poco_assert (!option.fullName().empty()); |
51 | OptionVec::const_iterator it = _options.begin(); |
52 | OptionVec::const_iterator itEnd = _options.end(); |
53 | for (; it != itEnd; ++it) |
54 | { |
55 | if (it->fullName() == option.fullName()) |
56 | { |
57 | throw DuplicateOptionException(it->fullName()); |
58 | } |
59 | } |
60 | |
61 | _options.push_back(option); |
62 | } |
63 | |
64 | |
65 | bool OptionSet::hasOption(const std::string& name, bool matchShort) const |
66 | { |
67 | bool found = false; |
68 | for (Iterator it = _options.begin(); it != _options.end(); ++it) |
69 | { |
70 | if ((matchShort && it->matchesShort(name)) || (!matchShort && it->matchesFull(name))) |
71 | { |
72 | if (!found) |
73 | found = true; |
74 | else |
75 | return false; |
76 | } |
77 | } |
78 | return found; |
79 | } |
80 | |
81 | |
82 | const Option& OptionSet::getOption(const std::string& name, bool matchShort) const |
83 | { |
84 | const Option* pOption = 0; |
85 | for (Iterator it = _options.begin(); it != _options.end(); ++it) |
86 | { |
87 | if ((matchShort && it->matchesShort(name)) || (!matchShort && it->matchesPartial(name))) |
88 | { |
89 | if (!pOption) |
90 | { |
91 | pOption = &*it; |
92 | if (!matchShort && it->matchesFull(name)) |
93 | break; |
94 | } |
95 | else if (!matchShort && it->matchesFull(name)) |
96 | { |
97 | pOption = &*it; |
98 | break; |
99 | } |
100 | else throw AmbiguousOptionException(name); |
101 | } |
102 | } |
103 | if (pOption) |
104 | return *pOption; |
105 | else |
106 | throw UnknownOptionException(name); |
107 | } |
108 | |
109 | |
110 | OptionSet::Iterator OptionSet::begin() const |
111 | { |
112 | return _options.begin(); |
113 | } |
114 | |
115 | |
116 | OptionSet::Iterator OptionSet::end() const |
117 | { |
118 | return _options.end(); |
119 | } |
120 | |
121 | |
122 | } } // namespace Poco::Util |
123 |