decompose, add new main group "Application"

PICLI code brush
This commit is contained in:
2024-09-13 13:31:31 +03:00
parent cdc966bd8c
commit bf5bb45771
6 changed files with 166 additions and 113 deletions

View File

@@ -69,11 +69,6 @@
PICLI::PICLI(int argc, char * argv[]) { PICLI::PICLI(int argc, char * argv[]) {
needParse = debug_ = true;
_prefix_short = "-";
_prefix_full = "--";
_count_opt = 0;
_count_mand = 0;
for (int i = 0; i < argc; ++i) for (int i = 0; i < argc; ++i)
_args_raw << argv[i]; _args_raw << argv[i];
if (argc > 0) PISystemInfo::instance()->execCommand = argv[0]; if (argc > 0) PISystemInfo::instance()->execCommand = argv[0];
@@ -89,7 +84,7 @@ void PICLI::parse() {
if (cra.left(2) == _prefix_full) { if (cra.left(2) == _prefix_full) {
last = 0; last = 0;
full = cra.right(cra.length() - 2); full = cra.right(cra.length() - 2);
piForeach(Argument & a, _args) { for (auto & a: _args) {
if (a.full_key == full) { if (a.full_key == full) {
a.found = true; a.found = true;
last = &a; last = &a;
@@ -101,7 +96,7 @@ void PICLI::parse() {
last = 0; last = 0;
for (int j = 1; j < cra.length(); ++j) { for (int j = 1; j < cra.length(); ++j) {
bool found = false; bool found = false;
piForeach(Argument & a, _args) { for (auto & a: _args) {
if ((a.short_key != '\0') && (a.short_key == cra[j])) { if ((a.short_key != '\0') && (a.short_key == cra[j])) {
a.found = true; a.found = true;
last = &a; last = &a;
@@ -121,7 +116,7 @@ void PICLI::parse() {
_args_opt << cra; _args_opt << cra;
continue; continue;
} }
piCoutObj << "[PICli] Arguments overflow, \"" << cra << "\" ignored"; piCoutObj << "Arguments overflow, \"" << cra << "\" ignored";
} }
if (last == 0 ? false : last->has_value) { if (last == 0 ? false : last->has_value) {
last->value = cra; last->value = cra;
@@ -132,3 +127,129 @@ void PICLI::parse() {
} }
needParse = false; needParse = false;
} }
void PICLI::addArgument(const PIString & name, bool value) {
_args << Argument(name, name[0], name, value);
needParse = true;
}
void PICLI::addArgument(const PIString & name, const PIChar & shortKey, bool value) {
_args << Argument(name, shortKey, name, value);
needParse = true;
}
void PICLI::addArgument(const PIString & name, const char * shortKey, bool value) {
_args << Argument(name, PIChar::fromUTF8(shortKey), name, value);
needParse = true;
}
void PICLI::addArgument(const PIString & name, const PIChar & shortKey, const PIString & fullKey, bool value) {
_args << Argument(name, shortKey, fullKey, value);
needParse = true;
}
void PICLI::addArgument(const PIString & name, const char * shortKey, const PIString & fullKey, bool value) {
_args << Argument(name, PIChar::fromUTF8(shortKey), fullKey, value);
needParse = true;
}
PIString PICLI::rawArgument(int index) {
parse();
return _args_raw[index];
}
PIString PICLI::mandatoryArgument(int index) {
parse();
return _args_mand[index];
}
PIString PICLI::optionalArgument(int index) {
parse();
return _args_opt[index];
}
const PIStringList & PICLI::rawArguments() {
parse();
return _args_raw;
}
const PIStringList & PICLI::mandatoryArguments() {
parse();
return _args_mand;
}
const PIStringList & PICLI::optionalArguments() {
parse();
return _args_opt;
}
PIString PICLI::programCommand() {
parse();
return _args_raw.isNotEmpty() ? _args_raw.front() : PIString();
}
bool PICLI::hasArgument(const PIString & name) {
parse();
for (const auto & i: _args)
if (i.name == name && i.found) return true;
return false;
}
PIString PICLI::argumentValue(const PIString & name) {
parse();
for (const auto & i: _args)
if (i.name == name && i.found) return i.value;
return PIString();
}
PIString PICLI::argumentShortKey(const PIString & name) {
for (const auto & i: _args)
if (i.name == name) return PIString(i.short_key);
return PIString();
}
PIString PICLI::argumentFullKey(const PIString & name) {
for (const auto & i: _args)
if (i.name == name) return i.full_key;
return PIString();
}
void PICLI::setShortKeyPrefix(const PIString & prefix) {
_prefix_short = prefix;
needParse = true;
}
void PICLI::setFullKeyPrefix(const PIString & prefix) {
_prefix_full = prefix;
needParse = true;
}
void PICLI::setMandatoryArgumentsCount(const int count) {
_count_mand = count;
needParse = true;
}
void PICLI::setOptionalArgumentsCount(const int count) {
_count_opt = count;
needParse = true;
}

View File

@@ -1,5 +1,5 @@
/*! \file picli.h /*! \file picli.h
* \ingroup Core * \ingroup Application
* \~\brief * \~\brief
* \~english Command-Line parser * \~english Command-Line parser
* \~russian Парсер командной строки * \~russian Парсер командной строки
@@ -29,7 +29,7 @@
#include "piset.h" #include "piset.h"
#include "pistringlist.h" #include "pistringlist.h"
//! \ingroup Core //! \ingroup Application
//! \~\brief //! \~\brief
//! \~english Command-Line parser. //! \~english Command-Line parser.
//! \~russian Парсер командной строки. //! \~russian Парсер командной строки.
@@ -42,131 +42,65 @@ public:
//! \~english Add argument with name "name", short key = name first letter and full key = name. //! \~english Add argument with name "name", short key = name first letter and full key = name.
//! \~russian Добавляет аргумент с именем "name", коротким ключом = первой букве имени и полным ключом = имени. //! \~russian Добавляет аргумент с именем "name", коротким ключом = первой букве имени и полным ключом = имени.
void addArgument(const PIString & name, bool value = false) { void addArgument(const PIString & name, bool value = false);
_args << Argument(name, name[0], name, value);
needParse = true;
}
//! \~english Add argument with name "name", short key = "shortKey" and full key = name. //! \~english Add argument with name "name", short key = "shortKey" and full key = name.
//! \~russian Добавляет аргумент с именем "name", коротким ключом = "shortKey" и полным ключом = имени. //! \~russian Добавляет аргумент с именем "name", коротким ключом = "shortKey" и полным ключом = имени.
void addArgument(const PIString & name, const PIChar & shortKey, bool value = false) { void addArgument(const PIString & name, const PIChar & shortKey, bool value = false);
_args << Argument(name, shortKey, name, value);
needParse = true;
}
//! \~english Add argument with name "name", short key = "shortKey" and full key = name. //! \~english Add argument with name "name", short key = "shortKey" and full key = name.
//! \~russian Добавляет аргумент с именем "name", коротким ключом = "shortKey" и полным ключом = имени. //! \~russian Добавляет аргумент с именем "name", коротким ключом = "shortKey" и полным ключом = имени.
void addArgument(const PIString & name, const char * shortKey, bool value = false) { void addArgument(const PIString & name, const char * shortKey, bool value = false);
_args << Argument(name, PIChar::fromUTF8(shortKey), name, value);
needParse = true;
}
//! \~english Add argument with name "name", short key = "shortKey" and full key = "fullKey". //! \~english Add argument with name "name", short key = "shortKey" and full key = "fullKey".
//! \~russian Добавляет аргумент с именем "name", коротким ключом = "shortKey" и полным ключом = "fullKey". //! \~russian Добавляет аргумент с именем "name", коротким ключом = "shortKey" и полным ключом = "fullKey".
void addArgument(const PIString & name, const PIChar & shortKey, const PIString & fullKey, bool value = false) { void addArgument(const PIString & name, const PIChar & shortKey, const PIString & fullKey, bool value = false);
_args << Argument(name, shortKey, fullKey, value);
needParse = true;
}
//! \~english Add argument with name "name", short key = "shortKey" and full key = "fullKey". //! \~english Add argument with name "name", short key = "shortKey" and full key = "fullKey".
//! \~russian Добавляет аргумент с именем "name", коротким ключом = "shortKey" и полным ключом = "fullKey". //! \~russian Добавляет аргумент с именем "name", коротким ключом = "shortKey" и полным ключом = "fullKey".
void addArgument(const PIString & name, const char * shortKey, const PIString & fullKey, bool value = false) { void addArgument(const PIString & name, const char * shortKey, const PIString & fullKey, bool value = false);
_args << Argument(name, PIChar::fromUTF8(shortKey), fullKey, value);
needParse = true;
}
//! \~english Returns unparsed command-line argument by index "index". Index 0 is program execute command. //! \~english Returns unparsed command-line argument by index "index". Index 0 is program execute command.
//! \~russian Возвращает исходный аргумент командной строки по индексу "index". Индекс 0 это команда вызова программы. //! \~russian Возвращает исходный аргумент командной строки по индексу "index". Индекс 0 это команда вызова программы.
PIString rawArgument(int index) { PIString rawArgument(int index);
parse(); PIString mandatoryArgument(int index);
return _args_raw[index]; PIString optionalArgument(int index);
}
PIString mandatoryArgument(int index) {
parse();
return _args_mand[index];
}
PIString optionalArgument(int index) {
parse();
return _args_opt[index];
}
//! \~english Returns unparsed command-line arguments. //! \~english Returns unparsed command-line arguments.
//! \~russian Возвращает исходные аргументы командной строки. //! \~russian Возвращает исходные аргументы командной строки.
const PIStringList & rawArguments() { const PIStringList & rawArguments();
parse(); const PIStringList & mandatoryArguments();
return _args_raw; const PIStringList & optionalArguments();
}
const PIStringList & mandatoryArguments() {
parse();
return _args_mand;
}
const PIStringList & optionalArguments() {
parse();
return _args_opt;
}
//! \~english Returns program execute command without arguments. //! \~english Returns program execute command without arguments.
//! \~russian Возвращает команду вызова программы без аргументов. //! \~russian Возвращает команду вызова программы без аргументов.
PIString programCommand() { PIString programCommand();
parse();
return _args_raw.size() > 0 ? _args_raw.front() : PIString();
}
//! \~english Returns if argument "name" found. //! \~english Returns if argument "name" found.
//! \~russian Возвращает найден ли аргумент "name". //! \~russian Возвращает найден ли аргумент "name".
bool hasArgument(const PIString & name) { bool hasArgument(const PIString & name);
parse();
piForeach(Argument & i, _args)
if (i.name == name && i.found) return true;
return false;
}
//! \~english Returns argument "name" value, or empty string if this is no value. //! \~english Returns argument "name" value, or empty string if this is no value.
//! \~russian Возвращает значение аргумента "name" или пустую строку, если значения нет. //! \~russian Возвращает значение аргумента "name" или пустую строку, если значения нет.
PIString argumentValue(const PIString & name) { PIString argumentValue(const PIString & name);
parse();
piForeach(Argument & i, _args)
if (i.name == name && i.found) return i.value;
return PIString();
}
//! \~english Returns short key of argument "name", or empty string if this is no argument. //! \~english Returns short key of argument "name", or empty string if this is no argument.
//! \~russian Возвращает короткий ключ аргумента "name" или пустую строку, если аргумента нет. //! \~russian Возвращает короткий ключ аргумента "name" или пустую строку, если аргумента нет.
PIString argumentShortKey(const PIString & name) { PIString argumentShortKey(const PIString & name);
piForeach(Argument & i, _args)
if (i.name == name) return PIString(i.short_key);
return PIString();
}
//! \~english Returns full key of argument "name", or empty string if this is no argument. //! \~english Returns full key of argument "name", or empty string if this is no argument.
//! \~russian Возвращает полный ключ аргумента "name" или пустую строку, если аргумента нет. //! \~russian Возвращает полный ключ аргумента "name" или пустую строку, если аргумента нет.
PIString argumentFullKey(const PIString & name) { PIString argumentFullKey(const PIString & name);
piForeach(Argument & i, _args)
if (i.name == name) return i.full_key;
return PIString();
}
const PIString & shortKeyPrefix() const { return _prefix_short; } const PIString & shortKeyPrefix() const { return _prefix_short; }
const PIString & fullKeyPrefix() const { return _prefix_full; } const PIString & fullKeyPrefix() const { return _prefix_full; }
int mandatoryArgumentsCount() const { return _count_mand; } int mandatoryArgumentsCount() const { return _count_mand; }
int optionalArgumentsCount() const { return _count_opt; } int optionalArgumentsCount() const { return _count_opt; }
void setShortKeyPrefix(const PIString & prefix) { void setShortKeyPrefix(const PIString & prefix);
_prefix_short = prefix; void setFullKeyPrefix(const PIString & prefix);
needParse = true; void setMandatoryArgumentsCount(const int count);
} void setOptionalArgumentsCount(const int count);
void setFullKeyPrefix(const PIString & prefix) {
_prefix_full = prefix;
needParse = true;
}
void setMandatoryArgumentsCount(const int count) {
_count_mand = count;
needParse = true;
}
void setOptionalArgumentsCount(const int count) {
_count_opt = count;
needParse = true;
}
bool debug() const { return debug_; } bool debug() const { return debug_; }
void setDebug(bool debug) { debug_ = debug; } void setDebug(bool debug) { debug_ = debug; }
@@ -175,29 +109,28 @@ public:
private: private:
struct Argument { struct Argument {
Argument() { has_value = found = false; } Argument() {}
Argument(const PIString & n, const PIChar & s, const PIString & f, bool v) { Argument(const PIString & n, const PIChar & s, const PIString & f, bool v) {
name = n; name = n;
short_key = s; short_key = s;
full_key = f; full_key = f;
has_value = v; has_value = v;
found = false;
} }
PIString name; PIString name;
PIChar short_key; PIChar short_key;
PIString full_key; PIString full_key;
PIString value; PIString value;
bool has_value, found; bool has_value = false, found = false;
}; };
void parse(); void parse();
PIString _prefix_short, _prefix_full; PIString _prefix_short = "-", _prefix_full = "--";
PIStringList _args_raw, _args_mand, _args_opt; PIStringList _args_raw, _args_mand, _args_opt;
PISet<PIString> keys_full, keys_short; PISet<PIString> keys_full, keys_short;
PIVector<Argument> _args; PIVector<Argument> _args;
int _count_mand, _count_opt; int _count_mand = 0, _count_opt = 0;
bool needParse, debug_; bool needParse = true, debug_ = true;
}; };
#endif // PICLI_H #endif // PICLI_H

View File

@@ -136,7 +136,7 @@ void PISingleApplication::run() {
shm->read(readed.data(), readed.size(), hdr_sz); shm->read(readed.data(), readed.size(), hdr_sz);
PIByteArray msg; PIByteArray msg;
readed >> msg; readed >> msg;
if (!msg.isEmpty()) { if (msg.isNotEmpty()) {
messageReceived(msg); messageReceived(msg);
// piCoutObj << "message" << msg; // piCoutObj << "message" << msg;
} }

View File

@@ -1,5 +1,5 @@
/*! \file pisingleapplication.h /*! \file pisingleapplication.h
* \ingroup System * \ingroup Application
* \~\brief * \~\brief
* \~english Single-instance application control * \~english Single-instance application control
* \~russian Контроль одного экземпляра приложения * \~russian Контроль одного экземпляра приложения
@@ -30,7 +30,7 @@
class PISharedMemory; class PISharedMemory;
//! \ingroup System //! \ingroup Application
//! \~\brief //! \~\brief
//! \~english Single-instance application control. //! \~english Single-instance application control.
//! \~russian Контроль одного экземпляра приложения. //! \~russian Контроль одного экземпляра приложения.

View File

@@ -20,8 +20,7 @@
#include "pisystemmonitor.h" #include "pisystemmonitor.h"
#include "pidir.h" #include "pidir.h"
#include "piincludes_p.h" #include "piliterals_string.h"
#include "piliterals.h"
#include "piprocess.h" #include "piprocess.h"
#include "pisysteminfo.h" #include "pisysteminfo.h"
#include "pitime_win.h" #include "pitime_win.h"

View File

@@ -1,5 +1,5 @@
/*! \file pisystemmonitor.h /*! \file pisystemmonitor.h
* \ingroup System * \ingroup Application
* \~\brief * \~\brief
* \~english System resources monitoring * \~english System resources monitoring
* \~russian Мониторинг ресурсов системы * \~russian Мониторинг ресурсов системы
@@ -30,7 +30,7 @@
#include "pithread.h" #include "pithread.h"
//! \ingroup System //! \ingroup Application
//! \~\brief //! \~\brief
//! \~english Process monitoring. //! \~english Process monitoring.
//! \~russian Мониторинг процесса. //! \~russian Мониторинг процесса.
@@ -46,7 +46,7 @@ public:
~PISystemMonitor(); ~PISystemMonitor();
#pragma pack(push, 1) #pragma pack(push, 1)
//! \ingroup System //! \ingroup Application
//! \~\brief //! \~\brief
//! \~english Process statistics (fixed-size fields). //! \~english Process statistics (fixed-size fields).
//! \~russian Статистика процесса (фиксированные поля). //! \~russian Статистика процесса (фиксированные поля).
@@ -116,7 +116,7 @@ public:
float cpu_load_user = 0.f; float cpu_load_user = 0.f;
}; };
//! \ingroup System //! \ingroup Application
//! \~\brief //! \~\brief
//! \~english Thread statistics (fixed-size fields). //! \~english Thread statistics (fixed-size fields).
//! \~russian Статистика потока (фиксированные поля). //! \~russian Статистика потока (фиксированные поля).
@@ -151,7 +151,7 @@ public:
}; };
#pragma pack(pop) #pragma pack(pop)
//! \ingroup System //! \ingroup Application
//! \~\brief //! \~\brief
//! \~english Process statistics. //! \~english Process statistics.
//! \~russian Статистика процесса. //! \~russian Статистика процесса.
@@ -189,7 +189,7 @@ public:
PIString data_memsize_readable; PIString data_memsize_readable;
}; };
//! \ingroup System //! \ingroup Application
//! \~\brief //! \~\brief
//! \~english Thread statistics. //! \~english Thread statistics.
//! \~russian Статистика потока. //! \~russian Статистика потока.