Reputation: 576
Is there a way I can read the command line arguments passed into a C++ wxWidgets application? If so, could you please provide an example of how to do so.
Upvotes: 7
Views: 7883
Reputation: 18631
Have a look at these examples (1, 2) or:
int main(int argc, char **argv)
{
wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, "program");
wxInitializer initializer;
if (!initializer)
{
fprintf(stderr, "Failed to initialize the wxWidgets library, aborting.");
return -1;
}
static const wxCmdLineEntryDesc cmdLineDesc[] =
{
{ wxCMD_LINE_SWITCH, "h", "help", "show this help message",
wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
// ... your other command line options here...
{ wxCMD_LINE_NONE }
};
wxCmdLineParser parser(cmdLineDesc, argc, wxArgv);
switch ( parser.Parse() )
{
case -1:
wxLogMessage(_T("Help was given, terminating."));
break;
case 0:
// everything is ok; proceed
break;
default:
wxLogMessage(_T("Syntax error detected, aborting."));
break;
}
return 0;
}
Upvotes: 1
Reputation: 2626
In plain C++, there is argc
and argv
. When you are building a wxWidgets application, you can access these using wxApp::argc
, wxApp::argv[]
or wxAppConsole::argc
, wxAppConsole::argv[]
. Note that wxApp
is derived from wxAppConsole
, so either works depending on if you have a console app or GUI app. See wxAppConsole
IMPLEMENT_APP(MyApp)
bool MyApp::OnInit() {
// Access command line arguments with wxApp::argc, wxApp::argv[0], etc.
// ...
}
You may also be interested in wxCmdLineParser.
Upvotes: 9
Reputation: 1821
You can access the command line variables from your wxApp
as it inherites from wxAppConsole
which provides wxAppConsole::argc
and wxAppConsole::argv.
Upvotes: 1