Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use QCommandLineParser for command line args parsing #543

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
332 changes: 169 additions & 163 deletions qdlt/qdltoptmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "qdltoptmanager.h"
#include "version.h"

#include <QDebug>
#include <QFileInfo>

Expand All @@ -37,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 @@ -79,206 +105,157 @@ void QDltOptManager::printVersion(QString appname)
qDebug() << "Version:" << PACKAGE_VERSION << PACKAGE_VERSION_STATE;
}

void QDltOptManager::printUsage()
void QDltOptManager::printUsage(const QString& helpText)
{
#if (WIN32)
qDebug()<<"Usage: dlt-viewer.exe [OPTIONS] [logfile] [projectfile] [filterfile] [mf4file] [pcapfile]";
#else
qDebug()<<"Usage: dlt-viewer [OPTIONS] [logfile] [projectfile] [filterfile] [mf4file] [pcapfile]";
#endif

qDebug()<<"\nOptions:";
qDebug()<<" [logfile]\tLoading one or more logfiles on startup (must end with .dlt)";
qDebug()<<" [projectfile]\tLoading project file on startup (must end with .dlp)";
qDebug()<<" [filterfile]\tLoading filterfile on startup (must end with .dlf)";
qDebug()<<" [pcapfile]\tImporting DLT/IPC from pcap file on startup (must end with .pcap)";
qDebug()<<" [mf4file]\tImporting DLT/IPC from mf4 file on startup (must end with .mf4)";
qDebug()<<" -h or --help\tPrint usage";
qDebug()<<" -c textfile\tConvert logfile file to textfile";
qDebug()<<" -u\tConversion will be done in UTF8 instead of ASCII";
qDebug()<<" -csv\tConversion will be done in CSV format";
qDebug()<<" -d\tConversion will NOT be done, save in dlt file format again instead";
qDebug()<<" -dd\tConversion will NOT be done, save as decoded messages in dlt format";
qDebug()<<" -b \"plugin|command|param1|..|param<n>\"\tExecute a plugin command with <n> parameters before loading log file.";
qDebug()<<" -e \"plugin|command|param1|..|param<n>\"\tExecute a plugin command with <n> parameters after loading log file.";
qDebug()<<" -s or --silent\tEnable silent mode without any GUI. Ideal for commandline usage.";
qDebug()<<" -stream\tTreat the input logfiles as DLT stream instead of DLT files.";
qDebug()<<" -t or --terminate\tTerminate DLT Viewer after command line execution.";
qDebug()<<" -v or --version\tOnly show version and buildtime information";
qDebug()<<" -w workingdirectory\tSet the working directory";
qDebug()<<" -delimiter <character>\tThe used delimiter for CSV export (Default: ,).";
qDebug()<<"\nExamples:";
qDebug()<<" dlt-viewer.exe -t -c output.txt input.dlt";
qDebug()<<" dlt-viewer.exe -t -s -u -c output.txt input.dlt";
qDebug()<<" dlt-viewer.exe -t -s -d -c output.dlt input.dlt";
qDebug()<<" dlt-viewer.exe -t -s decoded.dlp -dd -c output.dlt input.dlt ";
qDebug()<<" dlt-viewer.exe -t -s -csv -c output.csv input.dlt";
qDebug()<<" dlt-viewer.exe -t -s -d filter.dlf -c output.dlt input.dlt";
qDebug()<<" dlt-viewer.exe -p export.dlp -e \"Filetransfer Plugin|export|ftransferdir\" input.dlt";
qDebug()<<" dlt-viewer.exe input1.dlt input2.dlt";
qDebug()<<" dlt-viewer.exe -t -c output.txt input.pcap";
qDebug()<<" dlt-viewer.exe -t -c output.txt input1.mf4 input2.mf4";
qDebug() << helpText.toStdString().c_str();
qDebug() << "\nExamples:";
qDebug() << " dlt-viewer.exe -t -c output.txt input.dlt";
qDebug() << " dlt-viewer.exe -t -s -u -c output.txt input.dlt";
qDebug() << " dlt-viewer.exe -t -s -d -c output.dlt input.dlt";
qDebug() << " dlt-viewer.exe -t -s decoded.dlp -dd -c output.dlt input.dlt ";
qDebug() << " dlt-viewer.exe -t -s -csv -c output.csv input.dlt";
qDebug() << " dlt-viewer.exe -t -s -d filter.dlf -c output.dlt input.dlt";
qDebug() << " dlt-viewer.exe -p export.dlp -e \"Filetransfer Plugin|export|ftransferdir\" input.dlt";
qDebug() << " dlt-viewer.exe input1.dlt input2.dlt";
qDebug() << " dlt-viewer.exe -t -c output.txt input.pcap";
qDebug() << " dlt-viewer.exe -t -c output.txt input1.mf4 input2.mf4";
}

