Path: blob/main/contrib/kyua/utils/cmdline/options.cpp
48178 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.55cmdline::base_option::base_option(const char short_name_,56const char* long_name_,57const char* description_,58const char* arg_name_,59const char* default_value_) :60_short_name(short_name_),61_long_name(long_name_),62_description(description_),63_arg_name(arg_name_ == NULL ? "" : arg_name_),64_has_default_value(default_value_ != NULL),65_default_value(default_value_ == NULL ? "" : default_value_)66{67INV(short_name_ != '\0');68}697071/// Constructs a generic option with a long name only.72///73/// \param long_name_ The long name for the option.74/// \param description_ A user-friendly description for the option.75/// \param arg_name_ If not NULL, specifies that the option must receive an76/// argument and specifies the name of such argument for documentation77/// purposes.78/// \param default_value_ If not NULL, specifies that the option has a default79/// value for the mandatory argument.80cmdline::base_option::base_option(const char* long_name_,81const char* description_,82const char* arg_name_,83const char* default_value_) :84_short_name('\0'),85_long_name(long_name_),86_description(description_),87_arg_name(arg_name_ == NULL ? "" : arg_name_),88_has_default_value(default_value_ != NULL),89_default_value(default_value_ == NULL ? "" : default_value_)90{91}929394/// Destructor for the option.95cmdline::base_option::~base_option(void)96{97}9899100/// Checks whether the option has a short name or not.101///102/// \return True if the option has a short name, false otherwise.103bool104cmdline::base_option::has_short_name(void) const105{106return _short_name != '\0';107}108109110/// Returns the short name of the option.111///112/// \pre has_short_name() must be true.113///114/// \return The short name.115char116cmdline::base_option::short_name(void) const117{118PRE(has_short_name());119return _short_name;120}121122123/// Returns the long name of the option.124///125/// \return The long name.126const std::string&127cmdline::base_option::long_name(void) const128{129return _long_name;130}131132133/// Returns the description of the option.134///135/// \return The description.136const std::string&137cmdline::base_option::description(void) const138{139return _description;140}141142143/// Checks whether the option needs an argument or not.144///145/// \return True if the option needs an argument, false otherwise.146bool147cmdline::base_option::needs_arg(void) const148{149return !_arg_name.empty();150}151152153/// Returns the argument name of the option for documentation purposes.154///155/// \pre needs_arg() must be true.156///157/// \return The argument name.158const std::string&159cmdline::base_option::arg_name(void) const160{161INV(needs_arg());162return _arg_name;163}164165166/// Checks whether the option has a default value for its argument.167///168/// \pre needs_arg() must be true.169///170/// \return True if the option has a default value, false otherwise.171bool172cmdline::base_option::has_default_value(void) const173{174PRE(needs_arg());175return _has_default_value;176}177178179/// Returns the default value for the argument to the option.180///181/// \pre has_default_value() must be true.182///183/// \return The default value.184const std::string&185cmdline::base_option::default_value(void) const186{187INV(has_default_value());188return _default_value;;189}190191192/// Formats the short name of the option for documentation purposes.193///194/// \return A string describing the option's short name.195std::string196cmdline::base_option::format_short_name(void) const197{198PRE(has_short_name());199200if (needs_arg()) {201return F("-%s %s") % short_name() % arg_name();202} else {203return F("-%s") % short_name();204}205}206207208/// Formats the long name of the option for documentation purposes.209///210/// \return A string describing the option's long name.211std::string212cmdline::base_option::format_long_name(void) const213{214if (needs_arg()) {215return F("--%s=%s") % long_name() % arg_name();216} else {217return F("--%s") % long_name();218}219}220221222223/// Ensures that an argument passed to the option is valid.224///225/// This must be reimplemented by subclasses that describe options with226/// arguments.227///228/// \throw cmdline::option_argument_value_error Subclasses must raise this229/// exception to indicate the cases in which str is invalid.230void231cmdline::base_option::validate(const std::string& /* str */) const232{233UNREACHABLE_MSG("Option does not support an argument");234}235236237/// Constructs a boolean option with both a short and a long name.238///239/// \param short_name_ The short name for the option.240/// \param long_name_ The long name for the option.241/// \param description_ A user-friendly description for the option.242cmdline::bool_option::bool_option(const char short_name_,243const char* long_name_,244const char* description_) :245base_option(short_name_, long_name_, description_)246{247}248249250/// Constructs a boolean option with a long name only.251///252/// \param long_name_ The long name for the option.253/// \param description_ A user-friendly description for the option.254cmdline::bool_option::bool_option(const char* long_name_,255const char* description_) :256base_option(long_name_, description_)257{258}259260261/// Constructs an integer option with both a short and a long name.262///263/// \param short_name_ The short name for the option.264/// \param long_name_ The long name for the option.265/// \param description_ A user-friendly description for the option.266/// \param arg_name_ The name of the mandatory argument, for documentation267/// purposes.268/// \param default_value_ If not NULL, the default value for the mandatory269/// argument.270cmdline::int_option::int_option(const char short_name_,271const char* long_name_,272const char* description_,273const char* arg_name_,274const char* default_value_) :275base_option(short_name_, long_name_, description_, arg_name_,276default_value_)277{278}279280281/// Constructs an integer option with a long name only.282///283/// \param long_name_ The long name for the option.284/// \param description_ A user-friendly description for the option.285/// \param arg_name_ The name of the mandatory argument, for documentation286/// purposes.287/// \param default_value_ If not NULL, the default value for the mandatory288/// argument.289cmdline::int_option::int_option(const char* long_name_,290const char* description_,291const char* arg_name_,292const char* default_value_) :293base_option(long_name_, description_, arg_name_, default_value_)294{295}296297298/// Ensures that an integer argument passed to the int_option is valid.299///300/// \param raw_value The argument representing an integer as provided by the301/// user.302///303/// \throw cmdline::option_argument_value_error If the integer provided in304/// raw_value is invalid.305void306cmdline::int_option::validate(const std::string& raw_value) const307{308try {309(void)text::to_type< int >(raw_value);310} catch (const std::runtime_error& e) {311throw cmdline::option_argument_value_error(312F("--%s") % long_name(), raw_value, "Not a valid integer");313}314}315316317/// Converts an integer argument to a native integer.318///319/// \param raw_value The argument representing an integer as provided by the320/// user.321///322/// \return The integer.323///324/// \pre validate(raw_value) must be true.325int326cmdline::int_option::convert(const std::string& raw_value)327{328try {329return text::to_type< int >(raw_value);330} catch (const std::runtime_error& e) {331PRE_MSG(false, F("Raw value '%s' for int option not properly "332"validated: %s") % raw_value % e.what());333}334}335336337/// Constructs a list option with both a short and a long name.338///339/// \param short_name_ The short name for the option.340/// \param long_name_ The long name for the option.341/// \param description_ A user-friendly description for the option.342/// \param arg_name_ The name of the mandatory argument, for documentation343/// purposes.344/// \param default_value_ If not NULL, the default value for the mandatory345/// argument.346cmdline::list_option::list_option(const char short_name_,347const char* long_name_,348const char* description_,349const char* arg_name_,350const char* default_value_) :351base_option(short_name_, long_name_, description_, arg_name_,352default_value_)353{354}355356357/// Constructs a list option with a long name only.358///359/// \param long_name_ The long name for the option.360/// \param description_ A user-friendly description for the option.361/// \param arg_name_ The name of the mandatory argument, for documentation362/// purposes.363/// \param default_value_ If not NULL, the default value for the mandatory364/// argument.365cmdline::list_option::list_option(const char* long_name_,366const char* description_,367const char* arg_name_,368const char* default_value_) :369base_option(long_name_, description_, arg_name_, default_value_)370{371}372373374/// Ensures that a lisstring argument passed to the list_option is valid.375void376cmdline::list_option::validate(377const std::string& /* raw_value */) const378{379// Any list is potentially valid; the caller must check for semantics.380}381382383/// Converts a string argument to a vector.384///385/// \param raw_value The argument representing a list as provided by the user.386///387/// \return The list.388///389/// \pre validate(raw_value) must be true.390cmdline::list_option::option_type391cmdline::list_option::convert(const std::string& raw_value)392{393try {394return text::split(raw_value, ',');395} catch (const std::runtime_error& e) {396PRE_MSG(false, F("Raw value '%s' for list option not properly "397"validated: %s") % raw_value % e.what());398}399}400401402/// Constructs a path option with both a short and a long name.403///404/// \param short_name_ The short name for the option.405/// \param long_name_ The long name for the option.406/// \param description_ A user-friendly description for the option.407/// \param arg_name_ The name of the mandatory argument, for documentation408/// purposes.409/// \param default_value_ If not NULL, the default value for the mandatory410/// argument.411cmdline::path_option::path_option(const char short_name_,412const char* long_name_,413const char* description_,414const char* arg_name_,415const char* default_value_) :416base_option(short_name_, long_name_, description_, arg_name_,417default_value_)418{419}420421422/// Constructs a path option with a long name only.423///424/// \param long_name_ The long name for the option.425/// \param description_ A user-friendly description for the option.426/// \param arg_name_ The name of the mandatory argument, for documentation427/// purposes.428/// \param default_value_ If not NULL, the default value for the mandatory429/// argument.430cmdline::path_option::path_option(const char* long_name_,431const char* description_,432const char* arg_name_,433const char* default_value_) :434base_option(long_name_, description_, arg_name_, default_value_)435{436}437438439/// Ensures that a path argument passed to the path_option is valid.440///441/// \param raw_value The argument representing a path as provided by the user.442///443/// \throw cmdline::option_argument_value_error If the path provided in444/// raw_value is invalid.445void446cmdline::path_option::validate(const std::string& raw_value) const447{448try {449(void)utils::fs::path(raw_value);450} catch (const utils::fs::error& e) {451throw cmdline::option_argument_value_error(F("--%s") % long_name(),452raw_value, e.what());453}454}455456457/// Converts a path argument to a utils::fs::path.458///459/// \param raw_value The argument representing a path as provided by the user.460///461/// \return The path.462///463/// \pre validate(raw_value) must be true.464utils::fs::path465cmdline::path_option::convert(const std::string& raw_value)466{467try {468return utils::fs::path(raw_value);469} catch (const std::runtime_error& e) {470PRE_MSG(false, F("Raw value '%s' for path option not properly "471"validated: %s") % raw_value % e.what());472}473}474475476/// Constructs a property option with both a short and a long name.477///478/// \param short_name_ The short name for the option.479/// \param long_name_ The long name for the option.480/// \param description_ A user-friendly description for the option.481/// \param arg_name_ The name of the mandatory argument, for documentation482/// purposes. Must include the '=' delimiter.483cmdline::property_option::property_option(const char short_name_,484const char* long_name_,485const char* description_,486const char* arg_name_) :487base_option(short_name_, long_name_, description_, arg_name_)488{489PRE(arg_name().find('=') != std::string::npos);490}491492493/// Constructs a property option with a long name only.494///495/// \param long_name_ The long name for the option.496/// \param description_ A user-friendly description for the option.497/// \param arg_name_ The name of the mandatory argument, for documentation498/// purposes. Must include the '=' delimiter.499cmdline::property_option::property_option(const char* long_name_,500const char* description_,501const char* arg_name_) :502base_option(long_name_, description_, arg_name_)503{504PRE(arg_name().find('=') != std::string::npos);505}506507508/// Validates the argument to a property option.509///510/// \param raw_value The argument provided by the user.511void512cmdline::property_option::validate(const std::string& raw_value) const513{514const std::string::size_type pos = raw_value.find('=');515if (pos == std::string::npos)516throw cmdline::option_argument_value_error(517F("--%s") % long_name(), raw_value,518F("Argument does not have the form '%s'") % arg_name());519520const std::string key = raw_value.substr(0, pos);521if (key.empty())522throw cmdline::option_argument_value_error(523F("--%s") % long_name(), raw_value, "Empty property name");524525const std::string value = raw_value.substr(pos + 1);526if (value.empty())527throw cmdline::option_argument_value_error(528F("--%s") % long_name(), raw_value, "Empty value");529}530531532/// Returns the property option in a key/value pair form.533///534/// \param raw_value The argument provided by the user.535///536/// \return raw_value The key/value pair representation of the property.537///538/// \pre validate(raw_value) must be true.539cmdline::property_option::option_type540cmdline::property_option::convert(const std::string& raw_value)541{542const std::string::size_type pos = raw_value.find('=');543return std::make_pair(raw_value.substr(0, pos), raw_value.substr(pos + 1));544}545546547/// Constructs a string option with both a short and a long name.548///549/// \param short_name_ The short name for the option.550/// \param long_name_ The long name for the option.551/// \param description_ A user-friendly description for the option.552/// \param arg_name_ The name of the mandatory argument, for documentation553/// purposes.554/// \param default_value_ If not NULL, the default value for the mandatory555/// argument.556cmdline::string_option::string_option(const char short_name_,557const char* long_name_,558const char* description_,559const char* arg_name_,560const char* default_value_) :561base_option(short_name_, long_name_, description_, arg_name_,562default_value_)563{564}565566567/// Constructs a string option with a long name only.568///569/// \param long_name_ The long name for the option.570/// \param description_ A user-friendly description for the option.571/// \param arg_name_ The name of the mandatory argument, for documentation572/// purposes.573/// \param default_value_ If not NULL, the default value for the mandatory574/// argument.575cmdline::string_option::string_option(const char* long_name_,576const char* description_,577const char* arg_name_,578const char* default_value_) :579base_option(long_name_, description_, arg_name_, default_value_)580{581}582583584/// Does nothing; all string values are valid arguments to a string_option.585void586cmdline::string_option::validate(587const std::string& /* raw_value */) const588{589// Nothing to do.590}591592593/// Returns the string unmodified.594///595/// \param raw_value The argument provided by the user.596///597/// \return raw_value598///599/// \pre validate(raw_value) must be true.600std::string601cmdline::string_option::convert(const std::string& raw_value)602{603return raw_value;604}605606607