puzzle command line initial options

This commit is contained in:
vorzelmir 2020-04-07 00:57:01 +03:00
parent 186a855cbd
commit b88e23697e
3 changed files with 117 additions and 6 deletions

View file

@ -7,7 +7,7 @@
# File with common stuff for whole project
include(../../../common.pri)
QT += core gui widgets network xml xmlpatterns printsupport
QT += core gui widgets network xml xmlpatterns printsupport testlib
# Name of binary file
TARGET = puzzle

View file

@ -1,13 +1,125 @@
#include "vpuzzlecommandline.h"
#include "../vmisc/commandoptions.h"
#include "../vmisc/vsysexits.h"
#include <QDebug>
#include <QTest>
std::shared_ptr<VPuzzleCommandLine> VPuzzleCommandLine::instance = nullptr;
#define translate(context, source) QCoreApplication::translate((context), source)
//------------------------------------------------------------------------------------------------
bool VPuzzleCommandLine::IsExportEnabled() const
{
const bool result = IsOptionSet(QStringLiteral("destination"));
int argSize = parser.positionalArguments().size();
if (result && argSize != 1)
{
qCritical() << translate("Puzzle", "Export options can be used with single input file only.") << "/n";
const_cast<VPuzzleCommandLine*>(this)->parser.showHelp(V_EX_USAGE);
}
return result;
}
//----------------------------------------------------------------------------------------------
QString VPuzzleCommandLine::OptionBaseName() const
{
QString path;
if (IsExportEnabled())
{
path = OptionValue(QStringLiteral("destination"));
}
return path;
}
//--------------------------------------------------------------------------------------------
bool VPuzzleCommandLine::IsTestModeEnabled() const
{
const bool r = IsOptionSet(QStringLiteral("test"));
if (r && parser.positionalArguments().size() != 1)
{
qCritical() << translate("VCommandLine", "Test option can be used with single input file only.") << "/n";
const_cast<VPuzzleCommandLine*>(this)->parser.showHelp(V_EX_USAGE);
}
return r;
}
//--------------------------------------------------------------------------------------------
bool VPuzzleCommandLine::IsGuiEnabled() const
{
return isGuiEnabled;
}
//--------------------------------------------------------------------------------------------
QStringList VPuzzleCommandLine::OptionFileNames() const
{
return parser.positionalArguments();
}
//----------------------------------------------------------------------------------------------
VPuzzleCommandLine::VPuzzleCommandLine():
parser(),
isGuiEnabled(false)
{
parser.setApplicationDescription(tr("Valentina's manual layout editor."));
parser.setApplicationDescription(translate("Puzzle", "Valentina's manual layout editor."));
parser.addHelpOption();
parser.addVersionOption();
parser.addPositionalArgument("filename", tr("The raw layout file."));
InitCommandLineOptions();
}
//-------------------------------------------------------------------------------------------
std::shared_ptr<VPuzzleCommandLine> VPuzzleCommandLine::Instance(const QCoreApplication &app)
{
if (instance == nullptr)
{
instance.reset(new VPuzzleCommandLine);
}
instance->parser.process(app);
instance->isGuiEnabled = not (instance->IsGuiEnabled() || instance->IsExportEnabled());
return instance;
}
//-------------------------------------------------------------------------------------------
void VPuzzleCommandLine::InitCommandLineOptions()
{
if (IsExportEnabled())
{
QStringList args = parser.positionalArguments();
parser.setSingleDashWordOptionMode(
QCommandLineParser::SingleDashWordOptionMode(args.takeFirst().toInt()));
QString source = args.isEmpty() ? QString() : args.at(0);
QString destination = args.isEmpty() ? QString() : args.at(1);
parser.clearPositionalArguments();
parser.addPositionalArgument(source,
translate("Puzzle", "The raw layout input file."));
parser.addPositionalArgument(destination,
translate("Puzzle", "The destination folder"));
}
QCommandLineOption forceOption(QStringList() << "f" << "force",
translate("Puzzle", "Overwrite existing files."));
parser.addOption(forceOption);
}
//--------------------------------------------------------------------------------------------
bool VPuzzleCommandLine::IsOptionSet(const QString &option) const
{
return parser.isSet(option);
}
//-------------------------------------------------------------------------------------------
QString VPuzzleCommandLine::OptionValue(const QString &option) const
{
return parser.value(option);
}
//--------------------------------------------------------------------------------------------
QStringList VPuzzleCommandLine::OptionValues(const QString &option) const
{
return parser.values(option);
}

View file

@ -2,8 +2,6 @@
#define VPUZZLECOMMANDLINE_H
#include <memory>
#include <vector>
#include <QTextStream>
#include <QCoreApplication>
#include <QCommandLineParser>
@ -32,7 +30,7 @@ public:
/**
* @brief the file name which should be loaded
*/
QString OptionFileName() const;
QStringList OptionFileNames() const;
protected:
VPuzzleCommandLine();
/**
@ -49,6 +47,7 @@ private:
* @brief add options to the QCommandLineParser that there are in the cmd can be
*/
void InitCommandLineOptions();
bool IsOptionSet(const QString &option)const;
QString OptionValue(const QString &option) const;
QStringList OptionValues(const QString &option) const;