?? option.cpp
字號:
//
// Option.cpp
//
// $Id: //poco/Main/Util/src/Option.cpp#1 $
//
// Copyright (c) 2004-2005, Guenter Obiltschnig/Applied Informatics.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Redistributions in any form must be accompanied by information on
// how to obtain complete source code for this software and any
// accompanying software that uses this software. The source code
// must either be included in the distribution or be available for no
// more than the cost of distribution plus a nominal fee, and must be
// freely redistributable under reasonable conditions. For an
// executable file, complete source code means the source code for all
// modules it contains. It does not include source code for modules or
// files that typically accompany the major components of the operating
// system on which the executable file runs.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
#include "Util/Option.h"
#include "Util/OptionException.h"
#include "Foundation/String.h"
using Foundation::icompare;
Util_BEGIN
Option::Option():
_required(false),
_repeatable(false),
_argRequired(false)
{
}
Option::Option(const Option& option):
_shortName(option._shortName),
_fullName(option._fullName),
_description(option._description),
_required(option._required),
_repeatable(option._repeatable),
_argName(option._argName),
_argRequired(option._argRequired),
_group(option._group)
{
}
Option::Option(const std::string& fullName, const std::string& shortName):
_fullName(fullName),
_shortName(shortName),
_required(false),
_repeatable(false),
_argRequired(false)
{
}
Option::Option(const std::string& fullName, const std::string& shortName, const std::string& description, bool required):
_fullName(fullName),
_shortName(shortName),
_description(description),
_required(required),
_repeatable(false),
_argRequired(false)
{
}
Option::Option(const std::string& fullName, const std::string& shortName, const std::string& description, bool required, const std::string& argName, bool argOptional):
_fullName(fullName),
_shortName(shortName),
_description(description),
_required(required),
_repeatable(false),
_argName(argName),
_argRequired(argOptional)
{
}
Option::~Option()
{
}
Option& Option::operator = (const Option& option)
{
if (&option != this)
{
_shortName = option._shortName;
_fullName = option._fullName;
_description = option._description;
_required = option._required;
_repeatable = option._repeatable;
_argName = option._argName;
_argRequired = option._argRequired;
_group = option._group;
}
return *this;
}
Option& Option::shortName(const std::string& name)
{
_shortName = name;
return *this;
}
Option& Option::fullName(const std::string& name)
{
_fullName = name;
return *this;
}
Option& Option::description(const std::string& text)
{
_description = text;
return *this;
}
Option& Option::required(bool flag)
{
_required = flag;
return *this;
}
Option& Option::repeatable(bool flag)
{
_repeatable = flag;
return *this;
}
Option& Option::argument(const std::string& name, bool required)
{
_argName = name;
_argRequired = required;
return *this;
}
Option& Option::noArgument()
{
_argName.clear();
_argRequired = false;
return *this;
}
Option& Option::group(const std::string& group)
{
_group = group;
return *this;
}
bool Option::matchesShort(const std::string& option) const
{
return option.length() > 0
&& !_shortName.empty() && option.compare(0, _shortName.length(), _shortName) == 0;
}
bool Option::matchesFull(const std::string& option) const
{
std::string::size_type pos = option.find_first_of(":=");
std::string::size_type len = pos == std::string::npos ? option.length() : pos;
return option.length() > 0
&& icompare(option, 0, len, _fullName, 0, len) == 0;
}
void Option::process(const std::string& option, std::string& arg) const
{
std::string::size_type pos = option.find_first_of(":=");
std::string::size_type len = pos == std::string::npos ? option.length() : pos;
if (icompare(option, 0, len, _fullName, 0, len) == 0)
{
if (takesArgument())
{
if (argumentRequired() && pos == std::string::npos)
throw MissingArgumentException(_fullName + " requires " + argumentName());
if (pos != std::string::npos)
arg.assign(option, pos + 1, option.length() - pos - 1);
else
arg.clear();
}
else if (pos != std::string::npos)
{
throw UnexpectedArgumentException(option);
}
else arg.clear();
}
else if (!_shortName.empty() && option.compare(0, _shortName.length(), _shortName) == 0)
{
if (takesArgument())
{
if (argumentRequired() && option.length() == _shortName.length())
throw MissingArgumentException(_shortName + " requires " + argumentName());
arg.assign(option, _shortName.length(), option.length() - _shortName.length());
}
else if (option.length() != _shortName.length())
{
throw UnexpectedArgumentException(option);
}
else arg.clear();
}
else throw UnknownOptionException(option);
}
Util_END
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -