Add rough implementation of some command line options

Support some additional command line options for opening and running SQL
scripts directly.

Don't just take the first argument as a SQLite database to open as it
could be a command line option - Qt and X11 have some default ones.
This commit is contained in:
Martin Kleusberg
2013-05-14 18:12:11 +02:00
parent 6ba0cd76c0
commit 04b431d3f1
3 changed files with 74 additions and 7 deletions

View File

@@ -4,6 +4,7 @@
#include <QTranslator>
#include <QLibraryInfo>
#include <QLocale>
#include <QDebug>
#if 0 and defined(Q_WS_MAC)
#include <Carbon/Carbon.h>
@@ -90,16 +91,75 @@ int main( int argc, char ** argv )
apptranslator.load("translations/tr_" + QLocale::system().name());
a.installTranslator(&apptranslator);
// Parse command line
QString fileToOpen;
QStringList sqlToExecute;
bool quitApplication = false;
for(int i=1;i<a.arguments().size();i++)
{
// Check next command line argument
if(a.arguments().at(i) == "-h" || a.arguments().at(i) == "--help")
{
// Help
qWarning() << qPrintable(QObject::tr("Usage: %1 [options] [db]\n").arg(argv[0]));
qWarning() << qPrintable(QObject::tr("Possible command line arguments:"));
qWarning() << qPrintable(QObject::tr(" -h, --help\t\tShow command line options"));
qWarning() << qPrintable(QObject::tr(" -s, --sql [file]\tExecute this SQL file after opening the DB"));
qWarning() << qPrintable(QObject::tr(" -q, --quit\t\tExit application after running scripts"));
qWarning() << qPrintable(QObject::tr(" [file]\t\tOpen this SQLite database"));
return 0;
} else if(a.arguments().at(i) == "-s" || a.arguments().at(i) == "--sql") {
// Run SQL file: If file exists add it to list of scripts to execute
if(++i >= a.arguments().size())
qWarning() << qPrintable(QObject::tr("The -s/--sql option requires an argument"));
else if(!QFile::exists(a.arguments().at(i)))
qWarning() << qPrintable(QObject::tr("The file %1 does not exist").arg(a.arguments().at(i)));
else
sqlToExecute.append(a.arguments().at(i));
} else if(a.arguments().at(i) == "-q" || a.arguments().at(i) == "--quit") {
quitApplication = true;
} else {
// Other: Check if it's a valid file name
if(QFile::exists(a.arguments().at(i)))
fileToOpen = a.arguments().at(i);
else
qWarning() << qPrintable(QObject::tr("Invalid option/non-existant file: %1").arg(a.arguments().at(i)));
}
}
MainWindow w;
#if defined(Q_WS_MAC)
// AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
// NewAEEventHandlerUPP(odocHandler),reinterpret_cast<long>(&w),false);
#endif // Q_WS_MAC
// Show main window
w.show();
if (argc>1) {
//first and only argument we accept is the name of the database to open
w.fileOpen(QString(argv[1]));
a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
// Open database if one was specified
if(fileToOpen.size())
{
if(w.fileOpen(fileToOpen))
{
// If database could be opened run the SQL scripts
foreach(const QString& f, sqlToExecute)
{
QFile file(f);
if(file.open(QIODevice::ReadOnly))
{
w.getDb()->executeMultiSQL(file.readAll(), false, true);
file.close();
}
}
w.browseRefresh();
}
}
a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
// Quit application now if user doesn't want to see the UI
if(quitApplication)
return 0;
// Run application
return a.exec();
}