4
// $Id: //poco/1.2/Util/src/Option.cpp#3 $
10
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
13
// Permission is hereby granted, free of charge, to any person or organization
14
// obtaining a copy of the software and accompanying documentation covered by
15
// this license (the "Software") to use, reproduce, display, distribute,
16
// execute, and transmit the Software, and to prepare derivative works of the
17
// Software, and to permit third-parties to whom the Software is furnished to
18
// do so, all subject to the following:
20
// The copyright notices in the Software and this entire statement, including
21
// the above license grant, this restriction and the following disclaimer,
22
// must be included in all copies of the Software, in whole or in part, and
23
// all derivative works of the Software, unless such copies or derivative
24
// works are solely in the form of machine-executable object code generated by
25
// a source language processor.
27
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
30
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
31
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
32
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
33
// DEALINGS IN THE SOFTWARE.
37
#include "Poco/Util/Option.h"
38
#include "Poco/Util/OptionException.h"
39
#include "Poco/Util/Validator.h"
40
#include "Poco/Util/AbstractConfiguration.h"
41
#include "Poco/String.h"
63
Option::Option(const Option& option):
64
_shortName(option._shortName),
65
_fullName(option._fullName),
66
_description(option._description),
67
_required(option._required),
68
_repeatable(option._repeatable),
69
_argName(option._argName),
70
_argRequired(option._argRequired),
71
_group(option._group),
72
_binding(option._binding),
73
_pValidator(option._pValidator),
74
_pCallback(option._pCallback),
75
_pConfig(option._pConfig)
77
if (_pValidator) _pValidator->duplicate();
78
if (_pCallback) _pCallback = _pCallback->clone();
79
if (_pConfig) _pConfig->duplicate();
83
Option::Option(const std::string& fullName, const std::string& shortName):
84
_shortName(shortName),
96
Option::Option(const std::string& fullName, const std::string& shortName, const std::string& description, bool required):
97
_shortName(shortName),
99
_description(description),
110
Option::Option(const std::string& fullName, const std::string& shortName, const std::string& description, bool required, const std::string& argName, bool argOptional):
111
_shortName(shortName),
113
_description(description),
117
_argRequired(argOptional),
127
if (_pValidator) _pValidator->release();
128
if (_pConfig) _pConfig->release();
133
Option& Option::operator = (const Option& option)
144
void Option::swap(Option& option)
146
std::swap(_shortName, option._shortName);
147
std::swap(_fullName, option._fullName);
148
std::swap(_description, option._description);
149
std::swap(_required, option._required);
150
std::swap(_repeatable, option._repeatable);
151
std::swap(_argName, option._argName);
152
std::swap(_argRequired, option._argRequired);
153
std::swap(_group, option._group);
154
std::swap(_binding, option._binding);
155
std::swap(_pValidator, option._pValidator);
156
std::swap(_pCallback, option._pCallback);
157
std::swap(_pConfig, option._pConfig);
161
Option& Option::shortName(const std::string& name)
168
Option& Option::fullName(const std::string& name)
175
Option& Option::description(const std::string& text)
182
Option& Option::required(bool flag)
189
Option& Option::repeatable(bool flag)
196
Option& Option::argument(const std::string& name, bool required)
199
_argRequired = required;
204
Option& Option::noArgument()
207
_argRequired = false;
212
Option& Option::group(const std::string& group)
219
Option& Option::binding(const std::string& propertyName)
221
return binding(propertyName, 0);
225
Option& Option::binding(const std::string& propertyName, AbstractConfiguration* pConfig)
227
_binding = propertyName;
228
if (_pConfig) _pConfig->release();
230
if (_pConfig) _pConfig->duplicate();
235
Option& Option::callback(const AbstractOptionCallback& cb)
237
_pCallback = cb.clone();
242
Option& Option::validator(Validator* pValidator)
244
if (_pValidator) _pValidator->release();
245
_pValidator = pValidator;
246
if (_pValidator) _pValidator->duplicate();
251
bool Option::matchesShort(const std::string& option) const
253
return option.length() > 0
254
&& !_shortName.empty() && option.compare(0, _shortName.length(), _shortName) == 0;
258
bool Option::matchesFull(const std::string& option) const
260
std::string::size_type pos = option.find_first_of(":=");
261
std::string::size_type len = pos == std::string::npos ? option.length() : pos;
262
return len == _fullName.length()
263
&& icompare(option, 0, len, _fullName, 0, len) == 0;
267
bool Option::matchesPartial(const std::string& option) const
269
std::string::size_type pos = option.find_first_of(":=");
270
std::string::size_type len = pos == std::string::npos ? option.length() : pos;
271
return option.length() > 0
272
&& icompare(option, 0, len, _fullName, 0, len) == 0;
276
void Option::process(const std::string& option, std::string& arg) const
278
std::string::size_type pos = option.find_first_of(":=");
279
std::string::size_type len = pos == std::string::npos ? option.length() : pos;
280
if (icompare(option, 0, len, _fullName, 0, len) == 0)
284
if (argumentRequired() && pos == std::string::npos)
285
throw MissingArgumentException(_fullName + " requires " + argumentName());
286
if (pos != std::string::npos)
287
arg.assign(option, pos + 1, option.length() - pos - 1);
291
else if (pos != std::string::npos)
293
throw UnexpectedArgumentException(option);
297
else if (!_shortName.empty() && option.compare(0, _shortName.length(), _shortName) == 0)
301
if (argumentRequired() && option.length() == _shortName.length())
302
throw MissingArgumentException(_shortName + " requires " + argumentName());
303
arg.assign(option, _shortName.length(), option.length() - _shortName.length());
305
else if (option.length() != _shortName.length())
307
throw UnexpectedArgumentException(option);
311
else throw UnknownOptionException(option);
315
} } // namespace Poco::Util