parent
da0ebc6180
commit
28528963a8
@ -0,0 +1,106 @@
|
||||
#include "argumentparser.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QStringList>
|
||||
#include <QFileInfo>
|
||||
#include <QDebug>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
ArgumentParser::ArgumentParser(QObject *parent) :
|
||||
QObject(parent),
|
||||
m_isSystemTray(false),
|
||||
m_isShowMinimized(false),
|
||||
m_filePath()
|
||||
{
|
||||
qDebug() << __PRETTY_FUNCTION__;
|
||||
}
|
||||
|
||||
|
||||
void ArgumentParser::printUsage()
|
||||
{
|
||||
qDebug() << __PRETTY_FUNCTION__;
|
||||
|
||||
std::cout << tr("Usage: ").toStdString() << "qtmindmap [OPTION][FILE]" << std::endl
|
||||
<< tr("Mindmap program in QT").toStdString() << std::endl
|
||||
<< std::endl
|
||||
<< tr("Options:").toStdString() << std::endl
|
||||
<< "-h, --help\t\t" << tr("Prints this help.").toStdString() << std::endl
|
||||
<< "-t, --tray\t\t" << tr("Starts application in system tray.").toStdString() << std::endl
|
||||
<< "-s, --show-minimized\t" << tr("Hide main window, just show systray icon.").toStdString() << std::endl
|
||||
<< std::endl
|
||||
<< tr("Report bugs to: ").toStdString() << "denes.matetelki@gmail.com" << std::endl;
|
||||
}
|
||||
|
||||
|
||||
bool ArgumentParser::parseCmdLineArgs(bool &successful)
|
||||
{
|
||||
qDebug() << __PRETTY_FUNCTION__;
|
||||
|
||||
QStringList cmdlineArgs = QCoreApplication::arguments();
|
||||
cmdlineArgs.removeFirst();
|
||||
|
||||
QRegExp help("^-(h|-help)$");
|
||||
if (!cmdlineArgs.filter(help).isEmpty())
|
||||
{
|
||||
printUsage();
|
||||
successful = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
QRegExp tray("^-(t|-tray)$");
|
||||
if (!cmdlineArgs.filter(tray).isEmpty()) m_isSystemTray = true;
|
||||
|
||||
QRegExp minimized("^-(s|-show-minimized)$");
|
||||
if (!cmdlineArgs.filter(minimized).isEmpty()) m_isShowMinimized = true;
|
||||
|
||||
/// @note It is an error? Shall it be handled?
|
||||
// if (isSystemTray && isShowMinimized) return false;
|
||||
|
||||
QRegExp all("^-(t|-tray|h|-help|s|-show-minimized)$");
|
||||
QStringList others;
|
||||
foreach (QString arg, cmdlineArgs) if (all.indexIn(arg)==-1) others.append(arg);
|
||||
if (others.size() > 1)
|
||||
{
|
||||
std::cerr << tr("Unkown options: ").toStdString() << others.join(" ").toStdString() << std::endl;
|
||||
printUsage();
|
||||
successful = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (others.size()==1)
|
||||
{
|
||||
if (others.first().at(0)=='-')
|
||||
{
|
||||
successful = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
/// @note filecheck shall be done elsewhere?
|
||||
m_filePath = others.first();
|
||||
QFileInfo fileInfo(m_filePath);
|
||||
if (!fileInfo.exists())
|
||||
{
|
||||
std::cerr << tr("File: ").toStdString() << m_filePath.toStdString() << tr(" does not exists.").toStdString() << std::endl;
|
||||
successful = false;
|
||||
return false;
|
||||
}
|
||||
if (!fileInfo.isFile())
|
||||
{
|
||||
std::cerr << tr("File: ").toStdString() << m_filePath.toStdString() << tr(" is not a file.").toStdString() << std::endl;
|
||||
successful = false;
|
||||
return false;
|
||||
}
|
||||
if (!fileInfo.isReadable())
|
||||
{
|
||||
std::cerr << tr("File: ").toStdString() << m_filePath.toStdString() << tr(" is not readable.").toStdString() << std::endl;
|
||||
successful = false;
|
||||
return false;
|
||||
}
|
||||
if (!fileInfo.isWritable())
|
||||
{
|
||||
std::cout << tr("File: ").toStdString() << m_filePath.toStdString() << tr(" is not writeable.").toStdString() << std::endl;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
#ifndef ARGUMENTPARSER_H
|
||||
#define ARGUMENTPARSER_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
class ArgumentParser : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
explicit ArgumentParser(QObject *parent = 0);
|
||||
|
||||
/***
|
||||
* @param successful is true is the program needs to stop but its not an error.
|
||||
* @return true is program can continue
|
||||
*/
|
||||
bool parseCmdLineArgs(bool &successful);
|
||||
|
||||
bool isSystemTray() { return m_isSystemTray; }
|
||||
bool isShowMinimized() { return m_isShowMinimized; }
|
||||
QString filePath() { return m_filePath; }
|
||||
|
||||
|
||||
private:
|
||||
|
||||
void printUsage();
|
||||
|
||||
bool m_isSystemTray;
|
||||
bool m_isShowMinimized;
|
||||
QString m_filePath;
|
||||
};
|
||||
|
||||
#endif // ARGUMENTPARSER_H
|
Loading…
Reference in new issue