Command line tool - basics

This commit is contained in:
Jakub Melka
2020-09-25 18:08:39 +02:00
parent 82058c273b
commit 6fb771265e
6 changed files with 325 additions and 1 deletions

View File

@ -22,6 +22,7 @@ SUBDIRS += \
JBIG2_Viewer \ JBIG2_Viewer \
PdfExampleGenerator \ PdfExampleGenerator \
PdfForQtLib \ PdfForQtLib \
PdfTool \
UnitTests \ UnitTests \
PdfForQtViewer PdfForQtViewer

View File

@ -30,7 +30,7 @@ int main(int argc, char *argv[])
QCoreApplication::setOrganizationName("MelkaJ"); QCoreApplication::setOrganizationName("MelkaJ");
QCoreApplication::setApplicationName("PDF Viewer"); QCoreApplication::setApplicationName("PDF Viewer");
QCoreApplication::setApplicationVersion("0.1"); QCoreApplication::setApplicationVersion("1.0.0");
QCommandLineParser parser; QCommandLineParser parser;
parser.setApplicationDescription(QCoreApplication::applicationName()); parser.setApplicationDescription(QCoreApplication::applicationName());
parser.addHelpOption(); parser.addHelpOption();

57
PdfTool/PdfTool.pro Normal file
View File

@ -0,0 +1,57 @@
# Copyright (C) 2020 Jakub Melka
#
# This file is part of PdfForQt.
#
# PdfForQt 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.
#
# PdfForQt 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 PDFForQt. If not, see <https://www.gnu.org/licenses/>.
QT -= gui
CONFIG += c++11 console
CONFIG -= app_bundle
TARGET = PdfTool
VERSION = 1.0.0
QMAKE_TARGET_DESCRIPTION = "PDF tool for Qt"
QMAKE_TARGET_COPYRIGHT = "(c) Jakub Melka 2018-2020"
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
QMAKE_CXXFLAGS += /std:c++latest /utf-8
INCLUDEPATH += $$PWD/../PDFForQtLib/Sources
DESTDIR = $$OUT_PWD/..
LIBS += -L$$OUT_PWD/..
LIBS += -lPDFForQtLib
SOURCES += \
main.cpp \
pdftoolabstractapplication.cpp
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
application.files = $$DESTDIR/PdfTool.exe
application.path = $$DESTDIR/install
INSTALLS += application
HEADERS += \
pdftoolabstractapplication.h

58
PdfTool/main.cpp Normal file
View File

@ -0,0 +1,58 @@
// Copyright (C) 2020 Jakub Melka
//
// This file is part of PdfForQt.
//
// PdfForQt 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.
//
// PdfForQt 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 PDFForQt. If not, see <https://www.gnu.org/licenses/>.
#include "pdftoolabstractapplication.h"
#include <QCoreApplication>
#include <QCommandLineParser>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QCoreApplication::setOrganizationName("MelkaJ");
QCoreApplication::setApplicationName("PdfTool");
QCoreApplication::setApplicationVersion("1.0.0");
QStringList arguments = QCoreApplication::arguments();
QCommandLineParser parser;
parser.setApplicationDescription("PdfTool - work with pdf documents via command line");
parser.addPositionalArgument("command", "Command to execute.");
parser.parse(arguments);
QStringList positionalArguments = parser.positionalArguments();
QString command = !positionalArguments.isEmpty() ? positionalArguments.front() : QString();
arguments.removeOne(command);
pdftool::PDFToolAbstractApplication* application = pdftool::PDFToolApplicationStorage::getApplicationByCommand(command);
if (!application)
{
application = pdftool::PDFToolApplicationStorage::getDefaultApplication();
}
else
{
parser.clearPositionalArguments();
}
application->initializeCommandLineParser(&parser);
parser.addHelpOption();
parser.addVersionOption();
parser.process(arguments);
return application->execute(application->getOptions(&parser));
}

View File

