Path: blob/main/contrib/kyua/utils/cmdline/exceptions.cpp
48199 views
// Copyright 2010 The Kyua Authors.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728#include "utils/cmdline/exceptions.hpp"2930#include "utils/format/macros.hpp"31#include "utils/sanity.hpp"3233namespace cmdline = utils::cmdline;343536#define VALIDATE_OPTION_NAME(option) PRE_MSG( \37(option.length() == 2 && (option[0] == '-' && option[1] != '-')) || \38(option.length() > 2 && (option[0] == '-' && option[1] == '-')), \39F("The option name %s must be fully specified") % option);404142/// Constructs a new error with a plain-text message.43///44/// \param message The plain-text error message.45cmdline::error::error(const std::string& message) :46std::runtime_error(message)47{48}495051/// Destructor for the error.52cmdline::error::~error(void) throw()53{54}555657/// Constructs a new usage_error.58///59/// \param message The reason behind the usage error.60cmdline::usage_error::usage_error(const std::string& message) :61error(message)62{63}646566/// Destructor for the error.67cmdline::usage_error::~usage_error(void) throw()68{69}707172/// Constructs a new missing_option_argument_error.73///74/// \param option_ The option for which no argument was provided. The option75/// name must be fully specified (with - or -- in front).76cmdline::missing_option_argument_error::missing_option_argument_error(77const std::string& option_) :78usage_error(F("Missing required argument for option %s") % option_),79_option(option_)80{81VALIDATE_OPTION_NAME(option_);82}838485/// Destructor for the error.86cmdline::missing_option_argument_error::~missing_option_argument_error(void)87throw()88{89}909192/// Returns the option name for which no argument was provided.93///94/// \return The option name.95const std::string&96cmdline::missing_option_argument_error::option(void) const97{98return _option;99}100101102/// Constructs a new option_argument_value_error.103///104/// \param option_ The option to which an invalid argument was passed. The105/// option name must be fully specified (with - or -- in front).106/// \param argument_ The invalid argument.107/// \param reason_ The reason describing why the argument is invalid.108cmdline::option_argument_value_error::option_argument_value_error(109const std::string& option_, const std::string& argument_,110const std::string& reason_) :111usage_error(F("Invalid argument '%s' for option %s: %s") % argument_ %112option_ % reason_),113_option(option_),114_argument(argument_),115_reason(reason_)116{117VALIDATE_OPTION_NAME(option_);118}119120121/// Destructor for the error.122cmdline::option_argument_value_error::~option_argument_value_error(void)123throw()124{125}126127128/// Returns the option to which the invalid argument was passed.129///130/// \return The option name.131const std::string&132cmdline::option_argument_value_error::option(void) const133{134return _option;135}136137138/// Returns the invalid argument value.139///140/// \return The invalid argument.141const std::string&142cmdline::option_argument_value_error::argument(void) const143{144return _argument;145}146147148/// Constructs a new unknown_option_error.149///150/// \param option_ The unknown option. The option name must be fully specified151/// (with - or -- in front).152cmdline::unknown_option_error::unknown_option_error(153const std::string& option_) :154usage_error(F("Unknown option %s") % option_),155_option(option_)156{157VALIDATE_OPTION_NAME(option_);158}159160161/// Destructor for the error.162cmdline::unknown_option_error::~unknown_option_error(void) throw()163{164}165166167/// Returns the unknown option name.168///169/// \return The unknown option.170const std::string&171cmdline::unknown_option_error::option(void) const172{173return _option;174}175176177