fix option validataor

This commit is contained in:
Andrei Yankovich 2021-07-27 17:24:43 +03:00
parent fc80dba292
commit 93839d1dc0
4 changed files with 49 additions and 43 deletions

View File

@ -11,14 +11,14 @@
namespace QuasarAppUtils{
OptionData::OptionData(const QString& name,
OptionData::OptionData(const QStringList& name,
const QString& arguments,
const QString& description,
const QString& example,
const QString& depricatedMsg,
bool removed) {
setName(name);
setNames(name);
setArguments(arguments);
setDescription(description);
setExample(example);
@ -26,11 +26,11 @@ OptionData::OptionData(const QString& name,
_removed = removed;
}
const QString &OptionData::name() const {
const QStringList &OptionData::names() const {
return _name;
}
void OptionData::setName(const QString &newName) {
void OptionData::setNames(const QStringList &newName) {
_name = newName;
}
@ -75,6 +75,6 @@ bool OptionData::isDepricated() const {
}
Help::Options OptionData::toHelp() const {
return {{{name() + " " + arguments()}, {description() + " " + example()}}};
return {{{names().join(" / ") + " " + arguments()}, {description() + " " + example()}}};
}
}

View File

@ -23,14 +23,14 @@ public:
/**
* @brief OptionData This is main constructor
* @param name This is name of the option. It is a required argument and cannot be empty.
* @param names This is names list of the option. It is a required argument and cannot be empty.
* @param arguments This is input arguments of this option or help meesage about arguments.
* @param description This is description message of this option.
* @param example This is example of use string.
* @param depricatedMsg This is a message that will be printed as a warning if user will use this option. If you set this argument to empty value then warning message will be ignored and option not be marked asa a depricated. An option will be marked as a depricated when this arguments will not equal empty string.
* @param removed This option show depricatedMsg as a error and force the parseParams method return false. This option will be ignored if the depricatedMsg will be empty.
*/
OptionData(const QString& name,
OptionData(const QStringList& names,
const QString& arguments = "",
const QString& description = "",
const QString& example = "",
@ -41,7 +41,7 @@ public:
* @brief name This is name of the option. It is a required argument and cannot be empty.
* @return return name of this option.
*/
const QString &name() const;
const QStringList &names() const;
/**
* @brief description This is description message of this option.
@ -98,11 +98,11 @@ public:
protected:
/**
* @brief setName This method sets new value of the option name.
* @param newName This is a new value of the options name.
* @brief setNames This method sets new value of the option name.
* @param newNames This is a new value of the options name.
* @note see the OptionData::name method.
*/
void setName(const QString &newName);
void setNames(const QStringList &newNames);
/**
* @brief setDescription This method sets new description of this options.
@ -136,7 +136,7 @@ protected:
void setDepricatedMsg(const QString &newDepricatedMsg);
private:
QString _name;
QStringList _name;
QString _description;
QString _example;
QString _arguments;
@ -147,6 +147,6 @@ private:
/**
* @brief OptionsList is Hash map of the OptionData items. Where the key it is group name and value it is option data.
*/
typedef QHash<QString, OptionData> OptionsDataList;
typedef QMultiHash<QString, OptionData> OptionsDataList;
}
#endif // OPTIONDATA_H

View File

@ -56,16 +56,6 @@ void Params::log(const QString &log, VerboseLvl vLvl) {
}
}
Help::Charters Params::getParamsHelp() {
return {
{
"Base Options", {
{"-verbose (level 1 - 3)", "Shows debug log"},
{"-fileLog (path to file)", "Sets path of log file. Default it is path to executable file with suffix '.log'"},
}
}
};
}
VerboseLvl Params::getVerboseLvl() {
return static_cast<VerboseLvl>(getArg("verbose", DEFAULT_VERBOSE_LVL).toInt());
@ -84,10 +74,6 @@ bool Params::isDebugBuild() {
}
void Params::showHelp() {
Help::print(getParamsHelp() + userHelp);
}
void Params::showUserHelp() {
Help::print(userHelp);
}
@ -109,6 +95,23 @@ QString Params::getCurrentExecutableDir() {
return appPath;
}
OptionsDataList Params::availableArguments() {
return OptionsDataList{
{
"Base Options",
OptionData{
{"-verbose"}, "(level 1 - 3)", "Shows debug log"
}
},
{
"Base Options",
OptionData{
{"-fileLog"}, "(path to file)", "Sets path of log file. Default it is path to executable file with suffix '.log'"
}
}
};
}
int Params::size() {
return params.size();
}
@ -188,7 +191,9 @@ bool Params::parseParams(const QStringList &paramsArray, const OptionsDataList &
params.clear();
OptionsDataList availableOptions;
parseAvailableOptions(options, &availableOptions, &userHelp);
parseAvailableOptions(OptionsDataList{}.unite(options).unite(availableArguments()),
&availableOptions,
&userHelp);
#ifdef Q_OS_WIN
char buffer[MAX_PATH];
@ -218,7 +223,7 @@ bool Params::parseParams(const QStringList &paramsArray, const OptionsDataList &
for (int i = 0 ; i < paramsArray.size(); ++i) {
auto optionData = availableOptions.value(paramsArray[i], {""});
auto optionData = availableOptions.value(paramsArray[i], {{}});
if (!optionData.isValid()) {
QuasarAppUtils::Params::log("You use wrong option name, please check the help before run your commnad.",
@ -290,16 +295,23 @@ void Params::parseAvailableOptions(const OptionsDataList &availableOptionsListIn
if (!(availableOptionsListOut && helpOut))
return;
QHash<QString, Help::Options> options;
for (auto it = availableOptionsListIn.begin(); it != availableOptionsListIn.end(); ++it) {
if (availableOptionsListOut) {
availableOptionsListOut->insert(it.value().name(), it.value());
for (const auto &name : qAsConst(it.value().names())) {
availableOptionsListOut->insert(name, it.value());
}
}
if (helpOut) {
helpOut->insert(it.key(), it.value().toHelp());
options[it.key()].unite(it.value().toHelp());
}
}
for (auto it = options.begin(); it != options.end(); ++it) {
helpOut->insert(it.key(), it.value());
}
}
QString Params::getArg(const QString& key,const QString& def) {

View File

@ -112,13 +112,6 @@ public:
*/
static void log(const QString& log, VerboseLvl vLvl = VerboseLvl::Debug);
/**
* @brief getParamsHelp This method return help object of the params class.
* @note All Options from the Params class can be used on any application that incuded this library. So if you printing your own help do not forget print this help.
* @return help object of default params.
*/
static Help::Section getParamsHelp();
/**
* @brief getVerboseLvl This method return the verbose log level.
* @return verbose log lvl.
@ -148,11 +141,6 @@ public:
*/
static void showHelp();
/**
* @brief showUserHelp This method shows help message that has generated after invoke the parseParams method.
*/
static void showUserHelp();
/**
* @brief getUserParamsMap This method return const reference to the parsed arguments map.
* @return A map object with parsed arguments.
@ -176,6 +164,12 @@ public:
*/
static QString getCurrentExecutableDir();
/**
* @brief availableArguments This method return list of the available arguments of QuasarAppLibrary
* @return list of the available arguments
*/
static OptionsDataList availableArguments();
private:
static QString timeString();
static std::string lvlToString(VerboseLvl vLvl);