@ -0,0 +1,112 @@
// Copyright (C) 2020 Jakub Melka
//
// This file is part of PdfForQt.
//
// PdfForQt 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.
//
// PdfForQt 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 PDFForQt. If not, see <https://www.gnu.org/licenses/>.
#include "pdftoolabstractapplication.h"
namespace pdftool
{
class PDFToolHelpApplication : public PDFToolAbstractApplication
{
public:
PDFToolHelpApplication() : PDFToolAbstractApplication(true) { }
virtual QString getStandardString(StandardString standardString) const override;
virtual int execute(const PDFToolOptions& options) override;
};
static PDFToolHelpApplication s_helpApplication;
QString PDFToolHelpApplication::getStandardString(StandardString standardString) const
{
switch (standardString)
{
case Command:
return "help";
case Name:
return PDFToolTranslationContext::tr("Help");
case Description:
return PDFToolTranslationContext::tr("Show list of all available commands.");
default:
Q_ASSERT(false);
break;
}
return QString();
}
int PDFToolHelpApplication::execute(const PDFToolOptions& options)
{
return EXIT_SUCCESS;
}
PDFToolAbstractApplication::PDFToolAbstractApplication(bool isDefault)
{
PDFToolApplicationStorage::registerApplication(this, isDefault);
}
void PDFToolAbstractApplication::initializeCommandLineParser(QCommandLineParser* parser) const
{
}
PDFToolOptions PDFToolAbstractApplication::getOptions(QCommandLineParser* parser) const
{
PDFToolOptions options;
return options;
}
PDFToolAbstractApplication* PDFToolApplicationStorage::getApplicationByCommand(const QString& command)
{
for (PDFToolAbstractApplication* application : getInstance()->m_applications)
{
if (application->getStandardString(PDFToolAbstractApplication::Command) == command)
{
return application;
}
}
return nullptr;
}
void PDFToolApplicationStorage::registerApplication(PDFToolAbstractApplication* application, bool isDefault)
{
PDFToolApplicationStorage* storage = getInstance();
storage->m_applications.push_back(application);
if (isDefault)
{
storage->m_defaultApplication = application;
}
}
PDFToolAbstractApplication* PDFToolApplicationStorage::getDefaultApplication()
{
return getInstance()->m_defaultApplication;
}
PDFToolApplicationStorage* PDFToolApplicationStorage::getInstance()
{
static PDFToolApplicationStorage storage;
return &storage;
}
} // pdftool

View File

@ -0,0 +1,96 @@
// Copyright (C) 2020 Jakub Melka
//
// This file is part of PdfForQt.
//
// PdfForQt 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.
//
// PdfForQt 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 PDFForQt. If not, see <https://www.gnu.org/licenses/>.
#ifndef PDFTOOLABSTRACTAPPLICATION_H
#define PDFTOOLABSTRACTAPPLICATION_H
#include <QtGlobal>
#include <QString>
#include <QCoreApplication>
#include <vector>
class QCommandLineParser;
namespace pdftool
{
struct PDFToolTranslationContext
{
Q_DECLARE_TR_FUNCTIONS(PDFToolTranslationContext)
};
struct PDFToolOptions
{
QString document;
QString password;
};
/// Base class for all applications
class PDFToolAbstractApplication
{
public:
explicit PDFToolAbstractApplication(bool isDefault = false);
virtual ~PDFToolAbstractApplication() = default;
enum StandardString
{
Command, ///< Command, by which is this application invoked
Name, ///< Name of application
Description ///< Description (what this application does)
};
virtual QString getStandardString(StandardString standardString) const = 0;
virtual int execute(const PDFToolOptions& options) = 0;
void initializeCommandLineParser(QCommandLineParser* parser) const;
PDFToolOptions getOptions(QCommandLineParser* parser) const;
};
/// This class stores information about all applications available. Application
/// can be selected by command string. Also, default application is available.
class PDFToolApplicationStorage
{
public:
/// Returns application by command. If application for given command is not found,
/// then nullptr is returned.
/// \param command Command
/// \returns Application for given command or nullptr
static PDFToolAbstractApplication* getApplicationByCommand(const QString& command);
/// Registers application to the application storage. If \p isDefault
/// is set to true, application is registered as default application.
/// \param application Apllication
/// \param isDefault Is default application?
static void registerApplication(PDFToolAbstractApplication* application, bool isDefault = false);
/// Returns default application
/// \returns Default application
static PDFToolAbstractApplication* getDefaultApplication();
private:
PDFToolApplicationStorage() = default;
static PDFToolApplicationStorage* getInstance();
std::vector<PDFToolAbstractApplication*> m_applications;
PDFToolAbstractApplication* m_defaultApplication = nullptr;
};
} // namespace pdftool
#endif // PDFTOOLABSTRACTAPPLICATION_H