mirror of
https://github.com/QuasarApp/QuasarAppLib.git
synced 2025-04-29 11:14:39 +00:00
fix option validataor
This commit is contained in:
parent
fc80dba292
commit
93839d1dc0
@ -11,14 +11,14 @@
|
|||||||
|
|
||||||
namespace QuasarAppUtils{
|
namespace QuasarAppUtils{
|
||||||
|
|
||||||
OptionData::OptionData(const QString& name,
|
OptionData::OptionData(const QStringList& name,
|
||||||
const QString& arguments,
|
const QString& arguments,
|
||||||
const QString& description,
|
const QString& description,
|
||||||
const QString& example,
|
const QString& example,
|
||||||
const QString& depricatedMsg,
|
const QString& depricatedMsg,
|
||||||
bool removed) {
|
bool removed) {
|
||||||
|
|
||||||
setName(name);
|
setNames(name);
|
||||||
setArguments(arguments);
|
setArguments(arguments);
|
||||||
setDescription(description);
|
setDescription(description);
|
||||||
setExample(example);
|
setExample(example);
|
||||||
@ -26,11 +26,11 @@ OptionData::OptionData(const QString& name,
|
|||||||
_removed = removed;
|
_removed = removed;
|
||||||
}
|
}
|
||||||
|
|
||||||
const QString &OptionData::name() const {
|
const QStringList &OptionData::names() const {
|
||||||
return _name;
|
return _name;
|
||||||
}
|
}
|
||||||
|
|
||||||
void OptionData::setName(const QString &newName) {
|
void OptionData::setNames(const QStringList &newName) {
|
||||||
_name = newName;
|
_name = newName;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,6 +75,6 @@ bool OptionData::isDepricated() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Help::Options OptionData::toHelp() const {
|
Help::Options OptionData::toHelp() const {
|
||||||
return {{{name() + " " + arguments()}, {description() + " " + example()}}};
|
return {{{names().join(" / ") + " " + arguments()}, {description() + " " + example()}}};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
16
optiondata.h
16
optiondata.h
@ -23,14 +23,14 @@ public:
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief OptionData This is main constructor
|
* @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 arguments This is input arguments of this option or help meesage about arguments.
|
||||||
* @param description This is description message of this option.
|
* @param description This is description message of this option.
|
||||||
* @param example This is example of use string.
|
* @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 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.
|
* @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& arguments = "",
|
||||||
const QString& description = "",
|
const QString& description = "",
|
||||||
const QString& example = "",
|
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.
|
* @brief name This is name of the option. It is a required argument and cannot be empty.
|
||||||
* @return return name of this option.
|
* @return return name of this option.
|
||||||
*/
|
*/
|
||||||
const QString &name() const;
|
const QStringList &names() const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief description This is description message of this option.
|
* @brief description This is description message of this option.
|
||||||
@ -98,11 +98,11 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
/**
|
/**
|
||||||
* @brief setName This method sets new value of the option name.
|
* @brief setNames This method sets new value of the option name.
|
||||||
* @param newName This is a new value of the options name.
|
* @param newNames This is a new value of the options name.
|
||||||
* @note see the OptionData::name method.
|
* @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.
|
* @brief setDescription This method sets new description of this options.
|
||||||
@ -136,7 +136,7 @@ protected:
|
|||||||
void setDepricatedMsg(const QString &newDepricatedMsg);
|
void setDepricatedMsg(const QString &newDepricatedMsg);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QString _name;
|
QStringList _name;
|
||||||
QString _description;
|
QString _description;
|
||||||
QString _example;
|
QString _example;
|
||||||
QString _arguments;
|
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.
|
* @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
|
#endif // OPTIONDATA_H
|
||||||
|
48
params.cpp
48
params.cpp
@ -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() {
|
VerboseLvl Params::getVerboseLvl() {
|
||||||
return static_cast<VerboseLvl>(getArg("verbose", DEFAULT_VERBOSE_LVL).toInt());
|
return static_cast<VerboseLvl>(getArg("verbose", DEFAULT_VERBOSE_LVL).toInt());
|
||||||
@ -84,10 +74,6 @@ bool Params::isDebugBuild() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Params::showHelp() {
|
void Params::showHelp() {
|
||||||
Help::print(getParamsHelp() + userHelp);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Params::showUserHelp() {
|
|
||||||
Help::print(userHelp);
|
Help::print(userHelp);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,6 +95,23 @@ QString Params::getCurrentExecutableDir() {
|
|||||||
return appPath;
|
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() {
|
int Params::size() {
|
||||||
return params.size();
|
return params.size();
|
||||||
}
|
}
|
||||||
@ -188,7 +191,9 @@ bool Params::parseParams(const QStringList ¶msArray, const OptionsDataList &
|
|||||||
params.clear();
|
params.clear();
|
||||||
OptionsDataList availableOptions;
|
OptionsDataList availableOptions;
|
||||||
|
|
||||||
parseAvailableOptions(options, &availableOptions, &userHelp);
|
parseAvailableOptions(OptionsDataList{}.unite(options).unite(availableArguments()),
|
||||||
|
&availableOptions,
|
||||||
|
&userHelp);
|
||||||
|
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
char buffer[MAX_PATH];
|
char buffer[MAX_PATH];
|
||||||
@ -218,7 +223,7 @@ bool Params::parseParams(const QStringList ¶msArray, const OptionsDataList &
|
|||||||
|
|
||||||
for (int i = 0 ; i < paramsArray.size(); ++i) {
|
for (int i = 0 ; i < paramsArray.size(); ++i) {
|
||||||
|
|
||||||
auto optionData = availableOptions.value(paramsArray[i], {""});
|
auto optionData = availableOptions.value(paramsArray[i], {{}});
|
||||||
|
|
||||||
if (!optionData.isValid()) {
|
if (!optionData.isValid()) {
|
||||||
QuasarAppUtils::Params::log("You use wrong option name, please check the help before run your commnad.",
|
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))
|
if (!(availableOptionsListOut && helpOut))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
QHash<QString, Help::Options> options;
|
||||||
for (auto it = availableOptionsListIn.begin(); it != availableOptionsListIn.end(); ++it) {
|
for (auto it = availableOptionsListIn.begin(); it != availableOptionsListIn.end(); ++it) {
|
||||||
|
|
||||||
if (availableOptionsListOut) {
|
if (availableOptionsListOut) {
|
||||||
availableOptionsListOut->insert(it.value().name(), it.value());
|
for (const auto &name : qAsConst(it.value().names())) {
|
||||||
|
availableOptionsListOut->insert(name, it.value());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (helpOut) {
|
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) {
|
QString Params::getArg(const QString& key,const QString& def) {
|
||||||
|
18
params.h
18
params.h
@ -112,13 +112,6 @@ public:
|
|||||||
*/
|
*/
|
||||||
static void log(const QString& log, VerboseLvl vLvl = VerboseLvl::Debug);
|
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.
|
* @brief getVerboseLvl This method return the verbose log level.
|
||||||
* @return verbose log lvl.
|
* @return verbose log lvl.
|
||||||
@ -148,11 +141,6 @@ public:
|
|||||||
*/
|
*/
|
||||||
static void showHelp();
|
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.
|
* @brief getUserParamsMap This method return const reference to the parsed arguments map.
|
||||||
* @return A map object with parsed arguments.
|
* @return A map object with parsed arguments.
|
||||||
@ -176,6 +164,12 @@ public:
|
|||||||
*/
|
*/
|
||||||
static QString getCurrentExecutableDir();
|
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:
|
private:
|
||||||
static QString timeString();
|
static QString timeString();
|
||||||
static std::string lvlToString(VerboseLvl vLvl);
|
static std::string lvlToString(VerboseLvl vLvl);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user