Files

235 lines
8.2 KiB
C++

/**
* @file include/ActivMiddleware/ActivBase/CommandLineParser.h
*
* @brief Header file for a command line parser class.
*/
#if (!defined ACTIV_BASE_COMMAND_LINE_PARSER_H)
#define ACTIV_BASE_COMMAND_LINE_PARSER_H
#include "ActivMiddleware/ActivBase/Detail/CommandLineParserDetail.h"
//! Activ namespace.
namespace Activ
{
/**
* @brief Command line parser class.
*
* This is a wrapper over the more complete Boost::program_options library.
*
* "help" (short form '?') and "version" switches are implictly added.
*/
class CommandLineParser : private Boost::noncopyable
{
public:
/**
* @brief Default constructor.
*/
ACTIV_BASE_API CommandLineParser();
/**
* @brief Destructor.
*/
ACTIV_BASE_API virtual ~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, default std::cout.
*
* @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.
*/
ACTIV_BASE_API bool Process(const int argc, char *argv[], std::ostream &outStream = std::cout);
/**
* @brief Print usage display.
*
* @param argv0 argv[0] from main().
* @param outStream iostream to print usage to.
*/
ACTIV_BASE_API virtual void PrintUsage(const String &argv0, std::ostream &outStream) const;
/**
* @brief Print version.
*
* @param outStream iostream to print version to.
*/
ACTIV_BASE_API virtual 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.
*/
ACTIV_BASE_API bool IsDefaultValue(const String &name) const;
/**
* @brief Get object to help with appending descriptions.
*
* @return DescriptionAppender helper object reference.
*/
ACTIV_BASE_API CommandLineParserDetail::DescriptionAppender &AddDescriptions();
/**
* @brief Helper function to construct a mandatory parameter description with default store action.
*
* @param name name of the parameter.
* @param description descriptive text of the parameter.
* @param value location to store the parameter's value.
*/
template<typename T>
static CommandLineParserDetail::MandatoryParameterDescription<T> Parameter(const String &name,
const String &description, T &value)
{
return CommandLineParserDetail::MandatoryParameterDescription<T>(name, description,
CommandLineParserDetail::ValueStore<T>(value));
}
/**
* @brief Helper function to construct a mandatory parameter description with arbitrary action.
*
* @param name name of the parameter.
* @param description descriptive text of the parameter.
* @param notifier function to invoke with the parameter's value.
*/
template<typename T, typename F>
static CommandLineParserDetail::MandatoryParameterDescription<T> Parameter(const String &name,
const String &description, const F &notifier)
{
return CommandLineParserDetail::MandatoryParameterDescription<T>(name, description, notifier);
}
/**
* @brief Helper function to construct an optional parameter description with default value and store action.
*
* @param name name of the parameter.
* @param description descriptive text of the parameter.
* @param defaultValue the default value to assign for this parameter if none is provided.
* @param value location to store the parameter's value.
*/
template<typename T>
static CommandLineParserDetail::OptionalParameterDescription<T> Parameter(const String &name,
const String &description, const T &defaultValue, T &value)
{
return CommandLineParserDetail::OptionalParameterDescription<T>(name, description, defaultValue,
CommandLineParserDetail::ValueStore<T>(value));
}
/**
* @brief Helper function to construct an optional parameter description with a default value and arbitrary action.
*
* @param name name of the parameter.
* @param description descriptive text of the parameter.
* @param defaultValue the default value to assign for this parameter if none is provided.
* @param notifier function to invoke with the parameter's value.
*/
template<typename T, typename F>
static CommandLineParserDetail::OptionalParameterDescription<T> Parameter(const String &name,
const String &description, const T &defaultValue, const F &notifier)
{
return CommandLineParserDetail::OptionalParameterDescription<T>(name, description, defaultValue, notifier);
}
/**
* @brief Helper function to construct a mandatory switch description with default store action.
*
* @param name name of the switch.
* @param description descriptive text of the switch.
* @param value location to store the switch's value.
*/
template<typename T>
static CommandLineParserDetail::MandatorySwitchDescription<T> Switch(const String &name,
const String &description, T &value)
{
return CommandLineParserDetail::MandatorySwitchDescription<T>(name, description,
CommandLineParserDetail::ValueStore<T>(value));
}
/**
* @brief Helper function to construct a mandatory switch description with arbitrary action.
*
* @param name name of the switch.
* @param description descriptive text of the switch.
* @param notifier function to invoke with the switch's value.
*/
template<typename T, typename F>
static CommandLineParserDetail::MandatorySwitchDescription<T> Switch(const String &name, const String &description,
const F &notifier)
{
return CommandLineParserDetail::MandatorySwitchDescription<T>(name, description, notifier);
}
/**
* @brief Helper function to construct an optional switch description with default value and default store action.
*
* @param name name of the switch.
* @param description descriptive text of the switch.
* @param defaultValue the default value to assign for this switch if none is provided.
* @param value location to store the switch's value.
*/
template<typename T>
static CommandLineParserDetail::OptionalSwitchDescription<T> Switch(const String &name,
const String &description, const T &defaultValue, T &value)
{
return CommandLineParserDetail::OptionalSwitchDescription<T>(name, description, defaultValue,
CommandLineParserDetail::ValueStore<T>(value));
}
/**
* @brief Helper function to construct an optional switch description with default value and arbitrary action.
*
* @param name name of the switch.
* @param description descriptive text of the switch.
* @param defaultValue the default value to assign for this switch if none is provided.
* @param notifier function to invoke with the switch's value.
*/
template<typename T, typename F>
static CommandLineParserDetail::OptionalSwitchDescription<T> Switch(const String &name,
const String &description, const T &defaultValue, const F &notifier)
{
return CommandLineParserDetail::OptionalSwitchDescription<T>(name, description, defaultValue, notifier);
}
/**
* @brief Helper function to construct a boolean switch description and default store action.
*
* @param name name of the switch.
* @param description descriptive text of the switch.
* @param value
*/
ACTIV_BASE_API static CommandLineParserDetail::BooleanSwitchDescription BooleanSwitch(const String &name,
const String &description, bool &value);
/**
* @brief Helper function to construct a boolean switch description and arbitrary action.
*
* @param name name of the switch.
* @param description descriptive text of the switch.
* @param notifier function to invoke with the switch's value.
*/
template<typename F>
static CommandLineParserDetail::BooleanSwitchDescription BooleanSwitch(const String &name,
const String &description, const F &notifier)
{
return CommandLineParserDetail::BooleanSwitchDescription(name, description, notifier);
}
private:
CommandLineParserDetail::CommandLineParserImpl m_commandLineParserImpl;
CommandLineParserDetail::DescriptionAppender m_descriptionAppender;
};
} // namespace Activ
#endif // (!defined ACTIV_BASE_COMMAND_LINE_PARSER_H)