high_flyer, you are correct and I have got things to work the way I want on Linux (and most likely on Mac OS X too) using the following code:
int main(int pArgc, char *pArgv[])
{
QtSingleApplication app(pArgc, pArgv);
// Handle the command line options, if any
QxtCommandOptions options;
options.add("help", "Show this help text");
options.alias("help", "h");
options.parse(app.arguments());
if(options.count("help") || options.showUnrecognizedWarning())
{
options.showUsage();
return -1;
}
...
// Execute the application
return app.exec();
}
int main(int pArgc, char *pArgv[])
{
QtSingleApplication app(pArgc, pArgv);
// Handle the command line options, if any
QxtCommandOptions options;
options.add("help", "Show this help text");
options.alias("help", "h");
options.parse(app.arguments());
if(options.count("help") || options.showUnrecognizedWarning())
{
options.showUsage();
return -1;
}
...
// Execute the application
return app.exec();
}
To copy to clipboard, switch view to plain text mode
It's just that on Windows, a GUI application cannot directly write to the console (unless your application is a pure console application which mine clearly isn't) and
options.showUsage();
options.showUsage();
To copy to clipboard, switch view to plain text mode
above will therefore never output anything while it does on Linux.
Now, I had a quick look at the Windows API and it would seem that one might be able to do work around this issue by attaching a console to the GUI application:
#ifdef Q_WS_WIN
#define _WIN32_WINNT 0x0501 // I.e. Windows XP and higher
#include <windows.h>
#endif
...
int main(int pArgc, char *pArgv[])
{
...
if(options.count("help") || options.showUnrecognizedWarning())
{
#ifdef Q_WS_WIN
AttachConsole(ATTACH_PARENT_PROCESS);
// Some calls to WriteConsole to simulate the call to options.showUsage() which
// works perfectly fine on Linux but not on bloody Windows!
#else
options.showUsage();
#endif
return -1;
}
...
}
#ifdef Q_WS_WIN
#define _WIN32_WINNT 0x0501 // I.e. Windows XP and higher
#include <windows.h>
#endif
...
int main(int pArgc, char *pArgv[])
{
...
if(options.count("help") || options.showUnrecognizedWarning())
{
#ifdef Q_WS_WIN
AttachConsole(ATTACH_PARENT_PROCESS);
// Some calls to WriteConsole to simulate the call to options.showUsage() which
// works perfectly fine on Linux but not on bloody Windows!
#else
options.showUsage();
#endif
return -1;
}
...
}
To copy to clipboard, switch view to plain text mode
The problem with the above is that it's really ugly and very much looks like a big hack to get around Windows' stupid limitation.
Bookmarks