From b88e23697eb3e15137315b044cdd27be876a7064 Mon Sep 17 00:00:00 2001 From: vorzelmir Date: Tue, 7 Apr 2020 00:57:01 +0300 Subject: [PATCH] puzzle command line initial options --- src/app/puzzle/puzzle.pro | 2 +- src/app/puzzle/vpuzzlecommandline.cpp | 116 +++++++++++++++++++++++++- src/app/puzzle/vpuzzlecommandline.h | 5 +- 3 files changed, 117 insertions(+), 6 deletions(-) diff --git a/src/app/puzzle/puzzle.pro b/src/app/puzzle/puzzle.pro index 7463ba832..818e299dd 100644 --- a/src/app/puzzle/puzzle.pro +++ b/src/app/puzzle/puzzle.pro @@ -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 diff --git a/src/app/puzzle/vpuzzlecommandline.cpp b/src/app/puzzle/vpuzzlecommandline.cpp index 681d04f32..1c826e341 100644 --- a/src/app/puzzle/vpuzzlecommandline.cpp +++ b/src/app/puzzle/vpuzzlecommandline.cpp @@ -1,13 +1,125 @@ #include "vpuzzlecommandline.h" +#include "../vmisc/commandoptions.h" +#include "../vmisc/vsysexits.h" +#include +#include +std::shared_ptr 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(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(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::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); } diff --git a/src/app/puzzle/vpuzzlecommandline.h b/src/app/puzzle/vpuzzlecommandline.h index a5148f666..2735e6487 100644 --- a/src/app/puzzle/vpuzzlecommandline.h +++ b/src/app/puzzle/vpuzzlecommandline.h @@ -2,8 +2,6 @@ #define VPUZZLECOMMANDLINE_H #include -#include -#include #include #include @@ -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;