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.
Asked
Active
Viewed 6,985 times
6
-
Your `main` function has two arguments, normally called `argc` and `argv`. Those are the command line arguments passed to your application, and which you pass on to wxWidgets. Just check those arguments to `main` yourself if you need to. – Some programmer dude Apr 12 '12 at 06:22
-
@JoachimPileborg: I am not sure if you are familiar with how wxWidgets programs initialized, however, they typically use wxFrame and wxApplication windows and you don't plainly have access to those two variables. – jack Apr 12 '12 at 06:28
-
If there's no methods in the application class to get the parameters, maybe you have to re-implement the `IMPLEMENT_APP` macro, to get the arguments from there. – Some programmer dude Apr 12 '12 at 06:31
3 Answers
7
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.

JohnPS
- 2,518
- 19
- 17
1
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;
}
1
You can access the command line variables from your wxApp
as it inherites from wxAppConsole
which provides wxAppConsole::argc
and wxAppConsole::argv.

SteveL
- 1,811
- 1
- 13
- 23