Skip to content

Commit

Permalink
Make qcommandlineparser a member of the qdltoptmanager
Browse files Browse the repository at this point in the history
Signed-off-by: Viktor Kopp <[email protected]>
  • Loading branch information
vifactor committed Sep 23, 2024
1 parent 46991bf commit 71d6e78
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 124 deletions.
85 changes: 58 additions & 27 deletions qdlt/qdltoptmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

#include <QDebug>
#include <QFileInfo>
#include <QCommandLineParser>

// Global static pointer used to ensure a single instance of the class.
QDltOptManager* QDltOptManager::instance;
Expand All @@ -39,6 +38,31 @@ QDltOptManager::QDltOptManager()
convertionmode = e_ASCI;
commandline_mode = false;
delimiter=',';

m_parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);

m_parser.addPositionalArgument("logfile", "Loading one or more logfiles on startup (must end with .dlt)");
m_parser.addPositionalArgument("projectfile", "Loading project file on startup (must end with .dlp)");
m_parser.addPositionalArgument("filterfile", "Loading filterfile on startup (must end with .dlf)");
m_parser.addPositionalArgument("pcapfile", "Importing DLT/IPC from pcap file on startup (must end with .pcap)");
m_parser.addPositionalArgument("mf4file", "Importing DLT/IPC from mf4 file on startup (must end with .mf4)");
m_parser.addOptions({
{"c", "Convert logfile file to <textfile>", "textfile"},
{"u", "Conversion will be done in UTF8 instead of ASCII"},
{"csv", "Conversion will be done in CSV format"},
{"d", "Conversion will NOT be done, save in dlt file format again instead"},
{"dd", "Conversion will NOT be done, save as decoded messages in dlt format"},
{"b", "Execute a plugin command with <n> parameters before loading log file.", "plugin|command|param1|..|param<n>"},
{"e", "Execute a plugin command with <n> parameters after loading log file.", "plugin|command|param1|..|param<n>"},
{QStringList() << "s" << "silent", "Enable silent mode without any GUI. Ideal for commandline usage."},
{"stream", "Treat the input logfiles as DLT stream instead of DLT files."},
{QStringList() << "t" << "terminate", "Terminate DLT Viewer after command line execution."},
{"w", "Set the working directory", "workingdirectory"},
{"delimiter", "The used delimiter for CSV export (Default: ,).", "character"},
{QStringList() << "h" << "help", "Print this help message."},
{QStringList() << "v" << "version", "Print the version."}
});

}

QDltOptManager* QDltOptManager::getInstance()
Expand Down Expand Up @@ -97,18 +121,20 @@ void QDltOptManager::printUsage(const QString& helpText)
qDebug() << " dlt-viewer.exe -t -c output.txt input1.mf4 input2.mf4";
}

