Mercurial > code
changeset 553:2d1a04e8240a
Options: rename to options.hpp, style
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 15 Jun 2016 13:45:44 +0200 |
parents | c8b6ce961af0 |
children | 20fcf96038ae |
files | modules/options/CMakeLists.txt modules/options/options.cpp modules/options/options.h modules/options/options.hpp modules/options/test/main.cpp |
diffstat | 5 files changed, 169 insertions(+), 158 deletions(-) [+] |
line wrap: on
line diff
--- a/modules/options/CMakeLists.txt Wed Jun 15 13:40:03 2016 +0200 +++ b/modules/options/CMakeLists.txt Wed Jun 15 13:45:44 2016 +0200 @@ -22,5 +22,5 @@ NAME options SOURCES ${options_SOURCE_DIR}/options.cpp - ${options_SOURCE_DIR}/options.h + ${options_SOURCE_DIR}/options.hpp )
--- a/modules/options/options.cpp Wed Jun 15 13:40:03 2016 +0200 +++ b/modules/options/options.cpp Wed Jun 15 13:45:44 2016 +0200 @@ -18,7 +18,7 @@ #include <cassert> -#include "options.h" +#include "options.hpp" namespace option { @@ -55,7 +55,7 @@ if (opt == definition.end()) throw InvalidOption{arg}; - /* Need argument? */ + // Need argument? if (opt->second) { if (it == end || isOption(*it)) throw MissingValue{arg}; @@ -85,7 +85,7 @@ if (opt == definition.end()) throw InvalidOption{arg}; - /* Need argument? */ + // Need argument? if (opt->second) { if (it == end || isOption(*it)) throw MissingValue{arg}; @@ -119,7 +119,7 @@ if (opt->second) { if (i == (len - 1)) { - /* End of string, get the next argument (see 2.) */ + // End of string, get the next argument (see 2.). if (++it == end || isOption(*it)) throw MissingValue{arg}; @@ -129,9 +129,8 @@ result.insert(std::make_pair(arg, value.substr(i + 1))); i = len; } - } else { + } else result.insert(std::make_pair(arg, "")); - } } it = args.erase(args.begin(), args.begin() + toremove);
--- a/modules/options/options.h Wed Jun 15 13:40:03 2016 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,141 +0,0 @@ -/* - * options.h -- parse Unix command line options - * - * Copyright (c) 2015 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef _OPTIONS_H_ -#define _OPTIONS_H_ - -#include <exception> -#include <map> -#include <string> -#include <utility> -#include <vector> - -/** - * Namespace for options parsing. - */ -namespace option { - -/** - * @class InvalidOption - * @brief This exception is thrown when an invalid option has been found. - */ -class InvalidOption : public std::exception { -private: - std::string message; - -public: - /** - * The invalid option given. - */ - std::string argument; - - /** - * Construct the exception. - * - * @param arg the argument missing - */ - inline InvalidOption(std::string arg) - : argument{std::move(arg)} - { - message = std::string{"invalid option: "} + argument; - } - - /** - * Get the error message. - * - * @return the error message - */ - const char *what() const noexcept override - { - return message.c_str(); - } -}; - -/** - * @class MissingValue - * @brief This exception is thrown when an option requires a value and no value has been given - */ -class MissingValue : public std::exception { -private: - std::string message; - -public: - /** - * The argument that requires a value. - */ - std::string argument; - - /** - * Construct the exception. - * - * @param arg the argument that requires a value - */ - inline MissingValue(std::string arg) - : argument{std::move(arg)} - { - message = std::string{"missing argument for: "} + argument; - } - - /** - * Get the error message. - * - * @return the error message - */ - const char *what() const noexcept override - { - return message.c_str(); - } -}; - -/** - * Packed multimap of options. - */ -using Result = std::multimap<std::string, std::string>; - -/** - * Define the allowed options. - */ -using Options = std::map<std::string, bool>; - -/** - * Extract the command line options and return a result. - * - * @param args the arguments - * @param definition - * @warning the arguments vector is modified in place to remove parsed options - * @throw MissingValue - * @throw InvalidOption - */ -Result read(std::vector<std::string> &args, const Options &definition); - -/** - * Overloaded function for usage with main() arguments. - * - * @param argc the number of arguments - * @param argv the argument vector - * @param definition - * @note don't forget to remove the first argv[0] argument - * @warning the argc and argv are modified in place to remove parsed options - * @throw MissingValue - * @throw InvalidOption - */ -Result read(int &argc, char **&argv, const Options &definition); - -} // !option - -#endif // !_OPTIONS_H_
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/modules/options/options.hpp Wed Jun 15 13:45:44 2016 +0200 @@ -0,0 +1,150 @@ +/* + * options.h -- parse Unix command line options + * + * Copyright (c) 2015 David Demelier <markand@malikania.fr> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef OPTIONS_HPP +#define OPTIONS_HPP + +/** + * \file options.h + * \brief Basic Unix options parser. + */ + +#include <exception> +#include <map> +#include <string> +#include <utility> +#include <vector> + +/** + * Namespace for options parsing. + */ +namespace option { + +/** + * \brief This exception is thrown when an invalid option has been found. + */ +class InvalidOption : public std::exception { +private: + std::string message; + +public: + /** + * The invalid option given. + */ + std::string argument; + + /** + * Construct the exception. + * + * \param arg the argument missing + */ + inline InvalidOption(std::string arg) + : argument(std::move(arg)) + { + message = std::string("invalid option: ") + argument; + } + + /** + * Get the error message. + * + * \return the error message + */ + const char *what() const noexcept override + { + return message.c_str(); + } +}; + +/** + * \brief This exception is thrown when an option requires a value and no value has been given. + */ +class MissingValue : public std::exception { +private: + std::string m_message; + std::string m_option; + +public: + /** + * Construct the exception. + * + * \param option the option that requires a value + */ + inline MissingValue(std::string option) + : m_option(std::move(option)) + { + m_message = std::string("missing argument for: ") + m_option; + } + + /** + * Get the options that requires a value. + * + * \return the option name + */ + inline const std::string &option() const noexcept + { + return m_option; + } + + /** + * Get the error message. + * + * \return the error message + */ + const char *what() const noexcept override + { + return m_message.c_str(); + } +}; + +/** + * Packed multimap of options. + */ +using Result = std::multimap<std::string, std::string>; + +/** + * Define the allowed options. + */ +using Options = std::map<std::string, bool>; + +/** + * Extract the command line options and return a result. + * + * \param args the arguments + * \param definition + * \warning the arguments vector is modified in place to remove parsed options + * \throw MissingValue + * \throw InvalidOption + */ +Result read(std::vector<std::string> &args, const Options &definition); + +/** + * Overloaded function for usage with main() arguments. + * + * \param argc the number of arguments + * \param argv the argument vector + * \param definition + * \note don't forget to remove the first argv[0] argument + * \warning the argc and argv are modified in place to remove parsed options + * \throw MissingValue + * \throw InvalidOption + */ +Result read(int &argc, char **&argv, const Options &definition); + +} // !option + +#endif // !OPTIONS_HPP
--- a/modules/options/test/main.cpp Wed Jun 15 13:40:03 2016 +0200 +++ b/modules/options/test/main.cpp Wed Jun 15 13:45:44 2016 +0200 @@ -18,11 +18,12 @@ #include <gtest/gtest.h> -#include <options.h> +#include <options.hpp> -/* -------------------------------------------------------- - * Short options - * -------------------------------------------------------- */ +/* + * Short options. + * ------------------------------------------------------------------ + */ TEST(Short, simpleNoArg) { @@ -118,9 +119,10 @@ ASSERT_EQ("foo.conf", pack.find("-c")->second); } -/* -------------------------------------------------------- - * Long options - * -------------------------------------------------------- */ +/* + * Long options. + * ------------------------------------------------------------------ + */ TEST(Long, simple) { @@ -157,9 +159,10 @@ ASSERT_EQ("2", pack.find("--level")->second); } -/* -------------------------------------------------------- - * Errors - * -------------------------------------------------------- */ +/* + * Errors. + * ------------------------------------------------------------------ + */ TEST(Errors, stop) {