/*! \file picli.h * \ingroup Core * \~\brief * \~english Command-Line parser * \~russian Парсер командной строки */ /* PIP - Platform Independent Primitives Command-Line Parser Ivan Pelipenko peri4ko@yandex.ru This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ #ifndef PICLI_H #define PICLI_H #include "pistringlist.h" #include "piset.h" //! \ingroup Core //! \~\brief //! \~english Command-Line parser. //! \~russian Парсер командной строки. class PIP_EXPORT PICLI { public: //! \~english Constructor //! \~russian Конструктор PICLI(int argc, char * argv[]); //! \~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;} //! \~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;} //! \~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(shortKey), name, value); needParse = true;} //! \~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;} //! \~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(shortKey), fullKey, value); needParse = true;} //! \~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];} //! \~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;} //! \~english Returns program execute command without arguments //! \~russian Возвращает команду вызова программы без аргументов PIString programCommand() {parse(); return _args_raw.size() > 0 ? _args_raw.front() : PIString();} //! \~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;} //! \~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 argumentShortKey(const PIString & name) {piForeach (Argument &i, _args) if (i.name == name) return i.short_key; return PIString();} 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 & 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;} bool debug() const {return debug_;} void setDebug(bool debug) {debug_ = debug;} PIConstChars className() const {return "PICLI";} PIString name() const {return PIStringAscii("CLI");} private: struct Argument { Argument() {has_value = found = false;} 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; }; void parse(); PIString _prefix_short, _prefix_full; PIStringList _args_raw, _args_mand, _args_opt; PISet keys_full, keys_short; PIVector _args; int _count_mand, _count_opt; bool needParse, debug_; }; #endif // PICLI_H