void QDltOptManager::parse(const QCommandLineParser& parser, const QString& executableName)
void QDltOptManager::parse(const QStringList& args)
{
m_parser.parse(args);

qDebug() << "### Starting DLT Viewer";

printVersion(executableName);
printVersion(args.at(0));

/* the default parameter - exactly one parameter - should either be
* a dlt or a dlp file, so this enables the "doubleclick" feature
*/
if (parser.optionNames().isEmpty() && parser.positionalArguments().size() == 1)
if (m_parser.optionNames().isEmpty() && m_parser.positionalArguments().size() == 1)
{
const QString& arg = parser.positionalArguments().at(0);
const QString& arg = m_parser.positionalArguments().at(0);
if(arg.endsWith(".dlp") || arg.endsWith(".DLP"))
{
projectFile = arg;
Expand All @@ -125,72 +151,72 @@ void QDltOptManager::parse(const QCommandLineParser& parser, const QString& exec
}
}

if (parser.isSet("help")) {
printUsage(parser.helpText());
if (m_parser.isSet("help")) {
printUsage(m_parser.helpText());
exit(0);
}

if (parser.isSet("silent")) {
if (m_parser.isSet("silent")) {
silent_mode = true;
qDebug() << "Silent mode enabled";
}

if (parser.isSet("version")) {
if (m_parser.isSet("version")) {
// version is already printed above
exit(0);
}

if (parser.isSet("terminate")) {
if (m_parser.isSet("terminate")) {
terminate = true;
commandline_mode = true;
}

if (parser.isSet("c")) {
convertDestFile = parser.value("c");
if (m_parser.isSet("c")) {
convertDestFile = m_parser.value("c");
qDebug() << "Convert filename:" << convertDestFile;
commandline_mode = true;
}

if (parser.isSet("delimiter")) {
delimiter = parser.value("delimiter").front().toLatin1();
if (m_parser.isSet("delimiter")) {
delimiter = m_parser.value("delimiter").front().toLatin1();
qDebug() << "Delimiter:" << delimiter;
}

if (parser.isSet("u")) {
if (m_parser.isSet("u")) {
convertionmode = e_UTF8;
}

if (parser.isSet("csv")) {
if (m_parser.isSet("csv")) {
convertionmode = e_CSV;
}

if (parser.isSet("d")) {
if (m_parser.isSet("d")) {
convertionmode = e_DLT;
}

if (parser.isSet("dd")) {
if (m_parser.isSet("dd")) {
convertionmode = e_DDLT;
}

if (parser.isSet("stream")) {
if (m_parser.isSet("stream")) {
inputmode = e_inputmode::STREAM;
}

if (parser.isSet("e")) {
postPluginCommands += parser.value("e");
if (m_parser.isSet("e")) {
postPluginCommands += m_parser.value("e");
commandline_mode = true;
}

if (parser.isSet("b")) {
prePluginCommands += parser.value("b");
if (m_parser.isSet("b")) {
prePluginCommands += m_parser.value("b");
commandline_mode = true;
}

if (parser.isSet("w")) {
workingDirectory = parser.value("w");
if (m_parser.isSet("w")) {
workingDirectory = m_parser.value("w");
}

QStringList positionalArguments = parser.positionalArguments();
QStringList positionalArguments = m_parser.positionalArguments();
for (const QString &arg : positionalArguments)
{
if(arg.endsWith(".dlt") || arg.endsWith(".DLT"))
Expand All @@ -204,7 +230,7 @@ void QDltOptManager::parse(const QCommandLineParser& parser, const QString& exec
if (project == true)
{
qDebug() << "\nError: Can only load one project file\n";
printUsage(parser.helpText());
printUsage(m_parser.helpText());
exit(-1);
}

Expand Down Expand Up @@ -267,6 +293,11 @@ QStringList QDltOptManager::getCommandParams(){return commandParams;}
QString QDltOptManager::getWorkingDirectory() const { return workingDirectory; }
char QDltOptManager::getDelimiter(){return delimiter;}

QString QDltOptManager::getHelpText() const
{
return m_parser.helpText();
}

void QDltOptManager::reset()
{
project = false;
Expand Down
9 changes: 6 additions & 3 deletions qdlt/qdltoptmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@
#define QDLTOPTMANAGER_H

#include <QStringList>
#include <QCommandLineParser>

#include "export_rules.h"

class QCommandLineParser;

enum e_convertionmode
{
e_ASCI = 0,
Expand All @@ -48,7 +47,7 @@ class QDLT_EXPORT QDltOptManager
static QDltOptManager* getInstance();
void printUsage(const QString& helpText);
void printVersion(QString appname);
void parse(const QCommandLineParser& parser, const QString& executableName);
void parse(const QStringList& arguments);

bool isProjectFile();
bool isTerminate();
Expand All @@ -73,6 +72,8 @@ class QDLT_EXPORT QDltOptManager
const QStringList &getMf4Files() const;
char getDelimiter();

QString getHelpText() const;

// only testing relevant
void reset();

Expand Down Expand Up @@ -103,6 +104,8 @@ class QDLT_EXPORT QDltOptManager

QString workingDirectory;
char delimiter;

QCommandLineParser m_parser;
};

#endif //QDLTOPTMANAGER_H
62 changes: 11 additions & 51 deletions qdlt/tests/test_dltoptmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,49 +24,21 @@ class OptManagerTest : public ::testing::Test {

void SetUp() override {
m_manager->reset();

// TODO: move to a reusable method
m_parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);

m_parser.addPositionalArgument("logfile", "Loading one or more logfiles on startup (must end with .dlt)");
m_parser.addPositionalArgument("projectfile", "Loading project file on startup (must end with .dlp)");
m_parser.addPositionalArgument("filterfile", "Loading filterfile on startup (must end with .dlf)");
m_parser.addPositionalArgument("pcapfile", "Importing DLT/IPC from pcap file on startup (must end with .pcap)");
m_parser.addPositionalArgument("mf4file", "Importing DLT/IPC from mf4 file on startup (must end with .mf4)");
m_parser.addOptions({
{"c", "Convert logfile file to <textfile>", "textfile"},
{"u", "Conversion will be done in UTF8 instead of ASCII"},
{"csv", "Conversion will be done in CSV format"},
{"d", "Conversion will NOT be done, save in dlt file format again instead"},
{"dd", "Conversion will NOT be done, save as decoded messages in dlt format"},
{"b", "Execute a plugin command with <n> parameters before loading log file.", "plugin|command|param1|..|param<n>"},
{"e", "Execute a plugin command with <n> parameters after loading log file.", "plugin|command|param1|..|param<n>"},
{QStringList() << "s" << "silent", "Enable silent mode without any GUI. Ideal for commandline usage."},
{"stream", "Treat the input logfiles as DLT stream instead of DLT files."},
{QStringList() << "t" << "terminate", "Terminate DLT Viewer after command line execution."},
{"w", "Set the working directory", "workingdirectory"},
{"delimiter", "The used delimiter for CSV export (Default: ,).", "character"},
{QStringList() << "h" << "help", "Print this help message."},
{QStringList() << "v" << "version", "Print the version."}
});
}

void TearDown() override {
logMessageSink.clear();
}

static QDltOptManager* m_manager;
QCommandLineParser m_parser;

};

QDltOptManager* OptManagerTest::m_manager = nullptr;

TEST_F(OptManagerTest, txtConversion) {
auto args = QStringList() << "executable" << "-t" << "-c" << "output.txt" << "input.dlt";
m_parser.parse(args);

m_manager->parse(m_parser, "test");
m_manager->parse(args);

EXPECT_TRUE(m_manager->isTerminate());
EXPECT_EQ(m_manager->getConvertDestFile(), "output.txt");
Expand All @@ -76,9 +48,8 @@ TEST_F(OptManagerTest, txtConversion) {

TEST_F(OptManagerTest, txtConversionSilentUtf8Mode) {
auto args = QStringList() << "executable" << "-t" << "-s" << "-u" << "-c" << "output.txt" << "input.dlt";
m_parser.parse(args);

m_manager->parse(m_parser, "test");
m_manager->parse(args);

EXPECT_TRUE(m_manager->isTerminate());
EXPECT_TRUE(m_manager->issilentMode());
Expand All @@ -90,9 +61,8 @@ TEST_F(OptManagerTest, txtConversionSilentUtf8Mode) {

TEST_F(OptManagerTest, txtConversionSilentAsciiMode) {
auto args = QStringList() << "executable" << "-t" << "-s" << "-d" << "-c" << "output.txt" << "input.dlt";
m_parser.parse(args);

m_manager->parse(m_parser, "test");
m_manager->parse(args);

EXPECT_TRUE(m_manager->isTerminate());
EXPECT_TRUE(m_manager->issilentMode());
Expand All @@ -104,9 +74,8 @@ TEST_F(OptManagerTest, txtConversionSilentAsciiMode) {

TEST_F(OptManagerTest, csvConversionSilentMode) {
auto args = QStringList() << "executable" << "-t" << "-s" << "-csv" << "-c" << "output.csv" << "input.dlt";
m_parser.parse(args);

m_manager->parse(m_parser, "test");
m_manager->parse(args);

EXPECT_TRUE(m_manager->isTerminate());
EXPECT_TRUE(m_manager->issilentMode());
Expand All @@ -118,9 +87,8 @@ TEST_F(OptManagerTest, csvConversionSilentMode) {

TEST_F(OptManagerTest, txtConversionSilentDdlMode) {
auto args = QStringList() << "executable" << "-t" << "-s" << "decoded.dlp" << "-dd" << "-c" << "output.dlt" << "input.dlt";
m_parser.parse(args);

m_manager->parse(m_parser, "test");
m_manager->parse(args);

EXPECT_TRUE(m_manager->isTerminate());
EXPECT_TRUE(m_manager->issilentMode());
Expand All @@ -138,9 +106,7 @@ TEST_F(OptManagerTest, pluginPostCommands) {
<< "-e"
<< "\"Filetransfer Plugin|export|ftransferdir\""
<< "input.dlt";
m_parser.parse(args);

m_manager->parse(m_parser, "test");
m_manager->parse(args);

EXPECT_EQ(m_manager->getProjectFile(), "export.dlp");
EXPECT_TRUE(m_manager->getLogFiles().contains("input.dlt"));
Expand All @@ -153,9 +119,7 @@ TEST_F(OptManagerTest, pluginPreCommands) {
<< "-b"
<< "\"Filetransfer Plugin|export|ftransferdir\""
<< "input.dlt";
m_parser.parse(args);

m_manager->parse(m_parser, "test");
m_manager->parse(args);

EXPECT_TRUE(m_manager->getLogFiles().contains("input.dlt"));
EXPECT_TRUE(m_manager->getPrePluginCommands().contains("\"Filetransfer Plugin|export|ftransferdir\""));
Expand All @@ -164,19 +128,17 @@ TEST_F(OptManagerTest, pluginPreCommands) {

TEST_F(OptManagerTest, multipleLogFiles) {
auto args = QStringList() << "executable" << "input1.dlt" << "input2.dlt";
m_parser.parse(args);

m_manager->parse(m_parser, "test");
m_manager->parse(args);

EXPECT_TRUE(m_manager->getLogFiles().contains("input1.dlt"));
EXPECT_TRUE(m_manager->getLogFiles().contains("input2.dlt"));
}

TEST_F(OptManagerTest, pcapFile) {
auto args = QStringList() << "executable" << "-t" << "-c" << "output.txt" << "input.pcap";
m_parser.parse(args);

m_manager->parse(m_parser, "test");
m_manager->parse(args);

EXPECT_TRUE(m_manager->isTerminate());
EXPECT_EQ(m_manager->getConvertDestFile(), "output.txt");
Expand All @@ -186,9 +148,8 @@ TEST_F(OptManagerTest, pcapFile) {

TEST_F(OptManagerTest, mf4Files) {
auto args = QStringList() << "executable" << "-t" << "-c" << "output.txt" << "input1.mf4" << "input2.mf4";
m_parser.parse(args);

m_manager->parse(m_parser, "test");
m_manager->parse(args);

EXPECT_TRUE(m_manager->isTerminate());
EXPECT_EQ(m_manager->getConvertDestFile(), "output.txt");
Expand All @@ -201,9 +162,8 @@ TEST_F(OptManagerTest, version) {
// impossible to check just version because there is a call to exit(0) in the qdltoptmanager
// but any output will be enough to check the version call because it is always printed
auto args = QStringList() << "executable" << "some.dlt";
m_parser.parse(args);

m_manager->parse(m_parser, "test");
m_manager->parse(args);

EXPECT_TRUE(logMessageSink.contains("Executable Name:"));
EXPECT_TRUE(logMessageSink.contains("Build time:"));
Expand Down
Loading

0 comments on commit 71d6e78

Please sign in to comment.