13#include <QCoreApplication>
18#include <mach-o/dyld.h>
28QMap<QString, QString> Params::params = QMap<QString, QString>();
29QString Params::appPath =
"";
30QString Params::appName =
"";
36 return params.contains(key);
47 qCritical().noquote() <<
log;
51 qWarning().noquote() <<
log;
55 qDebug().noquote() <<
log;
61 qInfo().noquote() <<
log;
89 if (inputOptions.size() > 1) {
102 if (inputOptions.size() <= 1 ) {
107 for (
const auto &optionData: std::as_const(inputOptions) ) {
108 help.unite(optionData.toHelp());
111 return Help::Section{{
"Information about using options", help}};
141 {
"-verbose"},
"(level 1 - 3)",
"Shows debug log"
148 {
"-fileLog"},
"(path to file)",
"Sets path of log file. Default it is path to executable file with suffix '.log'"
155 return params.size();
158bool Params::optionsForEach(
const QStringList ¶msArray,
161 for (
int i = 0 ; i < paramsArray.size(); ++i) {
163 QStringList virtualOptionsList = paramsArray[i].split(
'=');
165 if (virtualOptionsList.size() > 1) {
166 return optionsForEach(virtualOptionsList, availableOptions);
169 auto optionData = availableOptions.value(paramsArray[i], {{}});
170 if (!checkOption(optionData, paramsArray[i])) {
174 inputOptions.insert(paramsArray[i], optionData);
176 if (paramsArray[i][0] ==
'-') {
178 if (i < (paramsArray.size() - 1) && paramsArray[i + 1][0] !=
'-') {
179 params[paramsArray[i].mid(1)] = paramsArray[i + 1];
182 qCritical() <<
"Missing argument for " + paramsArray[i];
186 params[paramsArray[i]] =
"";
196 for (
int i = 1; i < argc; i++) {
197 params.push_back(argv[i]);
204 return parseParams(argc,
const_cast<const char**
>(argv), options);
216 char buffer[MAX_PATH];
217 memset(buffer, 0,
sizeof buffer);
219 GetModuleFileNameA(
nullptr, buffer, MAX_PATH);
220 appPath = QFileInfo(buffer).absolutePath();
221 appName = QFileInfo(buffer).fileName();
226 memset(path, 0,
sizeof path);
228 if (readlink(
"/proc/self/exe", path, 2048) < 0) {
229 qCritical() <<
"parseParams can't get self path!";
232 appPath = QFileInfo(path).absolutePath();
233 appName = QFileInfo(path).fileName();
238 _NSGetExecutablePath(
nullptr, &
size);
239 std::vector<char> buffer(
size);
240 if (_NSGetExecutablePath(buffer.data(), &
size) == 0) {
241 appPath = QString::fromUtf8(buffer.data());
246 appName = QCoreApplication::applicationName();
249 if (!appPath.size()) {
253 if (!optionsForEach(paramsArray, availableOptions)) {
257 printWorkingOptions();
262void Params::printWorkingOptions() {
263 qDebug() <<
"--- Working options table start ---";
265 QMap<QString, QString>::const_iterator iter = params.constBegin();
266 while (iter != params.constEnd()) {
268 QString row = QString{
"Option[%0]"}.arg(iter.key());
270 QString value = iter.value();
271 if (!value.isEmpty()) {
272 row += QString{
": %1"}.arg(value);
280 qDebug() <<
"--- Working options table end ---";
283bool Params::checkOption(
const OptionData& optionData,
const QString& rawOptionName) {
285#ifndef QA_ALLOW_NOT_SUPPORTED_OPTIONS
288 qCritical() << QString(
"The '%0' option not exists!"
289 " You use wrong option name,"
290 " please check the help before run your command.").
296 Q_UNUSED(rawOptionName);
309 qWarning() << QString(
"The %0 option(s) marked as deprecated! "
310 "And most likely will be removed in next release.").
311 arg(optionData.
names().join(
"/"));
313 qWarning() << QString(
"Option message: %0").arg(optionData.
deprecatedMsg());
320void Params::parseAvailableOptions(
const OptionsDataList &availableOptionsListIn,
324 if (!(availableOptionsListOut && helpOut))
329 QHash<QString, Help::Options> options;
330 for (
auto it = availableOptionsListIn.begin(); it != availableOptionsListIn.end(); ++it) {
332 if (availableOptionsListOut) {
333 for (
const auto &name : std::as_const(it.value().names())) {
334 availableOptionsListOut->insert(name, it.value());
339 options[it.key()].unite(it.value().toHelp());
343 for (
auto it = options.begin(); it != options.end(); ++it) {
344 helpOut->insert(it.key(), it.value());
349 return params.value(key, def);
353 params.insert(key, val);
358 params.insert(key,
"");
The OptionData class contains information about one option.
bool isDeprecated() const
isDeprecated This method return true if this option is deprecated.
const QString & deprecatedMsg() const
deprecatedWarning This is a message that will be printed as a warning if user will use this option....
bool isValid() const
isValid This method return true if the option is valid. If option do not contain a name then it is in...
bool isRemoved() const
isRemoved This method return true if the option is removed.
const QStringList & names() const
name This is name of the option. It is a required argument and cannot be empty.
static void clearParsedData()
clearParsedData This method clear all parsed data.
static void showHelpForInputOptions()
showHelpForInputOptions This method show help for each input option.
static void showHelp()
showHelp This method shows all help message.
static void setArg(const QString &key, const QString &val)
setArg sets a new value of a key.
static QString getCurrentExecutableDir()
getCurrentExecutableDir This method return a path to a folder with the current executable.
static void setEnable(const QString &key, bool enable)
setEnable This method sets boolean value of key.
static bool isEndable(const QString &key)
isEndable This method check if enable a key argument.
static const Help::Section & getHelp()
getHelp This method return options help page.
static bool isDebug()
isDebug This method return true if the application verbose level >= VerboseLvl::Debug.
static VerboseLvl getVerboseLvl()
getVerboseLvl This method return the verbose log level.
static OptionsDataList availableArguments()
availableArguments This method return list of the available arguments of QuasarAppLibrary
static QString getArg(const QString &key, const QString &def={})
getArg return string value of a key if key is exits else return a def value. If a def value not defin...
static Help::Section getHelpOfInputOptions()
getHelpOfInputOptions This method return help abut input options only. Except help and h options.
static int size()
size This method return count of the all input arguments.
static void log(const QString &log, VerboseLvl vLvl=VerboseLvl::Debug)
log This method print log text on console.
static QString getCurrentExecutable()
getCurrentExecutable This method return path to the current executable.
static bool isDebugBuild()
isDebugBuild This method return true if the library buildet in debug mode.
static bool parseParams(const int argc, const char *argv[], const OptionsDataList &options={})
parseParams Parse input data of started application.
static const QMap< QString, QString > & getUserParamsMap()
getUserParamsMap This method return const reference to the parsed arguments map.
QMultiMap< QString, QString > Options
Options this is list of key-descriptions pairs of help. The key is name of the available argument and...
void print(const QString &key, const QString &value, int keyLength)
QMultiMap< QString, Options > Section
Section This is list of the help Sections. The one section it is Title of the section and Help::Optio...
The QuasarAppUtils namespace contains the base functions for all applications of the QuasarApp group....
QMultiHash< QString, OptionData > OptionsDataList
OptionsList is Hash map of the OptionData items. Where the key it is group name and value it is optio...
VerboseLvl
The VerboseLvl enum uses for sets log level.
@ Error
Error message. This logs will marked as a Error and printing if the verbose lvl >= 0.
@ Warning
Warning message. This logs will marked as a Warning and printing if the verbose lvl >= 1.
@ Info
General information. This logs will marked as a Info and and printing if the verbose lvl >= 2.
@ Debug
Debug message. This logs will marked as a Debug and printing if the verbose lvl >= 3.
#define DEFAULT_VERBOSE_LVL
QHashType & unite(QHashType &left, const QHashType &right)
Simplified union operation for QHash.