mirror of
https://github.com/juce-framework/JUCE.git
synced 2026-01-10 23:44:24 +00:00
Refactored the handling of command-line args: added new static methods JUCEApplication::getCommandLineParameters() and getCommandLineParameterArray().
This commit is contained in:
parent
1406b72b54
commit
18cfadf898
11 changed files with 112 additions and 102 deletions
|
|
@ -136,10 +136,8 @@ bool JUCEApplication::perform (const InvocationInfo& info)
|
|||
}
|
||||
|
||||
//==============================================================================
|
||||
bool JUCEApplication::initialiseApp (const String& commandLine)
|
||||
bool JUCEApplication::initialiseApp()
|
||||
{
|
||||
commandLineParameters = commandLine.trim();
|
||||
|
||||
#if ! (JUCE_IOS || JUCE_ANDROID)
|
||||
jassert (appLock == nullptr); // initialiseApp must only be called once!
|
||||
|
||||
|
|
@ -150,7 +148,7 @@ bool JUCEApplication::initialiseApp (const String& commandLine)
|
|||
if (! appLock->enter(0))
|
||||
{
|
||||
appLock = nullptr;
|
||||
MessageManager::broadcastMessage (getApplicationName() + "/" + commandLineParameters);
|
||||
MessageManager::broadcastMessage (getApplicationName() + "/" + getCommandLineParameters());
|
||||
|
||||
DBG ("Another instance is running - quitting...");
|
||||
return false;
|
||||
|
|
@ -159,7 +157,7 @@ bool JUCEApplication::initialiseApp (const String& commandLine)
|
|||
#endif
|
||||
|
||||
// let the app do its setting-up..
|
||||
initialise (commandLineParameters);
|
||||
initialise (getCommandLineParameters());
|
||||
|
||||
#if JUCE_MAC
|
||||
juce_initialiseMacMainMenu(); // needs to be called after the app object has created, to get its name
|
||||
|
|
@ -190,73 +188,85 @@ int JUCEApplication::shutdownApp()
|
|||
}
|
||||
|
||||
//==============================================================================
|
||||
#if ! JUCE_ANDROID
|
||||
int JUCEApplication::main (const String& commandLine)
|
||||
#if JUCE_ANDROID
|
||||
|
||||
StringArray JUCEApplication::getCommandLineParameterArray() { return StringArray(); }
|
||||
String JUCEApplication::getCommandLineParameters() { return String::empty; }
|
||||
|
||||
#else
|
||||
|
||||
int JUCEApplication::main()
|
||||
{
|
||||
ScopedJuceInitialiser_GUI libraryInitialiser;
|
||||
jassert (createInstance != nullptr);
|
||||
int returnCode = 0;
|
||||
|
||||
const ScopedPointer<JUCEApplication> app (dynamic_cast <JUCEApplication*> (createInstance()));
|
||||
jassert (app != nullptr);
|
||||
|
||||
if (! app->initialiseApp())
|
||||
return 0;
|
||||
|
||||
JUCE_TRY
|
||||
{
|
||||
const ScopedPointer<JUCEApplication> app (dynamic_cast <JUCEApplication*> (createInstance()));
|
||||
|
||||
jassert (app != nullptr);
|
||||
|
||||
if (! app->initialiseApp (commandLine))
|
||||
return 0;
|
||||
|
||||
JUCE_TRY
|
||||
{
|
||||
// loop until a quit message is received..
|
||||
MessageManager::getInstance()->runDispatchLoop();
|
||||
}
|
||||
JUCE_CATCH_EXCEPTION
|
||||
|
||||
returnCode = app->shutdownApp();
|
||||
// loop until a quit message is received..
|
||||
MessageManager::getInstance()->runDispatchLoop();
|
||||
}
|
||||
JUCE_CATCH_EXCEPTION
|
||||
|
||||
return returnCode;
|
||||
return app->shutdownApp();
|
||||
}
|
||||
|
||||
#if ! JUCE_WINDOWS
|
||||
#if JUCE_IOS
|
||||
extern int juce_iOSMain (int argc, const char* argv[]);
|
||||
#endif
|
||||
|
||||
#if ! JUCE_WINDOWS
|
||||
extern const char* juce_Argv0;
|
||||
#endif
|
||||
|
||||
#if JUCE_MAC
|
||||
extern void initialiseNSApplication();
|
||||
#endif
|
||||
|
||||
const char* const* juce_argv = nullptr;
|
||||
int juce_argc = 0;
|
||||
|
||||
StringArray JUCEApplication::getCommandLineParameterArray()
|
||||
{
|
||||
return StringArray (juce_argv + 1, juce_argc - 1);
|
||||
}
|
||||
|
||||
String JUCEApplication::getCommandLineParameters()
|
||||
{
|
||||
String argString;
|
||||
|
||||
for (int i = 1; i < juce_argc; ++i)
|
||||
{
|
||||
String arg (juce_argv[i]);
|
||||
|
||||
if (arg.containsChar (' ') && ! arg.isQuotedString())
|
||||
arg = arg.quoted ('"');
|
||||
|
||||
argString << arg << ' ';
|
||||
}
|
||||
|
||||
return argString.trim();
|
||||
}
|
||||
|
||||
int JUCEApplication::main (int argc, const char* argv[])
|
||||
{
|
||||
JUCE_AUTORELEASEPOOL
|
||||
|
||||
juce_argc = argc;
|
||||
juce_argv = argv;
|
||||
|
||||
#if JUCE_MAC
|
||||
initialiseNSApplication();
|
||||
#endif
|
||||
|
||||
#if ! JUCE_WINDOWS
|
||||
jassert (createInstance != nullptr);
|
||||
juce_Argv0 = argv[0];
|
||||
#endif
|
||||
|
||||
#if JUCE_IOS
|
||||
return juce_iOSMain (argc, argv);
|
||||
#else
|
||||
String cmd;
|
||||
for (int i = 1; i < argc; ++i)
|
||||
{
|
||||
String arg (argv[i]);
|
||||
if (arg.containsChar (' ') && ! arg.isQuotedString())
|
||||
arg = arg.quoted ('"');
|
||||
|
||||
cmd << arg << ' ';
|
||||
}
|
||||
|
||||
return JUCEApplication::main (cmd);
|
||||
return JUCEApplication::main();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -204,8 +204,15 @@ public:
|
|||
*/
|
||||
int getApplicationReturnValue() const noexcept { return appReturnValue; }
|
||||
|
||||
/** Returns the application's command line parameters. */
|
||||
const String& getCommandLineParameters() const noexcept { return commandLineParameters; }
|
||||
/** Returns the application's command line parameters as a set of strings.
|
||||
@see getCommandLineParameters
|
||||
*/
|
||||
static StringArray JUCE_CALLTYPE getCommandLineParameterArray();
|
||||
|
||||
/** Returns the application's command line parameters as a single string.
|
||||
@see getCommandLineParameterArray
|
||||
*/
|
||||
static String JUCE_CALLTYPE getCommandLineParameters();
|
||||
|
||||
/** Returns true if this executable is running as an app (as opposed to being a plugin
|
||||
or other kind of shared library. */
|
||||
|
|
@ -224,16 +231,15 @@ public:
|
|||
//==============================================================================
|
||||
#ifndef DOXYGEN
|
||||
// The following methods are internal calls - not for public use.
|
||||
static int main (const String& commandLine);
|
||||
static int main();
|
||||
static int main (int argc, const char* argv[]);
|
||||
static void sendUnhandledException (const std::exception*, const char* sourceFile, int lineNumber);
|
||||
bool initialiseApp (const String& commandLine);
|
||||
bool initialiseApp();
|
||||
int shutdownApp();
|
||||
#endif
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
String commandLineParameters;
|
||||
ScopedPointer<InterProcessLock> appLock;
|
||||
ScopedPointer<ActionListener> broadcastCallback;
|
||||
int appReturnValue;
|
||||
|
|
|
|||
|
|
@ -89,8 +89,16 @@ public:
|
|||
#define START_JUCE_APPLICATION(AppClass) \
|
||||
juce::JUCEApplication* juce_CreateApplication() { return new AppClass(); }
|
||||
|
||||
#elif defined (JUCE_GCC) || defined (__MWERKS__)
|
||||
#elif JUCE_WINDOWS
|
||||
#define START_JUCE_APPLICATION(AppClass) \
|
||||
static juce::JUCEApplicationBase* juce_CreateApplication() { return new AppClass(); } \
|
||||
int __stdcall WinMain (void*, void*, const char*, int) \
|
||||
{ \
|
||||
juce::JUCEApplication::createInstance = &juce_CreateApplication; \
|
||||
return juce::JUCEApplication::main(); \
|
||||
}
|
||||
|
||||
#else
|
||||
#define START_JUCE_APPLICATION(AppClass) \
|
||||
static juce::JUCEApplicationBase* juce_CreateApplication() { return new AppClass(); } \
|
||||
int main (int argc, char* argv[]) \
|
||||
|
|
@ -99,36 +107,6 @@ public:
|
|||
return juce::JUCEApplication::main (argc, (const char**) argv); \
|
||||
}
|
||||
|
||||
#elif JUCE_WINDOWS
|
||||
|
||||
#ifdef _CONSOLE
|
||||
#define START_JUCE_APPLICATION(AppClass) \
|
||||
static juce::JUCEApplicationBase* juce_CreateApplication() { return new AppClass(); } \
|
||||
int main (int, char* argv[]) \
|
||||
{ \
|
||||
juce::JUCEApplication::createInstance = &juce_CreateApplication; \
|
||||
return juce::JUCEApplication::main (juce::Process::getCurrentCommandLineParams()); \
|
||||
}
|
||||
#elif ! defined (_AFXDLL)
|
||||
#ifdef _WINDOWS_
|
||||
#define START_JUCE_APPLICATION(AppClass) \
|
||||
static juce::JUCEApplicationBase* juce_CreateApplication() { return new AppClass(); } \
|
||||
int WINAPI WinMain (HINSTANCE, HINSTANCE, LPSTR, int) \
|
||||
{ \
|
||||
juce::JUCEApplication::createInstance = &juce_CreateApplication; \
|
||||
return juce::JUCEApplication::main (juce::Process::getCurrentCommandLineParams()); \
|
||||
}
|
||||
#else
|
||||
#define START_JUCE_APPLICATION(AppClass) \
|
||||
static juce::JUCEApplicationBase* juce_CreateApplication() { return new AppClass(); } \
|
||||
int __stdcall WinMain (void*, void*, const char*, int) \
|
||||
{ \
|
||||
juce::JUCEApplication::createInstance = &juce_CreateApplication; \
|
||||
return juce::JUCEApplication::main (juce::Process::getCurrentCommandLineParams()); \
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue