#ifndef ZTD_OPTIONS_HPP #define ZTD_OPTIONS_HPP //DUPLICATES NOT HANDLED: takes last one //NO ORDER: no way to know options order //RETURNS TRANSITIONAL STATE IF ERROR #include #include #include #include /*! @file options.hpp * @brief CLI option reading and processing * * Easy managing of POSIX/GNU style options */ namespace ztd { //! @brief Option exception /*! Thrown when errors are encountered during processing of options */ class option_error : public std::exception { public: //! @brief option error types enum error_type { unknown_option, takes_no_arg, missing_arg}; //! @brief Conctructor option_error(error_type type, const std::string& option); //! @brief Type of error inline error_type type() { return errtype; } //! @brief Error message inline const char * what () const throw () {return msg.c_str();} //! @brief Subject of said error inline const char * option () const throw () {return opt.c_str();} private: error_type errtype; std::string opt; std::string msg; }; //! @brief Convert argc/argv into vector std::vector argVector(int argc, char** argv); //! @brief Single option object /*! Definition of a GNU/POSIX style option */ class option { public: /* CTOR/DTOR */ //ctors option(); //! @brief Create a char defined option option(char c, bool arg=false, std::string helptext="", std::string argname="arg"); //! @brief Create a string defined option option(const std::string& str, bool arg=false, std::string helptext="", std::string argname="arg"); //! @brief Create a char and string defined option option(char c, const std::string& str, bool arg=false, std::string helptext="", std::string argname="arg"); //! @brief Create copy option(const option& opt); /* FUNCTIONS */ //! @brief Print option's help /*! @param leftpad Put @a leftpad spaces at start of the line @param rightpad Help text starts at column @a leftpad + @a rightpad */ void print_help(int leftpad, int rightpad) const; /* PROPERTIES */ bool shortDef; // has a char definition char charName; bool longDef; // has a string definition std::string strName; bool takesArgument; // option takes an argument std::string help_text; // text to display in print_help std::string arg_name; // name of the argument to display in print_help /* PROCESSING STATUS */ //! @brief Option was activated bool activated; // option was activated //! @brief Option's argument std::string argument; // argument of the option inline operator bool() const { return activated; } inline operator std::string() const { return argument; } }; //! @brief Set of POSIX/GNU style options /*! Process arguments through it to extract options After processing: - for global option settings use find() or parse option_vec - for sequential option settings parse option_sequence */ class option_set { public: /*CREATION FUNCTIONS*/ //! @brief Add option to the set inline void add(option opt) { option_vec.push_back(opt); } //! @brief Variadic call of add() template void add(Args... args) { std::vector