reformatted to Mozilla code style
This commit is contained in:
committed by
borgmanJeremy
parent
c0e2e48db4
commit
c8d15205be
@@ -17,38 +17,46 @@
|
||||
|
||||
#include "commandargument.h"
|
||||
|
||||
CommandArgument::CommandArgument() {
|
||||
CommandArgument::CommandArgument() {}
|
||||
|
||||
}
|
||||
CommandArgument::CommandArgument(const QString& name,
|
||||
const QString& description)
|
||||
: m_name(name)
|
||||
, m_description(description)
|
||||
{}
|
||||
|
||||
CommandArgument::CommandArgument(const QString &name,
|
||||
const QString &description) :
|
||||
m_name(name), m_description(description)
|
||||
void
|
||||
CommandArgument::setName(const QString& name)
|
||||
{
|
||||
|
||||
m_name = name;
|
||||
}
|
||||
|
||||
void CommandArgument::setName(const QString &name) {
|
||||
m_name = name;
|
||||
QString
|
||||
CommandArgument::name() const
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
|
||||
QString CommandArgument::name() const {
|
||||
return m_name;
|
||||
void
|
||||
CommandArgument::setDescription(const QString& description)
|
||||
{
|
||||
m_description = description;
|
||||
}
|
||||
|
||||
void CommandArgument::setDescription(const QString &description) {
|
||||
m_description = description;
|
||||
QString
|
||||
CommandArgument::description() const
|
||||
{
|
||||
return m_description;
|
||||
}
|
||||
|
||||
QString CommandArgument::description() const {
|
||||
return m_description;
|
||||
bool
|
||||
CommandArgument::isRoot() const
|
||||
{
|
||||
return m_name.isEmpty() && m_description.isEmpty();
|
||||
}
|
||||
|
||||
bool CommandArgument::isRoot() const {
|
||||
return m_name.isEmpty() && m_description.isEmpty();
|
||||
}
|
||||
|
||||
bool CommandArgument::operator ==(const CommandArgument &arg) const {
|
||||
return m_description == arg.m_description
|
||||
&& m_name == arg.m_name;
|
||||
bool
|
||||
CommandArgument::operator==(const CommandArgument& arg) const
|
||||
{
|
||||
return m_description == arg.m_description && m_name == arg.m_name;
|
||||
}
|
||||
|
||||
@@ -19,22 +19,23 @@
|
||||
|
||||
#include <QString>
|
||||
|
||||
class CommandArgument {
|
||||
class CommandArgument
|
||||
{
|
||||
public:
|
||||
CommandArgument();
|
||||
explicit CommandArgument(const QString &name, const QString &description);
|
||||
CommandArgument();
|
||||
explicit CommandArgument(const QString& name, const QString& description);
|
||||
|
||||
void setName(const QString &name);
|
||||
QString name() const;
|
||||
void setName(const QString& name);
|
||||
QString name() const;
|
||||
|
||||
void setDescription(const QString &description);
|
||||
QString description() const;
|
||||
void setDescription(const QString& description);
|
||||
QString description() const;
|
||||
|
||||
bool isRoot() const;
|
||||
bool isRoot() const;
|
||||
|
||||
bool operator ==(const CommandArgument &arg) const;
|
||||
bool operator==(const CommandArgument& arg) const;
|
||||
|
||||
private:
|
||||
QString m_name;
|
||||
QString m_description;
|
||||
QString m_name;
|
||||
QString m_description;
|
||||
};
|
||||
|
||||
@@ -19,384 +19,405 @@
|
||||
#include <QApplication>
|
||||
#include <QTextStream>
|
||||
|
||||
CommandLineParser::CommandLineParser() :
|
||||
m_description(qApp->applicationName())
|
||||
{
|
||||
|
||||
}
|
||||
CommandLineParser::CommandLineParser()
|
||||
: m_description(qApp->applicationName())
|
||||
{}
|
||||
|
||||
namespace {
|
||||
|
||||
QTextStream out(stdout);
|
||||
QTextStream err(stderr);
|
||||
|
||||
auto versionOption = CommandOption({"v", "version"},
|
||||
QStringLiteral("Displays version information"));
|
||||
auto helpOption = CommandOption({"h", "help"},
|
||||
QStringLiteral("Displays this help"));
|
||||
auto versionOption =
|
||||
CommandOption({ "v", "version" },
|
||||
QStringLiteral("Displays version information"));
|
||||
auto helpOption =
|
||||
CommandOption({ "h", "help" }, QStringLiteral("Displays this help"));
|
||||
|
||||
QString optionsToString(const QList<CommandOption> &options,
|
||||
const QList<CommandArgument> &arguments) {
|
||||
int size = 0; // track the largest size
|
||||
QStringList dashedOptionList;
|
||||
// save the dashed options and its size in order to print the description
|
||||
// of every option at the same horizontal character position.
|
||||
for (auto const &option: options) {
|
||||
QStringList dashedOptions = option.dashedNames();
|
||||
QString joinedDashedOptions = dashedOptions.join(QStringLiteral(", "));
|
||||
if (!option.valueName().isEmpty()) {
|
||||
joinedDashedOptions += QStringLiteral(" <%1>")
|
||||
.arg(option.valueName());
|
||||
}
|
||||
if (joinedDashedOptions.length() > size) {
|
||||
size = joinedDashedOptions.length();
|
||||
}
|
||||
dashedOptionList << joinedDashedOptions;
|
||||
QString
|
||||
optionsToString(const QList<CommandOption>& options,
|
||||
const QList<CommandArgument>& arguments)
|
||||
{
|
||||
int size = 0; // track the largest size
|
||||
QStringList dashedOptionList;
|
||||
// save the dashed options and its size in order to print the description
|
||||
// of every option at the same horizontal character position.
|
||||
for (auto const& option : options) {
|
||||
QStringList dashedOptions = option.dashedNames();
|
||||
QString joinedDashedOptions = dashedOptions.join(QStringLiteral(", "));
|
||||
if (!option.valueName().isEmpty()) {
|
||||
joinedDashedOptions += QStringLiteral(" <%1>").arg(option.valueName());
|
||||
}
|
||||
// check the length of the arguments
|
||||
for (auto const &arg: arguments) {
|
||||
if(arg.name().length() > size)
|
||||
size = arg.name().length();
|
||||
if (joinedDashedOptions.length() > size) {
|
||||
size = joinedDashedOptions.length();
|
||||
}
|
||||
// generate the text
|
||||
QString result;
|
||||
if(!dashedOptionList.isEmpty()) {
|
||||
result += QLatin1String("Options:\n");
|
||||
QString linePadding = QStringLiteral(" ").repeated(size + 4).prepend("\n");
|
||||
for (int i = 0; i < options.length(); ++i) {
|
||||
result += QStringLiteral(" %1 %2\n")
|
||||
.arg(dashedOptionList.at(i).leftJustified(size, ' '))
|
||||
.arg(options.at(i).description()
|
||||
.replace(QLatin1String("\n"), linePadding));
|
||||
}
|
||||
if (!arguments.isEmpty()) {
|
||||
result += QLatin1String("\n");
|
||||
}
|
||||
dashedOptionList << joinedDashedOptions;
|
||||
}
|
||||
// check the length of the arguments
|
||||
for (auto const& arg : arguments) {
|
||||
if (arg.name().length() > size)
|
||||
size = arg.name().length();
|
||||
}
|
||||
// generate the text
|
||||
QString result;
|
||||
if (!dashedOptionList.isEmpty()) {
|
||||
result += QLatin1String("Options:\n");
|
||||
QString linePadding = QStringLiteral(" ").repeated(size + 4).prepend("\n");
|
||||
for (int i = 0; i < options.length(); ++i) {
|
||||
result += QStringLiteral(" %1 %2\n")
|
||||
.arg(dashedOptionList.at(i).leftJustified(size, ' '))
|
||||
.arg(options.at(i).description().replace(QLatin1String("\n"),
|
||||
linePadding));
|
||||
}
|
||||
if (!arguments.isEmpty()) {
|
||||
result += QLatin1String("Arguments:\n");
|
||||
result += QLatin1String("\n");
|
||||
}
|
||||
for (int i = 0; i < arguments.length(); ++i) {
|
||||
result += QStringLiteral(" %1 %2\n")
|
||||
}
|
||||
if (!arguments.isEmpty()) {
|
||||
result += QLatin1String("Arguments:\n");
|
||||
}
|
||||
for (int i = 0; i < arguments.length(); ++i) {
|
||||
result += QStringLiteral(" %1 %2\n")
|
||||
.arg(arguments.at(i).name().leftJustified(size, ' '))
|
||||
.arg(arguments.at(i).description());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
} // unnamed namespace
|
||||
|
||||
bool CommandLineParser::processArgs(const QStringList &args,
|
||||
QStringList::const_iterator &actualIt,
|
||||
Node * &actualNode)
|
||||
bool
|
||||
CommandLineParser::processArgs(const QStringList& args,
|
||||
QStringList::const_iterator& actualIt,
|
||||
Node*& actualNode)
|
||||
{
|
||||
QString argument = *actualIt;
|
||||
bool ok = true;
|
||||
bool isValidArg = false;
|
||||
for (Node &n: actualNode->subNodes) {
|
||||
if (n.argument.name() == argument) {
|
||||
actualNode = &n;
|
||||
isValidArg = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (isValidArg) {
|
||||
auto nextArg = actualNode->argument;
|
||||
m_foundArgs.append(nextArg);
|
||||
// check next is help
|
||||
++actualIt;
|
||||
ok = processIfOptionIsHelp(args, actualIt, actualNode);
|
||||
--actualIt;
|
||||
} else {
|
||||
ok = false;
|
||||
out << QStringLiteral("'%1' is not a valid argument.").arg(argument);
|
||||
QString argument = *actualIt;
|
||||
bool ok = true;
|
||||
bool isValidArg = false;
|
||||
for (Node& n : actualNode->subNodes) {
|
||||
if (n.argument.name() == argument) {
|
||||
actualNode = &n;
|
||||
isValidArg = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (isValidArg) {
|
||||
auto nextArg = actualNode->argument;
|
||||
m_foundArgs.append(nextArg);
|
||||
// check next is help
|
||||
++actualIt;
|
||||
ok = processIfOptionIsHelp(args, actualIt, actualNode);
|
||||
--actualIt;
|
||||
} else {
|
||||
ok = false;
|
||||
out << QStringLiteral("'%1' is not a valid argument.").arg(argument);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool
|
||||
CommandLineParser::processOptions(const QStringList& args,
|
||||
QStringList::const_iterator& actualIt,
|
||||
Node* const actualNode)
|
||||
{
|
||||
QString arg = *actualIt;
|
||||
bool ok = true;
|
||||
// track values
|
||||
int equalsPos = arg.indexOf(QLatin1String("="));
|
||||
QString valueStr;
|
||||
if (equalsPos != -1) {
|
||||
valueStr = arg.mid(equalsPos + 1); // right
|
||||
arg = arg.mid(0, equalsPos); // left
|
||||
}
|
||||
// check format -x --xx...
|
||||
bool isDoubleDashed = arg.startsWith(QLatin1String("--"));
|
||||
ok = isDoubleDashed ? arg.length() > 3 : arg.length() == 2;
|
||||
if (!ok) {
|
||||
out << QStringLiteral("the option %1 has a wrong format.").arg(arg);
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool CommandLineParser::processOptions(const QStringList &args,
|
||||
QStringList::const_iterator &actualIt,
|
||||
Node *const actualNode)
|
||||
{
|
||||
QString arg = *actualIt;
|
||||
bool ok = true;
|
||||
// track values
|
||||
int equalsPos = arg.indexOf(QLatin1String("="));
|
||||
QString valueStr;
|
||||
if (equalsPos != -1) {
|
||||
valueStr = arg.mid(equalsPos +1); // right
|
||||
arg = arg.mid(0, equalsPos); // left
|
||||
}
|
||||
arg = isDoubleDashed ? arg.remove(0, 2) : arg.remove(0, 1);
|
||||
// get option
|
||||
auto endIt = actualNode->options.cend();
|
||||
auto optionIt = endIt;
|
||||
for (auto i = actualNode->options.cbegin(); i != endIt; ++i) {
|
||||
if ((*i).names().contains(arg)) {
|
||||
optionIt = i;
|
||||
break;
|
||||
}
|
||||
// check format -x --xx...
|
||||
bool isDoubleDashed = arg.startsWith(QLatin1String("--"));
|
||||
ok = isDoubleDashed ? arg.length() > 3 :
|
||||
arg.length() == 2;
|
||||
if (!ok) {
|
||||
out << QStringLiteral("the option %1 has a wrong format.").arg(arg);
|
||||
return ok;
|
||||
}
|
||||
arg = isDoubleDashed ?
|
||||
arg.remove(0, 2) :
|
||||
arg.remove(0, 1);
|
||||
// get option
|
||||
auto endIt = actualNode->options.cend();
|
||||
auto optionIt = endIt;
|
||||
for (auto i = actualNode->options.cbegin(); i != endIt; ++i) {
|
||||
if ((*i).names().contains(arg)) {
|
||||
optionIt = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (optionIt == endIt) {
|
||||
QString argName = actualNode->argument.name();
|
||||
if (argName.isEmpty()) {
|
||||
argName = qApp->applicationName();
|
||||
}
|
||||
out << QStringLiteral("the option '%1' is not a valid option "
|
||||
"for the argument '%2'.").arg(arg)
|
||||
.arg(argName);
|
||||
ok = false;
|
||||
return ok;
|
||||
}
|
||||
// check presence of values
|
||||
CommandOption option = *optionIt;
|
||||
bool requiresValue = !(option.valueName().isEmpty());
|
||||
if (!requiresValue && equalsPos != -1) {
|
||||
out << QStringLiteral("the option '%1' contains a '=' and it doesn't "
|
||||
"require a value.").arg(arg);
|
||||
ok = false;
|
||||
return ok;
|
||||
} else if (requiresValue && valueStr.isEmpty()) {
|
||||
// find in the next
|
||||
if (actualIt+1 != args.cend()) {
|
||||
++actualIt;
|
||||
} else {
|
||||
out << QStringLiteral("Expected value after the option '%1'.").arg(arg);
|
||||
ok = false;
|
||||
return ok;
|
||||
}
|
||||
valueStr = *actualIt;
|
||||
}
|
||||
// check the value correctness
|
||||
if (requiresValue) {
|
||||
ok = option.checkValue(valueStr);
|
||||
if (!ok) {
|
||||
QString err = option.errorMsg();
|
||||
if (!err.endsWith(QLatin1String(".")))
|
||||
err += QLatin1String(".");
|
||||
out << err;
|
||||
return ok;
|
||||
}
|
||||
option.setValue(valueStr);
|
||||
}
|
||||
m_foundOptions.append(option);
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool CommandLineParser::parse(const QStringList &args) {
|
||||
m_foundArgs.clear();
|
||||
m_foundOptions.clear();
|
||||
bool ok = true;
|
||||
Node *actualNode = &m_parseTree;
|
||||
auto it = ++args.cbegin();
|
||||
// check version option
|
||||
QStringList dashedVersion = versionOption.dashedNames();
|
||||
if (m_withVersion && args.length() > 1 &&
|
||||
dashedVersion.contains(args.at(1)))
|
||||
{
|
||||
if (args.length() == 2) {
|
||||
printVersion();
|
||||
m_foundOptions << versionOption;
|
||||
} else {
|
||||
out << "Invalid arguments after the version option.";
|
||||
ok = false;
|
||||
}
|
||||
return ok;
|
||||
|
||||
}
|
||||
// check help option
|
||||
ok = processIfOptionIsHelp(args, it, actualNode);
|
||||
// process the other args
|
||||
for (; it != args.cend() && ok; ++it) {
|
||||
const QString &value = *it;
|
||||
if (value.startsWith(QLatin1String("-"))) {
|
||||
ok = processOptions(args, it, actualNode);
|
||||
|
||||
} else {
|
||||
ok = processArgs(args, it, actualNode);
|
||||
}
|
||||
}
|
||||
if (!ok && !m_generalErrorMessage.isEmpty()) {
|
||||
out << QStringLiteral(" %1\n").arg(m_generalErrorMessage);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
CommandOption CommandLineParser::addVersionOption() {
|
||||
m_withVersion = true;
|
||||
return versionOption;
|
||||
}
|
||||
|
||||
CommandOption CommandLineParser::addHelpOption() {
|
||||
m_withHelp = true;
|
||||
return helpOption;
|
||||
}
|
||||
|
||||
bool CommandLineParser::AddArgument(const CommandArgument &arg,
|
||||
const CommandArgument &parent)
|
||||
{
|
||||
bool res = true;
|
||||
Node *n = findParent(parent);
|
||||
if (n == nullptr) {
|
||||
res = false;
|
||||
} else {
|
||||
Node child;
|
||||
child.argument = arg;
|
||||
n->subNodes.append(child);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
bool CommandLineParser::AddOption(const CommandOption &option,
|
||||
const CommandArgument &parent)
|
||||
{
|
||||
bool res = true;
|
||||
Node *n = findParent(parent);
|
||||
if (n == nullptr) {
|
||||
res = false;
|
||||
} else {
|
||||
n->options.append(option);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
bool CommandLineParser::AddOptions(const QList<CommandOption> &options,
|
||||
const CommandArgument &parent)
|
||||
{
|
||||
bool res = true;
|
||||
for (auto const &option: options) {
|
||||
if (!AddOption(option, parent)) {
|
||||
res = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
void CommandLineParser::setGeneralErrorMessage(const QString &msg) {
|
||||
m_generalErrorMessage = msg;
|
||||
}
|
||||
|
||||
void CommandLineParser::setDescription(const QString &description) {
|
||||
m_description = description;
|
||||
}
|
||||
|
||||
bool CommandLineParser::isSet(const CommandArgument &arg) const {
|
||||
return m_foundArgs.contains(arg);
|
||||
}
|
||||
|
||||
|
||||
bool CommandLineParser::isSet(const CommandOption &option) const {
|
||||
return m_foundOptions.contains(option);
|
||||
}
|
||||
|
||||
QString CommandLineParser::value(const CommandOption &option) const {
|
||||
QString value = option.value();
|
||||
for (const CommandOption &fOption: m_foundOptions) {
|
||||
if (option == fOption) {
|
||||
value = fOption.value();
|
||||
break;
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
void CommandLineParser::printVersion() {
|
||||
out << "Flameshot " << qApp->applicationVersion() << "\nCompiled with Qt "
|
||||
<< static_cast<QString>(QT_VERSION_STR) << "\n";
|
||||
}
|
||||
|
||||
void CommandLineParser::printHelp(QStringList args, const Node *node) {
|
||||
args.removeLast(); // remove the help, it's always the last
|
||||
QString helpText;
|
||||
// add usage info
|
||||
QString argName = node->argument.name();
|
||||
}
|
||||
if (optionIt == endIt) {
|
||||
QString argName = actualNode->argument.name();
|
||||
if (argName.isEmpty()) {
|
||||
argName = qApp->applicationName();
|
||||
argName = qApp->applicationName();
|
||||
}
|
||||
QString argText = node->subNodes.isEmpty() ? "" : "[arguments]";
|
||||
helpText += QStringLiteral("Usage: %1 [%2-options] %3\n\n")
|
||||
.arg(args.join(QStringLiteral(" ")))
|
||||
.arg(argName).arg(argText);
|
||||
// add command options and subarguments
|
||||
QList<CommandArgument> subArgs;
|
||||
for (const Node &n: node->subNodes)
|
||||
subArgs.append(n.argument);
|
||||
auto modifiedOptions = node->options;
|
||||
if (m_withHelp)
|
||||
modifiedOptions << helpOption;
|
||||
if (m_withVersion && node == &m_parseTree) {
|
||||
modifiedOptions << versionOption;
|
||||
}
|
||||
helpText += optionsToString(modifiedOptions, subArgs);
|
||||
// print it
|
||||
out << helpText;
|
||||
}
|
||||
|
||||
CommandLineParser::Node* CommandLineParser::findParent(
|
||||
const CommandArgument &parent)
|
||||
{
|
||||
if (parent == CommandArgument()) {
|
||||
return &m_parseTree;
|
||||
}
|
||||
//find the parent in the subNodes recursively
|
||||
Node *res = nullptr;
|
||||
for (auto i = m_parseTree.subNodes.begin();
|
||||
i != m_parseTree.subNodes.end(); ++i)
|
||||
{
|
||||
res = recursiveParentSearch(parent, *i);
|
||||
if (res != nullptr) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
CommandLineParser::Node* CommandLineParser::recursiveParentSearch(
|
||||
const CommandArgument &parent, Node &node) const
|
||||
{
|
||||
Node * res = nullptr;
|
||||
if (node.argument == parent) {
|
||||
res = &node;
|
||||
out << QStringLiteral("the option '%1' is not a valid option "
|
||||
"for the argument '%2'.")
|
||||
.arg(arg)
|
||||
.arg(argName);
|
||||
ok = false;
|
||||
return ok;
|
||||
}
|
||||
// check presence of values
|
||||
CommandOption option = *optionIt;
|
||||
bool requiresValue = !(option.valueName().isEmpty());
|
||||
if (!requiresValue && equalsPos != -1) {
|
||||
out << QStringLiteral("the option '%1' contains a '=' and it doesn't "
|
||||
"require a value.")
|
||||
.arg(arg);
|
||||
ok = false;
|
||||
return ok;
|
||||
} else if (requiresValue && valueStr.isEmpty()) {
|
||||
// find in the next
|
||||
if (actualIt + 1 != args.cend()) {
|
||||
++actualIt;
|
||||
} else {
|
||||
for (auto i = node.subNodes.begin(); i != node.subNodes.end(); ++i){
|
||||
res = recursiveParentSearch(parent, *i);
|
||||
if (res != nullptr) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
out << QStringLiteral("Expected value after the option '%1'.").arg(arg);
|
||||
ok = false;
|
||||
return ok;
|
||||
}
|
||||
return res;
|
||||
valueStr = *actualIt;
|
||||
}
|
||||
// check the value correctness
|
||||
if (requiresValue) {
|
||||
ok = option.checkValue(valueStr);
|
||||
if (!ok) {
|
||||
QString err = option.errorMsg();
|
||||
if (!err.endsWith(QLatin1String(".")))
|
||||
err += QLatin1String(".");
|
||||
out << err;
|
||||
return ok;
|
||||
}
|
||||
option.setValue(valueStr);
|
||||
}
|
||||
m_foundOptions.append(option);
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool CommandLineParser::processIfOptionIsHelp(
|
||||
const QStringList &args,
|
||||
QStringList::const_iterator &actualIt,
|
||||
Node * &actualNode)
|
||||
bool
|
||||
CommandLineParser::parse(const QStringList& args)
|
||||
{
|
||||
bool ok = true;
|
||||
auto dashedHelpNames = helpOption.dashedNames();
|
||||
if (m_withHelp && actualIt != args.cend() &&
|
||||
dashedHelpNames.contains(*actualIt))
|
||||
{
|
||||
if (actualIt+1 == args.cend()) {
|
||||
m_foundOptions << helpOption;
|
||||
printHelp(args, actualNode);
|
||||
actualIt++;
|
||||
} else {
|
||||
out << "Invalid arguments after the help option.";
|
||||
ok = false;
|
||||
}
|
||||
m_foundArgs.clear();
|
||||
m_foundOptions.clear();
|
||||
bool ok = true;
|
||||
Node* actualNode = &m_parseTree;
|
||||
auto it = ++args.cbegin();
|
||||
// check version option
|
||||
QStringList dashedVersion = versionOption.dashedNames();
|
||||
if (m_withVersion && args.length() > 1 &&
|
||||
dashedVersion.contains(args.at(1))) {
|
||||
if (args.length() == 2) {
|
||||
printVersion();
|
||||
m_foundOptions << versionOption;
|
||||
} else {
|
||||
out << "Invalid arguments after the version option.";
|
||||
ok = false;
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
// check help option
|
||||
ok = processIfOptionIsHelp(args, it, actualNode);
|
||||
// process the other args
|
||||
for (; it != args.cend() && ok; ++it) {
|
||||
const QString& value = *it;
|
||||
if (value.startsWith(QLatin1String("-"))) {
|
||||
ok = processOptions(args, it, actualNode);
|
||||
|
||||
} else {
|
||||
ok = processArgs(args, it, actualNode);
|
||||
}
|
||||
}
|
||||
if (!ok && !m_generalErrorMessage.isEmpty()) {
|
||||
out << QStringLiteral(" %1\n").arg(m_generalErrorMessage);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
CommandOption
|
||||
CommandLineParser::addVersionOption()
|
||||
{
|
||||
m_withVersion = true;
|
||||
return versionOption;
|
||||
}
|
||||
|
||||
CommandOption
|
||||
CommandLineParser::addHelpOption()
|
||||
{
|
||||
m_withHelp = true;
|
||||
return helpOption;
|
||||
}
|
||||
|
||||
bool
|
||||
CommandLineParser::AddArgument(const CommandArgument& arg,
|
||||
const CommandArgument& parent)
|
||||
{
|
||||
bool res = true;
|
||||
Node* n = findParent(parent);
|
||||
if (n == nullptr) {
|
||||
res = false;
|
||||
} else {
|
||||
Node child;
|
||||
child.argument = arg;
|
||||
n->subNodes.append(child);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
bool
|
||||
CommandLineParser::AddOption(const CommandOption& option,
|
||||
const CommandArgument& parent)
|
||||
{
|
||||
bool res = true;
|
||||
Node* n = findParent(parent);
|
||||
if (n == nullptr) {
|
||||
res = false;
|
||||
} else {
|
||||
n->options.append(option);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
bool
|
||||
CommandLineParser::AddOptions(const QList<CommandOption>& options,
|
||||
const CommandArgument& parent)
|
||||
{
|
||||
bool res = true;
|
||||
for (auto const& option : options) {
|
||||
if (!AddOption(option, parent)) {
|
||||
res = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
void
|
||||
CommandLineParser::setGeneralErrorMessage(const QString& msg)
|
||||
{
|
||||
m_generalErrorMessage = msg;
|
||||
}
|
||||
|
||||
void
|
||||
CommandLineParser::setDescription(const QString& description)
|
||||
{
|
||||
m_description = description;
|
||||
}
|
||||
|
||||
bool
|
||||
CommandLineParser::isSet(const CommandArgument& arg) const
|
||||
{
|
||||
return m_foundArgs.contains(arg);
|
||||
}
|
||||
|
||||
bool
|
||||
CommandLineParser::isSet(const CommandOption& option) const
|
||||
{
|
||||
return m_foundOptions.contains(option);
|
||||
}
|
||||
|
||||
QString
|
||||
CommandLineParser::value(const CommandOption& option) const
|
||||
{
|
||||
QString value = option.value();
|
||||
for (const CommandOption& fOption : m_foundOptions) {
|
||||
if (option == fOption) {
|
||||
value = fOption.value();
|
||||
break;
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
void
|
||||
CommandLineParser::printVersion()
|
||||
{
|
||||
out << "Flameshot " << qApp->applicationVersion() << "\nCompiled with Qt "
|
||||
<< static_cast<QString>(QT_VERSION_STR) << "\n";
|
||||
}
|
||||
|
||||
void
|
||||
CommandLineParser::printHelp(QStringList args, const Node* node)
|
||||
{
|
||||
args.removeLast(); // remove the help, it's always the last
|
||||
QString helpText;
|
||||
// add usage info
|
||||
QString argName = node->argument.name();
|
||||
if (argName.isEmpty()) {
|
||||
argName = qApp->applicationName();
|
||||
}
|
||||
QString argText = node->subNodes.isEmpty() ? "" : "[arguments]";
|
||||
helpText += QStringLiteral("Usage: %1 [%2-options] %3\n\n")
|
||||
.arg(args.join(QStringLiteral(" ")))
|
||||
.arg(argName)
|
||||
.arg(argText);
|
||||
// add command options and subarguments
|
||||
QList<CommandArgument> subArgs;
|
||||
for (const Node& n : node->subNodes)
|
||||
subArgs.append(n.argument);
|
||||
auto modifiedOptions = node->options;
|
||||
if (m_withHelp)
|
||||
modifiedOptions << helpOption;
|
||||
if (m_withVersion && node == &m_parseTree) {
|
||||
modifiedOptions << versionOption;
|
||||
}
|
||||
helpText += optionsToString(modifiedOptions, subArgs);
|
||||
// print it
|
||||
out << helpText;
|
||||
}
|
||||
|
||||
CommandLineParser::Node*
|
||||
CommandLineParser::findParent(const CommandArgument& parent)
|
||||
{
|
||||
if (parent == CommandArgument()) {
|
||||
return &m_parseTree;
|
||||
}
|
||||
// find the parent in the subNodes recursively
|
||||
Node* res = nullptr;
|
||||
for (auto i = m_parseTree.subNodes.begin(); i != m_parseTree.subNodes.end();
|
||||
++i) {
|
||||
res = recursiveParentSearch(parent, *i);
|
||||
if (res != nullptr) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
CommandLineParser::Node*
|
||||
CommandLineParser::recursiveParentSearch(const CommandArgument& parent,
|
||||
Node& node) const
|
||||
{
|
||||
Node* res = nullptr;
|
||||
if (node.argument == parent) {
|
||||
res = &node;
|
||||
} else {
|
||||
for (auto i = node.subNodes.begin(); i != node.subNodes.end(); ++i) {
|
||||
res = recursiveParentSearch(parent, *i);
|
||||
if (res != nullptr) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
bool
|
||||
CommandLineParser::processIfOptionIsHelp(const QStringList& args,
|
||||
QStringList::const_iterator& actualIt,
|
||||
Node*& actualNode)
|
||||
{
|
||||
bool ok = true;
|
||||
auto dashedHelpNames = helpOption.dashedNames();
|
||||
if (m_withHelp && actualIt != args.cend() &&
|
||||
dashedHelpNames.contains(*actualIt)) {
|
||||
if (actualIt + 1 == args.cend()) {
|
||||
m_foundOptions << helpOption;
|
||||
printHelp(args, actualNode);
|
||||
actualIt++;
|
||||
} else {
|
||||
out << "Invalid arguments after the help option.";
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
@@ -21,69 +21,72 @@
|
||||
#include "src/cli/commandoption.h"
|
||||
#include <QMap>
|
||||
|
||||
class CommandLineParser {
|
||||
class CommandLineParser
|
||||
{
|
||||
public:
|
||||
CommandLineParser();
|
||||
CommandLineParser();
|
||||
|
||||
bool parse(const QStringList &args);
|
||||
bool parse(const QStringList& args);
|
||||
|
||||
CommandArgument rootArgument() const { return CommandArgument(); }
|
||||
CommandArgument rootArgument() const { return CommandArgument(); }
|
||||
|
||||
CommandOption addVersionOption();
|
||||
CommandOption addHelpOption();
|
||||
CommandOption addVersionOption();
|
||||
CommandOption addHelpOption();
|
||||
|
||||
bool AddArgument(const CommandArgument &arg,
|
||||
const CommandArgument &parent = CommandArgument());
|
||||
bool AddArgument(const CommandArgument& arg,
|
||||
const CommandArgument& parent = CommandArgument());
|
||||
|
||||
bool AddOption(const CommandOption &option,
|
||||
const CommandArgument &parent = CommandArgument());
|
||||
bool AddOption(const CommandOption& option,
|
||||
const CommandArgument& parent = CommandArgument());
|
||||
|
||||
bool AddOptions(const QList<CommandOption> &options,
|
||||
const CommandArgument &parent = CommandArgument());
|
||||
bool AddOptions(const QList<CommandOption>& options,
|
||||
const CommandArgument& parent = CommandArgument());
|
||||
|
||||
void setGeneralErrorMessage(const QString &msg);
|
||||
void setDescription(const QString &description);
|
||||
void setGeneralErrorMessage(const QString& msg);
|
||||
void setDescription(const QString& description);
|
||||
|
||||
bool isSet(const CommandArgument &arg) const;
|
||||
bool isSet(const CommandOption &option) const;
|
||||
QString value(const CommandOption &option) const;
|
||||
bool isSet(const CommandArgument& arg) const;
|
||||
bool isSet(const CommandOption& option) const;
|
||||
QString value(const CommandOption& option) const;
|
||||
|
||||
private:
|
||||
bool m_withHelp = false;
|
||||
bool m_withVersion = false;
|
||||
QString m_description;
|
||||
QString m_generalErrorMessage;
|
||||
bool m_withHelp = false;
|
||||
bool m_withVersion = false;
|
||||
QString m_description;
|
||||
QString m_generalErrorMessage;
|
||||
|
||||
struct Node {
|
||||
explicit Node(const CommandArgument &arg) : argument(arg) {}
|
||||
Node() {}
|
||||
bool operator==(const Node &n) const {
|
||||
return argument == n.argument &&
|
||||
options == n.options &&
|
||||
subNodes == n.subNodes;
|
||||
}
|
||||
CommandArgument argument;
|
||||
QList<CommandOption> options;
|
||||
QList<Node> subNodes;
|
||||
};
|
||||
struct Node
|
||||
{
|
||||
explicit Node(const CommandArgument& arg)
|
||||
: argument(arg)
|
||||
{}
|
||||
Node() {}
|
||||
bool operator==(const Node& n) const
|
||||
{
|
||||
return argument == n.argument && options == n.options &&
|
||||
subNodes == n.subNodes;
|
||||
}
|
||||
CommandArgument argument;
|
||||
QList<CommandOption> options;
|
||||
QList<Node> subNodes;
|
||||
};
|
||||
|
||||
Node m_parseTree;
|
||||
QList<CommandOption> m_foundOptions;
|
||||
QList<CommandArgument> m_foundArgs;
|
||||
Node m_parseTree;
|
||||
QList<CommandOption> m_foundOptions;
|
||||
QList<CommandArgument> m_foundArgs;
|
||||
|
||||
// helper functions
|
||||
void printVersion();
|
||||
void printHelp(QStringList args, const Node *node);
|
||||
Node* findParent(const CommandArgument &parent);
|
||||
Node* recursiveParentSearch(const CommandArgument &parent,
|
||||
Node &node) const;
|
||||
bool processIfOptionIsHelp(const QStringList &args,
|
||||
QStringList::const_iterator &actualIt,
|
||||
Node * &actualNode);
|
||||
bool processArgs(const QStringList &args,
|
||||
QStringList::const_iterator &actualIt,
|
||||
Node * &actualNode);
|
||||
bool processOptions(const QStringList &args,
|
||||
QStringList::const_iterator &actualIt,
|
||||
Node *const actualNode);
|
||||
// helper functions
|
||||
void printVersion();
|
||||
void printHelp(QStringList args, const Node* node);
|
||||
Node* findParent(const CommandArgument& parent);
|
||||
Node* recursiveParentSearch(const CommandArgument& parent, Node& node) const;
|
||||
bool processIfOptionIsHelp(const QStringList& args,
|
||||
QStringList::const_iterator& actualIt,
|
||||
Node*& actualNode);
|
||||
bool processArgs(const QStringList& args,
|
||||
QStringList::const_iterator& actualIt,
|
||||
Node*& actualNode);
|
||||
bool processOptions(const QStringList& args,
|
||||
QStringList::const_iterator& actualIt,
|
||||
Node* const actualNode);
|
||||
};
|
||||
|
||||
@@ -17,96 +17,124 @@
|
||||
|
||||
#include "commandoption.h"
|
||||
|
||||
CommandOption::CommandOption(const QString &name, const QString &description,
|
||||
const QString &valueName,
|
||||
const QString &defaultValue) :
|
||||
m_names(name), m_description(description), m_valueName(valueName),
|
||||
m_value(defaultValue)
|
||||
CommandOption::CommandOption(const QString& name,
|
||||
const QString& description,
|
||||
const QString& valueName,
|
||||
const QString& defaultValue)
|
||||
: m_names(name)
|
||||
, m_description(description)
|
||||
, m_valueName(valueName)
|
||||
, m_value(defaultValue)
|
||||
{
|
||||
m_checker = [](QString const&){ return true; };
|
||||
m_checker = [](QString const&) { return true; };
|
||||
}
|
||||
|
||||
CommandOption::CommandOption(const QStringList &names,
|
||||
const QString &description,
|
||||
const QString &valueName,
|
||||
const QString &defaultValue) :
|
||||
m_names(names), m_description(description), m_valueName(valueName),
|
||||
m_value(defaultValue)
|
||||
CommandOption::CommandOption(const QStringList& names,
|
||||
const QString& description,
|
||||
const QString& valueName,
|
||||
const QString& defaultValue)
|
||||
: m_names(names)
|
||||
, m_description(description)
|
||||
, m_valueName(valueName)
|
||||
, m_value(defaultValue)
|
||||
{
|
||||
m_checker = [](QString const&) -> bool { return true; };
|
||||
m_checker = [](QString const&) -> bool { return true; };
|
||||
}
|
||||
|
||||
void CommandOption::setName(const QString &name) {
|
||||
m_names = QStringList() << name;
|
||||
}
|
||||
|
||||
void CommandOption::setNames(const QStringList &names) {
|
||||
m_names = names;
|
||||
}
|
||||
|
||||
QStringList CommandOption::names() const {
|
||||
return m_names;
|
||||
}
|
||||
|
||||
QStringList CommandOption::dashedNames() const {
|
||||
QStringList dashedNames;
|
||||
for (const QString &name: m_names) {
|
||||
// prepend "-" to single character options, and "--" to the others
|
||||
QString dashedName = (name.length() == 1) ?
|
||||
QStringLiteral("-%1").arg(name) :
|
||||
QStringLiteral("--%1").arg(name);
|
||||
dashedNames << dashedName;
|
||||
}
|
||||
return dashedNames;
|
||||
}
|
||||
|
||||
void CommandOption::setValueName(const QString &name) {
|
||||
m_valueName = name;
|
||||
}
|
||||
|
||||
QString CommandOption::valueName() const {
|
||||
return m_valueName;
|
||||
}
|
||||
|
||||
void CommandOption::setValue(const QString &value) {
|
||||
if (m_valueName.isEmpty()) {
|
||||
m_valueName = QLatin1String("value");
|
||||
}
|
||||
m_value = value;
|
||||
}
|
||||
|
||||
QString CommandOption::value() const {
|
||||
return m_value;
|
||||
}
|
||||
|
||||
void CommandOption::addChecker(const function<bool (const QString &)> checker,
|
||||
const QString &errMsg)
|
||||
void
|
||||
CommandOption::setName(const QString& name)
|
||||
{
|
||||
m_checker = checker;
|
||||
m_errorMsg = errMsg;
|
||||
m_names = QStringList() << name;
|
||||
}
|
||||
|
||||
bool CommandOption::checkValue(const QString &value) const {
|
||||
return m_checker(value);
|
||||
}
|
||||
|
||||
QString CommandOption::description() const
|
||||
void
|
||||
CommandOption::setNames(const QStringList& names)
|
||||
{
|
||||
return m_description;
|
||||
m_names = names;
|
||||
}
|
||||
|
||||
void CommandOption::setDescription(const QString &description)
|
||||
QStringList
|
||||
CommandOption::names() const
|
||||
{
|
||||
m_description = description;
|
||||
return m_names;
|
||||
}
|
||||
|
||||
QString CommandOption::errorMsg() const {
|
||||
return m_errorMsg;
|
||||
}
|
||||
|
||||
bool CommandOption::operator ==(const CommandOption &option) const
|
||||
QStringList
|
||||
CommandOption::dashedNames() const
|
||||
{
|
||||
return m_description == option.m_description
|
||||
&& m_names == option.m_names
|
||||
&& m_valueName == option.m_valueName;
|
||||
QStringList dashedNames;
|
||||
for (const QString& name : m_names) {
|
||||
// prepend "-" to single character options, and "--" to the others
|
||||
QString dashedName = (name.length() == 1)
|
||||
? QStringLiteral("-%1").arg(name)
|
||||
: QStringLiteral("--%1").arg(name);
|
||||
dashedNames << dashedName;
|
||||
}
|
||||
return dashedNames;
|
||||
}
|
||||
|
||||
void
|
||||
CommandOption::setValueName(const QString& name)
|
||||
{
|
||||
m_valueName = name;
|
||||
}
|
||||
|
||||
QString
|
||||
CommandOption::valueName() const
|
||||
{
|
||||
return m_valueName;
|
||||
}
|
||||
|
||||
void
|
||||
CommandOption::setValue(const QString& value)
|
||||
{
|
||||
if (m_valueName.isEmpty()) {
|
||||
m_valueName = QLatin1String("value");
|
||||
}
|
||||
m_value = value;
|
||||
}
|
||||
|
||||
QString
|
||||
CommandOption::value() const
|
||||
{
|
||||
return m_value;
|
||||
}
|
||||
|
||||
void
|
||||
CommandOption::addChecker(const function<bool(const QString&)> checker,
|
||||
const QString& errMsg)
|
||||
{
|
||||
m_checker = checker;
|
||||
m_errorMsg = errMsg;
|
||||
}
|
||||
|
||||
bool
|
||||
CommandOption::checkValue(const QString& value) const
|
||||
{
|
||||
return m_checker(value);
|
||||
}
|
||||
|
||||
QString
|
||||
CommandOption::description() const
|
||||
{
|
||||
return m_description;
|
||||
}
|
||||
|
||||
void
|
||||
CommandOption::setDescription(const QString& description)
|
||||
{
|
||||
m_description = description;
|
||||
}
|
||||
|
||||
QString
|
||||
CommandOption::errorMsg() const
|
||||
{
|
||||
return m_errorMsg;
|
||||
}
|
||||
|
||||
bool
|
||||
CommandOption::operator==(const CommandOption& option) const
|
||||
{
|
||||
return m_description == option.m_description && m_names == option.m_names &&
|
||||
m_valueName == option.m_valueName;
|
||||
}
|
||||
|
||||
@@ -22,43 +22,47 @@
|
||||
|
||||
using std::function;
|
||||
|
||||
class CommandOption {
|
||||
class CommandOption
|
||||
{
|
||||
public:
|
||||
CommandOption(const QString &name, const QString &description,
|
||||
const QString &valueName = QString(),
|
||||
const QString &defaultValue = QString());
|
||||
CommandOption(const QString& name,
|
||||
const QString& description,
|
||||
const QString& valueName = QString(),
|
||||
const QString& defaultValue = QString());
|
||||
|
||||
CommandOption(const QStringList &names, const QString &description,
|
||||
const QString &valueName = QString(),
|
||||
const QString &defaultValue = QString());
|
||||
CommandOption(const QStringList& names,
|
||||
const QString& description,
|
||||
const QString& valueName = QString(),
|
||||
const QString& defaultValue = QString());
|
||||
|
||||
void setName(const QString &name);
|
||||
void setNames(const QStringList &names);
|
||||
QStringList names() const;
|
||||
QStringList dashedNames() const;
|
||||
void setName(const QString& name);
|
||||
void setNames(const QStringList& names);
|
||||
QStringList names() const;
|
||||
QStringList dashedNames() const;
|
||||
|
||||
void setValueName(const QString &name);
|
||||
QString valueName() const;
|
||||
void setValueName(const QString& name);
|
||||
QString valueName() const;
|
||||
|
||||
void setValue(const QString &value);
|
||||
QString value() const;
|
||||
void setValue(const QString& value);
|
||||
QString value() const;
|
||||
|
||||
void addChecker(const function<bool(QString const&)> checker, const QString &errMsg);
|
||||
bool checkValue(const QString &value) const;
|
||||
void addChecker(const function<bool(QString const&)> checker,
|
||||
const QString& errMsg);
|
||||
bool checkValue(const QString& value) const;
|
||||
|
||||
QString description() const;
|
||||
void setDescription(const QString &description);
|
||||
QString description() const;
|
||||
void setDescription(const QString& description);
|
||||
|
||||
QString errorMsg() const;
|
||||
QString errorMsg() const;
|
||||
|
||||
bool operator==(const CommandOption &option) const;
|
||||
bool operator==(const CommandOption& option) const;
|
||||
|
||||
private:
|
||||
QStringList m_names;
|
||||
QString m_description;
|
||||
QString m_valueName;
|
||||
QString m_value;
|
||||
QStringList m_names;
|
||||
QString m_description;
|
||||
QString m_valueName;
|
||||
QString m_value;
|
||||
|
||||
function<bool(QString const&)> m_checker;
|
||||
QString m_errorMsg;
|
||||
function<bool(QString const&)> m_checker;
|
||||
QString m_errorMsg;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user