Path: blob/main/contrib/kyua/utils/cmdline/options.cpp
103316 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/options.hpp"2930#include <stdexcept>31#include <vector>3233#include "utils/cmdline/exceptions.hpp"34#include "utils/defs.hpp"35#include "utils/format/macros.hpp"36#include "utils/fs/exceptions.hpp"37#include "utils/fs/path.hpp"38#include "utils/sanity.hpp"39#include "utils/text/operations.ipp"4041namespace cmdline = utils::cmdline;42namespace text = utils::text;434445/// Constructs a generic option with both a short and a long name.46///47/// \param short_name_ The short name for the option.48/// \param long_name_ The long name for the option.49/// \param description_ A user-friendly description for the option.50/// \param arg_name_ If not NULL, specifies that the option must receive an51/// argument and specifies the name of such argument for documentation52/// purposes.53/// \param default_value_ If not NULL, specifies that the option has a default54/// value for the mandatory argument.55/// \param arg_is_optional_ Specifies if a value must be provided or not.56cmdline::base_option::base_option(const char short_name_,57const char* long_name_,58const char* description_,59const char* arg_name_,60const char* default_value_,61bool arg_is_optional_) :62_short_name(short_name_),63_long_name(long_name_),64_description(description_),65_arg_name(arg_name_ == NULL ? "" : arg_name_),66_arg_is_optional(arg_is_optional_),67_has_default_value(default_value_ != NULL),68_default_value(default_value_ == NULL ? "" : default_value_)69{70INV(short_name_ != '\0');71}727374/// Constructs a generic option with a long name only.75///76/// \param long_name_ The long name for the option.77/// \param description_ A user-friendly description for the option.78/// \param arg_name_ If not NULL, specifies that the option must receive an79/// argument and specifies the name of such argument for documentation80/// purposes.81/// \param default_value_ If not NULL, specifies that the option has a default82/// value for the mandatory argument.83cmdline::base_option::base_option(const char* long_name_,84const char* description_,85const char* arg_name_,86const char* default_value_) :87_short_name('\0'),88_long_name(long_name_),89_description(description_),90_arg_name(arg_name_ == NULL ? "" : arg_name_),91_has_default_value(default_value_ != NULL),92_default_value(default_value_ == NULL ? "" : default_value_)93{94}959697/// Destructor for the option.98cmdline::base_option::~base_option(void)99{100}101102103/// Checks whether the option has a short name or not.104///105/// \return True if the option has a short name, false otherwise.106bool107cmdline::base_option::has_short_name(void) const108{109return _short_name != '\0';110}111112113/// Returns the short name of the option.114///115/// \pre has_short_name() must be true.116///117/// \return The short name.118char119cmdline::base_option::short_name(void) const120{121PRE(has_short_name());122return _short_name;123}124125126/// Returns the long name of the option.127///128/// \return The long name.129const std::string&130cmdline::base_option::long_name(void) const131{132return _long_name;133}134135136/// Returns the description of the option.137///138/// \return The description.139const std::string&140cmdline::base_option::description(void) const141{142return _description;143}144145146/// Checks whether the option needs an argument or not.147///148/// \return True if the option needs an argument, false otherwise.149bool150cmdline::base_option::needs_arg(void) const151{152return !_arg_name.empty();153}154155156/// Returns the argument name of the option for documentation purposes.157///158/// \pre needs_arg() must be true.159///160/// \return The argument name.161const std::string&162cmdline::base_option::arg_name(void) const163{164INV(needs_arg());165return _arg_name;166}167168169/// Returns optionality of the argument.170///171/// \return The optionality.172bool173cmdline::base_option::arg_is_optional(void) const174{175return _arg_is_optional;176}177178179/// Checks whether the option has a default value for its argument.180///181/// \pre needs_arg() must be true.182///183/// \return True if the option has a default value, false otherwise.184bool185cmdline::base_option::has_default_value(void) const186{187PRE(needs_arg());188return _has_default_value;189}190191192/// Returns the default value for the argument to the option.193///194/// \pre has_default_value() must be true.195///196/// \return The default value.197const std::string&198cmdline::base_option::default_value(void) const199{200INV(has_default_value());201return _default_value;;202}203204205/// Formats the short name of the option for documentation purposes.206///207/// \return A string describing the option's short name.208std::string209cmdline::base_option::format_short_name(void) const210{211PRE(has_short_name());212213if (needs_arg()) {214return F("-%s %s") % short_name() % arg_name();215} else {216return F("-%s") % short_name();217}218}219220221/// Formats the long name of the option for documentation purposes.222///223/// \return A string describing the option's long name.224std::string225cmdline::base_option::format_long_name(void) const226{227if (needs_arg()) {228return F("--%s=%s") % long_name() % arg_name();229} else {230return F("--%s") % long_name();231}232}233234235236/// Ensures that an argument passed to the option is valid.237///238/// This must be reimplemented by subclasses that describe options with239/// arguments.240///241/// \throw cmdline::option_argument_value_error Subclasses must raise this242/// exception to indicate the cases in which str is invalid.243void244cmdline::base_option::validate(const std::string& /* str */) const245{246UNREACHABLE_MSG("Option does not support an argument");247}248249250/// Constructs a boolean option with both a short and a long name.251///252/// \param short_name_ The short name for the option.253/// \param long_name_ The long name for the option.254/// \param description_ A user-friendly description for the option.255cmdline::bool_option::bool_option(const char short_name_,256const char* long_name_,257const char* description_) :258base_option(short_name_, long_name_, description_)259{260}261262263/// Constructs a boolean option with a long name only.264///265/// \param long_name_ The long name for the option.266/// \param description_ A user-friendly description for the option.267cmdline::bool_option::bool_option(const char* long_name_,268const char* description_) :269base_option(long_name_, description_)270{271}272273274/// Constructs an integer option with both a short and a long name.275///276/// \param short_name_ The short name for the option.277/// \param long_name_ The long name for the option.278/// \param description_ A user-friendly description for the option.279/// \param arg_name_ The name of the mandatory argument, for documentation280/// purposes.281/// \param default_value_ If not NULL, the default value for the mandatory282/// argument.283cmdline::int_option::int_option(const char short_name_,284const char* long_name_,285const char* description_,286const char* arg_name_,287const char* default_value_) :288base_option(short_name_, long_name_, description_, arg_name_,289default_value_)290{291}292293294/// Constructs an integer option with a long name only.295///296/// \param long_name_ The long name for the option.297/// \param description_ A user-friendly description for the option.298/// \param arg_name_ The name of the mandatory argument, for documentation299/// purposes.300/// \param default_value_ If not NULL, the default value for the mandatory301/// argument.302cmdline::int_option::int_option(const char* long_name_,303const char* description_,304const char* arg_name_,305const char* default_value_) :306base_option(long_name_, description_, arg_name_, default_value_)307{308}309310311/// Ensures that an integer argument passed to the int_option is valid.312///313/// \param raw_value The argument representing an integer as provided by the314/// user.315///316/// \throw cmdline::option_argument_value_error If the integer provided in317/// raw_value is invalid.318void319cmdline::int_option::validate(const std::string& raw_value) const320{321try {322(void)text::to_type< int >(raw_value);323} catch (const std::runtime_error& e) {324throw cmdline::option_argument_value_error(325F("--%s") % long_name(), raw_value, "Not a valid integer");326}327}328329330/// Converts an integer argument to a native integer.331///332/// \param raw_value The argument representing an integer as provided by the333/// user.334///335/// \return The integer.336///337/// \pre validate(raw_value) must be true.338int339cmdline::int_option::convert(const std::string& raw_value)340{341try {342return text::to_type< int >(raw_value);343} catch (const std::runtime_error& e) {344PRE_MSG(false, F("Raw value '%s' for int option not properly "345"validated: %s") % raw_value % e.what());346}347}348349350/// Constructs a list option with both a short and a long name.351///352/// \param short_name_ The short name for the option.353/// \param long_name_ The long name for the option.354/// \param description_ A user-friendly description for the option.355/// \param arg_name_ The name of the mandatory argument, for documentation356/// purposes.357/// \param default_value_ If not NULL, the default value for the mandatory358/// argument.359cmdline::list_option::list_option(const char short_name_,360const char* long_name_,361const char* description_,362const char* arg_name_,363const char* default_value_) :364base_option(short_name_, long_name_, description_, arg_name_,365default_value_)366{367}368369370/// Constructs a list option with a long name only.371///372/// \param long_name_ The long name for the option.373/// \param description_ A user-friendly description for the option.374/// \param arg_name_ The name of the mandatory argument, for documentation375/// purposes.376/// \param default_value_ If not NULL, the default value for the mandatory377/// argument.378cmdline::list_option::list_option(const char* long_name_,379const char* description_,380const char* arg_name_,381const char* default_value_) :382base_option(long_name_, description_, arg_name_, default_value_)383{384}385386387/// Ensures that a lisstring argument passed to the list_option is valid.388void389cmdline::list_option::validate(390const std::string& /* raw_value */) const391{392// Any list is potentially valid; the caller must check for semantics.393}394395396/// Converts a string argument to a vector.397///398/// \param raw_value The argument representing a list as provided by the user.399///400/// \return The list.401///402/// \pre validate(raw_value) must be true.403cmdline::list_option::option_type404cmdline::list_option::convert(const std::string& raw_value)405{406try {407return text::split(raw_value, ',');408} catch (const std::runtime_error& e) {409PRE_MSG(false, F("Raw value '%s' for list option not properly "410"validated: %s") % raw_value % e.what());411}412}413414415/// Constructs a path option with both a short and a long name.416///417/// \param short_name_ The short name for the option.418/// \param long_name_ The long name for the option.419/// \param description_ A user-friendly description for the option.420/// \param arg_name_ The name of the mandatory argument, for documentation421/// purposes.422/// \param default_value_ If not NULL, the default value for the mandatory423/// argument.424cmdline::path_option::path_option(const char short_name_,425const char* long_name_,426const char* description_,427const char* arg_name_,428const char* default_value_) :429base_option(short_name_, long_name_, description_, arg_name_,430default_value_)431{432}433434435/// Constructs a path option with a long name only.436///437/// \param long_name_ The long name for the option.438/// \param description_ A user-friendly description for the option.439/// \param arg_name_ The name of the mandatory argument, for documentation440/// purposes.441/// \param default_value_ If not NULL, the default value for the mandatory442/// argument.443cmdline::path_option::path_option(const char* long_name_,444const char* description_,445const char* arg_name_,446const char* default_value_) :447base_option(long_name_, description_, arg_name_, default_value_)448{449}450451452/// Ensures that a path argument passed to the path_option is valid.453///454/// \param raw_value The argument representing a path as provided by the user.455///456/// \throw cmdline::option_argument_value_error If the path provided in457/// raw_value is invalid.458void459cmdline::path_option::validate(const std::string& raw_value) const460{461try {462(void)utils::fs::path(raw_value);463} catch (const utils::fs::error& e) {464throw cmdline::option_argument_value_error(F("--%s") % long_name(),465raw_value, e.what());466}467}468469470/// Converts a path argument to a utils::fs::path.471///472/// \param raw_value The argument representing a path as provided by the user.473///474/// \return The path.475///476/// \pre validate(raw_value) must be true.477utils::fs::path478cmdline::path_option::convert(const std::string& raw_value)479{480try {481return utils::fs::path(raw_value);482} catch (const std::runtime_error& e) {483PRE_MSG(false, F("Raw value '%s' for path option not properly "484"validated: %s") % raw_value % e.what());485}486}487488489/// Constructs a property option with both a short and a long name.490///491/// \param short_name_ The short name for the option.492/// \param long_name_ The long name for the option.493/// \param description_ A user-friendly description for the option.494/// \param arg_name_ The name of the mandatory argument, for documentation495/// purposes. Must include the '=' delimiter.496cmdline::property_option::property_option(const char short_name_,497const char* long_name_,498const char* description_,499const char* arg_name_) :500base_option(short_name_, long_name_, description_, arg_name_)501{502PRE(arg_name().find('=') != std::string::npos);503}504505506/// Constructs a property option with a long name only.507///508/// \param long_name_ The long name for the option.509/// \param description_ A user-friendly description for the option.510/// \param arg_name_ The name of the mandatory argument, for documentation511/// purposes. Must include the '=' delimiter.512cmdline::property_option::property_option(const char* long_name_,513const char* description_,514const char* arg_name_) :515base_option(long_name_, description_, arg_name_)516{517PRE(arg_name().find('=') != std::string::npos);518}519520521/// Validates the argument to a property option.522///523/// \param raw_value The argument provided by the user.524void525cmdline::property_option::validate(const std::string& raw_value) const526{527const std::string::size_type pos = raw_value.find('=');528if (pos == std::string::npos)529throw cmdline::option_argument_value_error(530F("--%s") % long_name(), raw_value,531F("Argument does not have the form '%s'") % arg_name());532533const std::string key = raw_value.substr(0, pos);534if (key.empty())535throw cmdline::option_argument_value_error(536F("--%s") % long_name(), raw_value, "Empty property name");537538const std::string value = raw_value.substr(pos + 1);539if (value.empty())540throw cmdline::option_argument_value_error(541F("--%s") % long_name(), raw_value, "Empty value");542}543544545/// Returns the property option in a key/value pair form.546///547/// \param raw_value The argument provided by the user.548///549/// \return raw_value The key/value pair representation of the property.550///551/// \pre validate(raw_value) must be true.552cmdline::property_option::option_type553cmdline::property_option::convert(const std::string& raw_value)554{555const std::string::size_type pos = raw_value.find('=');556return std::make_pair(raw_value.substr(0, pos), raw_value.substr(pos + 1));557}558559560/// Constructs a string option with both a short and a long name.561///562/// \param short_name_ The short name for the option.563/// \param long_name_ The long name for the option.564/// \param description_ A user-friendly description for the option.565/// \param arg_name_ The name of the mandatory argument, for documentation566/// purposes.567/// \param default_value_ If not NULL, the default value for the mandatory568/// argument.569cmdline::string_option::string_option(const char short_name_,570const char* long_name_,571const char* description_,572const char* arg_name_,573const char* default_value_,574bool arg_is_optional_) :575base_option(short_name_, long_name_, description_, arg_name_,576default_value_, arg_is_optional_)577{578}579580581/// Constructs a string option with a long name only.582///583/// \param long_name_ The long name for the option.584/// \param description_ A user-friendly description for the option.585/// \param arg_name_ The name of the mandatory argument, for documentation586/// purposes.587/// \param default_value_ If not NULL, the default value for the mandatory588/// argument.589cmdline::string_option::string_option(const char* long_name_,590const char* description_,591const char* arg_name_,592const char* default_value_) :593base_option(long_name_, description_, arg_name_, default_value_)594{595}596597598/// Does nothing; all string values are valid arguments to a string_option.599void600cmdline::string_option::validate(601const std::string& /* raw_value */) const602{603// Nothing to do.604}605606607/// Returns the string unmodified.608///609/// \param raw_value The argument provided by the user.610///611/// \return raw_value612///613/// \pre validate(raw_value) must be true.614std::string615cmdline::string_option::convert(const std::string& raw_value)616{617return raw_value;618}619620621