/** * @file include/ActivMiddleware/ActivBase/Detail/CommandLineParserDetail.h * * @brief Implementation details for CommandLineParser. */ #if (!defined ACTIV_BASE_DETAIL_COMMAND_LINE_PARSER_DETAIL_H) #define ACTIV_BASE_DETAIL_COMMAND_LINE_PARSER_DETAIL_H #include "ActivMiddleware/ActivBase/External.h" #include "ActivMiddleware/ActivBase/StatusCodeException.h" #include "ActivMiddleware/CxxYZ/functional.h" #include ACTIV_BOOST_INCLUDE(any.hpp) #include ACTIV_BOOST_INCLUDE(optional/optional.hpp) #include ACTIV_BOOST_INCLUDE(optional/optional_io.hpp) #include #include ACTIV_BOOST_INCLUDE(program_options.hpp) //! Activ namespace. namespace Activ { class CommandLineParser; namespace CommandLineParserDetail { /** * @brief Default action to take for a command line value. */ template class ValueStore { public: /** * @brief Constructor. * * @param value where to store the value. */ ValueStore(T &value) : m_value(value) { } /** * @brief Function operator. * * @param value value to store in the location provided to the constructor. */ void operator()(const T &value) { m_value = value; } private: T &m_value; ///< location to store value from command line }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Description of a command line parameter/switch. */ template class Description { public: /** * @brief Constructor. * * @param name name of the parameter/switch. * @param description descriptive text of the parameter/switch. * @param notifier action to take when parameter/switch is seen on the command line. */ Description(const String &name, const String &description, const CxxYZ::function ¬ifier) : m_name(name), m_description(description), m_notifier(notifier) { } const String m_name; ///< name of the parameter/switch const String m_description; ///< descriptive text of the parameter/switch const CxxYZ::function m_notifier; ///< action to take when parameter/switch is seen on the command line }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Description of a command line parameter/switch with default value. */ template class DefaultDescription : public Description { public: /** * @brief Constructor. * * @param name name of the parameter/switch. * @param description descriptive text of the parameter/switch. * @param defaultValue the default value to use if not provided on the command line. * @param notifier action to take when parameter/switch is seen on the command line. */ DefaultDescription(const String &name, const String &description, const T &defaultValue, const CxxYZ::function ¬ifier) : Description(name, description, notifier), m_defaultValue(defaultValue) { } const T m_defaultValue; ///< the default value to use if not provided on the command line }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Description of a mandatory command line parameter. */ template class MandatoryParameterDescription : public Description { public: /** * @brief Constructor. * * @param name name of the parameter. * @param description descriptive text of the parameter. * @param notifier action to take when parameter is seen on the command line. */ MandatoryParameterDescription(const String &name, const String &description, const CxxYZ::function ¬ifier) : Description(name, description, notifier) { } }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Description of an optional command line parameter. */ template class OptionalParameterDescription : public DefaultDescription { public: /** * @brief Constructor. * * @param name name of the parameter. * @param description descriptive text of the parameter. * @param defaultValue the default value to use if not provided on the command line. * @param notifier action to take when parameter is seen on the command line. */ OptionalParameterDescription(const String &name, const String &description, const T &defaultValue, const CxxYZ::function ¬ifier) : DefaultDescription(name, description, defaultValue, notifier) { } }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Description of a mandatory command line switch. */ template class MandatorySwitchDescription : public Description { public: /** * @brief Constructor. * * @param name name of the switch. * @param description descriptive text of the switch. * @param notifier action to take when switch is seen on the command line. */ MandatorySwitchDescription(const String &name, const String &description, const CxxYZ::function ¬ifier) : Description(name, description, notifier) { } }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Description of an optional command line switch. */ template class OptionalSwitchDescription : public DefaultDescription { public: /** * @brief Constructor. * * @param name name of the switch. * @param description descriptive text of the switch. * @param defaultValue the default value to use if not provided on the command line. * @param notifier action to take when switch is seen on the command line. */ OptionalSwitchDescription(const String &name, const String &description, const T &defaultValue, const CxxYZ::function ¬ifier) : DefaultDescription(name, description, defaultValue, notifier) { } }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Description of a boolean command line switch. */ class BooleanSwitchDescription : public Description { public: /** * @brief Constructor. * * @param name name of the switch. * @param description descriptive text of the switch. * @param notifier action to take when switch is seen on the command line. */ BooleanSwitchDescription(const String &name, const String &description, const CxxYZ::function ¬ifier) : Description(name, description, notifier) { } }; // --------------------------------------------------------------------------------------------------------------------------------- class DescriptionAppender; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Implementation details of CommandLineParser. */ class CommandLineParserImpl : private Boost::noncopyable { public: /** * @brief Constructor. * * @param commandLineParser */ CommandLineParserImpl(CommandLineParser &commandLineParser); /** * @brief Process a command line. * * @param argc as passed to main(), * @param argv as passed to main(). * @param outStream iostream to print usage or version to. * * @retval true command line parsed. * @retval false help or version switch was passed. PrintUsage() or PrintVersion() has been called. * * @throw Exception. * @throw Boost::program_options::error. */ bool Process(const int argc, char *argv[], std::ostream &outStream); /** * @brief Print usage display. * * @param argv0 argv[0] from main(). * @param outStream iostream to print usage to. */ void PrintUsage(const String &argv0, std::ostream &outStream) const; /** * @brief Print version. * * @param outStream iostream to print version to. */ void PrintVersion(std::ostream &outStream) const; /** * @brief Determines if the element has been set to a default value or from the command line. * * @param name is the string to check if it is defaulted or not. This string must match the long name of the * Parameter, Switch etc... that you wish to check. * * @return true if it is defaulted. */ bool IsDefaultValue(const String &name) const; private: /** * @brief Get the program name. * * @brief argv0 argv[0] as provided to main(). * * @return The program name. */ static String GetProgramName(const String &argv0); /** * @brief Get the display width. * * @return The display width. */ static uint32_t GetLineLength(); CommandLineParser & m_commandLineParser; const uint32_t m_lineLength; Boost::program_options::options_description m_mandatoryParameterDescriptions; Boost::program_options::options_description m_optionalParameterDescriptions; Boost::program_options::positional_options_description m_positionalDescriptions; Boost::program_options::options_description m_mandatorySwitchDescriptions; Boost::program_options::options_description m_optionalSwitchDescriptions; Boost::program_options::variables_map m_variablesMap; friend class DescriptionAppender; }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Helper class to build description. */ class DescriptionAppender { public: /** * @brief Constructor. * * @param commandLineParserImpl CommandLineParserImpl to append descriptions to. */ DescriptionAppender(CommandLineParserImpl &commandLineParserImpl); /** * @brief Append a mandatory parameter description. * * @param parameterDescription ParameterDescription to append. * * @return *this. */ template DescriptionAppender &operator()(const MandatoryParameterDescription ¶meterDescription) { if (parameterDescription.m_name.empty()) ACTIV_THROW(StatusCodeException, STATUS_CODE_INVALID_FORMAT); m_commandLineParserImpl.m_mandatoryParameterDescriptions.add_options() ( parameterDescription.m_name.c_str(), Boost::program_options::value()->notifier(parameterDescription.m_notifier), parameterDescription.m_description.c_str() ); m_commandLineParserImpl.m_positionalDescriptions.add(parameterDescription.m_name.c_str(), 1); return *this; } /** * @brief Append a mandatory parameter list description. * * @param parameterDescription ParameterDescription to append. * * @return *this. */ template DescriptionAppender &operator()(const MandatoryParameterDescription > ¶meterDescription) { if (parameterDescription.m_name.empty()) ACTIV_THROW(StatusCodeException, STATUS_CODE_INVALID_FORMAT); m_commandLineParserImpl.m_mandatoryParameterDescriptions.add_options() ( parameterDescription.m_name.c_str(), Boost::program_options::value >()->composing()->notifier(parameterDescription.m_notifier), parameterDescription.m_description.c_str() ); m_commandLineParserImpl.m_positionalDescriptions.add(parameterDescription.m_name.c_str(), -1); return *this; } /** * @brief Append an optional parameter description. * * @param parameterDescription OptionalParameterDescription to append. * * @return *this. */ template DescriptionAppender &operator()(const OptionalParameterDescription ¶meterDescription) { if (parameterDescription.m_name.empty()) ACTIV_THROW(StatusCodeException, STATUS_CODE_INVALID_FORMAT); // disallow short options if (String::npos != parameterDescription.m_name.find_first_of(',')) ACTIV_THROW(StatusCodeException, STATUS_CODE_INVALID_FORMAT); m_commandLineParserImpl.m_optionalParameterDescriptions.add_options() ( parameterDescription.m_name.c_str(), Boost::program_options::value()->default_value(parameterDescription.m_defaultValue)->notifier( parameterDescription.m_notifier), parameterDescription.m_description.c_str() ); m_commandLineParserImpl.m_positionalDescriptions.add(parameterDescription.m_name.c_str(), 1); return *this; } /** * @brief Append an optional parameter list description. * * @param parameterDescription OptionalParameterDescription to append. * * @return *this. */ template DescriptionAppender &operator()(const OptionalParameterDescription > ¶meterDescription) { if (parameterDescription.m_name.empty()) ACTIV_THROW(StatusCodeException, STATUS_CODE_INVALID_FORMAT); // disallow short options if (String::npos != parameterDescription.m_name.find_first_of(',')) ACTIV_THROW(StatusCodeException, STATUS_CODE_INVALID_FORMAT); m_commandLineParserImpl.m_optionalParameterDescriptions.add_options() ( parameterDescription.m_name.c_str(), Boost::program_options::value >()->default_value(parameterDescription.m_defaultValue, "")-> composing()->notifier(parameterDescription.m_notifier), parameterDescription.m_description.c_str() ); m_commandLineParserImpl.m_positionalDescriptions.add(parameterDescription.m_name.c_str(), -1); return *this; } /** * @brief Append a mandatory switch description. * * @param switchDescription MandatorySwitchDescription to append. * * @return *this. */ template DescriptionAppender &operator()(const MandatorySwitchDescription &switchDescription) { m_commandLineParserImpl.m_mandatorySwitchDescriptions.add_options() ( switchDescription.m_name.c_str(), Boost::program_options::value()->notifier(switchDescription.m_notifier), switchDescription.m_description.c_str() ); return *this; } /** * @brief Append a mandatory switch list description. * * @param switchDescription MandatorySwitchDescription to append. * * @return *this. */ template DescriptionAppender &operator()(const MandatorySwitchDescription > &switchDescription) { m_commandLineParserImpl.m_mandatorySwitchDescriptions.add_options() ( switchDescription.m_name.c_str(), Boost::program_options::value >()->composing()->notifier(switchDescription.m_notifier), switchDescription.m_description.c_str() ); return *this; } /** * @brief Append an optional switch description with default value. * * @param switchDescription OptionalSwitchDescription to append. * * @return *this. */ template DescriptionAppender &operator()(const OptionalSwitchDescription &switchDescription) { m_commandLineParserImpl.m_optionalSwitchDescriptions.add_options() ( switchDescription.m_name.c_str(), Boost::program_options::value()->default_value(switchDescription.m_defaultValue)-> notifier(switchDescription.m_notifier), switchDescription.m_description.c_str() ); return *this; } /** * @brief Append an optional switch list description with default value. * * @param switchDescription OptionalSwitchDescription to append. * * @return *this. */ template DescriptionAppender &operator()(const OptionalSwitchDescription > &switchDescription) { m_commandLineParserImpl.m_optionalSwitchDescriptions.add_options() ( switchDescription.m_name.c_str(), Boost::program_options::value >()->default_value(switchDescription.m_defaultValue, "")->composing()-> notifier(switchDescription.m_notifier), switchDescription.m_description.c_str() ); return *this; } /** * @brief Append a boolean switch description. * * @param booleanSwitchDescription BooleanSwitchDescription to append. * * @return *this. */ DescriptionAppender &operator()(const BooleanSwitchDescription &booleanSwitchDescription) { m_commandLineParserImpl.m_optionalSwitchDescriptions.add_options() ( booleanSwitchDescription.m_name.c_str(), Boost::program_options::bool_switch()->notifier(booleanSwitchDescription.m_notifier), booleanSwitchDescription.m_description.c_str() ); return *this; } private: CommandLineParserImpl & m_commandLineParserImpl; }; } // namespace CommandLineParserDetail } // namespace Activ #endif // (!defined ACTIV_BASE_DETAIL_COMMAND_LINE_PARSER_DETAIL_H)