valentina/src/app/puzzle/vpuzzlecommandline.cpp

126 lines
4.3 KiB
C++
Raw Normal View History

2020-04-03 22:05:03 +02:00
#include "vpuzzlecommandline.h"
2020-04-06 23:57:01 +02:00
#include "../vmisc/commandoptions.h"
#include "../vmisc/vsysexits.h"
#include <QDebug>
#include <QTest>
2020-04-03 22:05:03 +02:00
2020-04-06 23:57:01 +02:00
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();
}
//----------------------------------------------------------------------------------------------
2020-04-03 22:05:03 +02:00
VPuzzleCommandLine::VPuzzleCommandLine():
parser(),
isGuiEnabled(false)
{
2020-04-06 23:57:01 +02:00
parser.setApplicationDescription(translate("Puzzle", "Valentina's manual layout editor."));
2020-04-03 22:05:03 +02:00
parser.addHelpOption();
parser.addVersionOption();
2020-04-06 23:57:01 +02:00
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);
2020-04-03 22:05:03 +02:00
}