void QDltOptManager::parse(QStringList *opt)
void QDltOptManager::parse(const QStringList& args)
{
QString str;
m_parser.parse(args);

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

printVersion(opt->at(0));

qDebug() << "### Parsing Options";
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
*/
//str = opt->at(0); && ( str.compare("-h)") != 0 || str.compare("-v") !=0 )
if(opt->size()==2 )
{
if(opt->at(1).endsWith(".dlp") || opt->at(1).endsWith(".DLP"))
{
projectFile = QString("%1").arg(opt->at(1));
project = true;
qDebug()<< "Project filename:" << projectFile;
return;
}
if(opt->at(1).endsWith(".dlt") || opt->at(1).endsWith(".DLT"))
{
const QString logFile = QString("%1").arg(opt->at(1));
logFiles += logFile;
qDebug()<< "DLT filename:" << logFile;
return;
}
}

// 0==Binary 1==First Argument
for (int i = 0; i < opt->size(); ++i)
{
str = opt->at(i);

if(str.compare("-h") == 0 || str.compare("--help") == 0)
{
printUsage();
exit(0);
}
else if(str.compare("-s") == 0 || str.compare("--silent") == 0)
{
if ( silent_mode == false)
{
silent_mode = true;
qDebug() << "Silent mode enabled";
}
}
else if(str.compare("-v") == 0 || str.compare("--version") == 0)
{
printVersion(opt->at(0));
exit(0);
}
else if(str.compare("-t") == 0 || str.compare("--terminate") == 0)
{
terminate = true;
commandline_mode = true;
}
else if(str.compare("-c")==0)
{
QString c1 = opt->value(i+1);

convertDestFile = QString("%1").arg(c1);
// check here already if the selected file exists

qDebug() << "Convert filename:" << convertDestFile;
commandline_mode = true;

i += 1;
}
else if(str.compare("-delimiter")==0)
{
QString c1 = opt->value(i+1);

delimiter = QString("%1").arg(c1).front().toLatin1();

qDebug() << "Delimiter:" << delimiter;

i += 1;
}
else if(str.compare("-u")==0)
{
convertionmode = e_UTF8;
}
else if(str.compare("-csv")==0)
{
convertionmode = e_CSV;
}
else if(str.compare("-d")==0)
{
convertionmode = e_DLT;
}
else if(str.compare("-dd")==0)
{
convertionmode = e_DDLT;
}
else if(str.compare("-stream")==0)
{
inputmode = e_inputmode::STREAM;
}
else if(str.compare("-e")==0)
{
QString c = opt->value(i+1);
postPluginCommands += c;
commandline_mode = true;
++i;
}
else if(str.compare("-b")==0)
{
QString c = opt->value(i+1);
prePluginCommands += c;
commandline_mode = true;
++i;
}
else if (str.compare("-w") == 0)
if (m_parser.optionNames().isEmpty() && m_parser.positionalArguments().size() == 1)
{
const QString& arg = m_parser.positionalArguments().at(0);
if(arg.endsWith(".dlp") || arg.endsWith(".DLP"))
{
workingDirectory = opt->value(i+1);
++i;
projectFile = arg;
project = true;
qDebug()<< "Project filename:" << projectFile;
return;
}
if (arg.endsWith(".dlt") || arg.endsWith(".DLT"))
{
const QString logFile = arg;
logFiles += logFile;
qDebug()<< "DLT filename:" << logFile;
return;
}
else if(opt->at(i).endsWith(".dlt") || opt->at(i).endsWith(".DLT"))
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

QStringList positionalArguments = m_parser.positionalArguments();
for (const QString &arg : positionalArguments)
{
if(arg.endsWith(".dlt") || arg.endsWith(".DLT"))
{
const QString logFile = QString("%1").arg(opt->at(i));
const QString logFile = arg;
logFiles += logFile;
qDebug()<< "DLT filename:" << logFile;
}
else if(opt->at(i).endsWith(".dlp") || opt->at(i).endsWith(".DLP"))
else if(arg.endsWith(".dlp") || arg.endsWith(".DLP"))
{
if (project == true)
{
qDebug() << "\nError: Can only load one project file\n";
printUsage();
printUsage(m_parser.helpText());
exit(-1);
}

projectFile = QString("%1").arg(opt->at(i));
projectFile = arg;
project = true;
qDebug()<< "Project filename:" << projectFile;
}
else if(opt->at(i).endsWith(".dlf") || opt->at(i).endsWith(".DLF"))
else if(arg.endsWith(".dlf") || arg.endsWith(".DLF"))
{
filterFiles += QString("%1").arg(opt->at(i));
qDebug()<< "Filter filename:" << QString("%1").arg(opt->at(i));
filterFiles += arg;
qDebug()<< "Filter filename:" << arg;
}
else if(opt->at(i).endsWith(".pcap") || opt->at(i).endsWith(".PCAP"))
else if(arg.endsWith(".pcap") || arg.endsWith(".PCAP"))
{
const QString pcapFile = QString("%1").arg(opt->at(i));
const QString pcapFile = arg;
pcapFiles += pcapFile;
qDebug()<< "Pcap filename:" << pcapFile;
}
else if(opt->at(i).endsWith(".mf4") || opt->at(i).endsWith(".MF4"))
else if(arg.endsWith(".mf4") || arg.endsWith(".MF4"))
{
const QString mf4File = QString("%1").arg(opt->at(i));
const QString mf4File = arg;
mf4Files += mf4File;
qDebug()<< "MF4 filename:" << mf4File;
}

} // end of for loop
}

/* On Windows we do not want to open a console in case
* we start the application e.g. from file explorer.
Expand Down Expand Up @@ -315,3 +292,32 @@ QString QDltOptManager::getCommandName(){return commandName;}
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;
terminate = false;
silent_mode = false;
commandline_mode = false;
convertionmode = e_ASCI;
inputmode = e_inputmode::DLT;
projectFile.clear();
logFiles.clear();
filterFiles.clear();
convertDestFile.clear();
pluginName.clear();
commandName.clear();
commandParams.clear();
prePluginCommands.clear();
postPluginCommands.clear();
workingDirectory.clear();
delimiter=',';
pcapFiles.clear();
mf4Files.clear();
}

Loading