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

View File

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

View File

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

View File

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

View File

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