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.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答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;
}
回答2:
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.
回答3:
You can access the command line variables from your wxApp
as it inherites from wxAppConsole
which provides wxAppConsole::argc
and wxAppConsole::argv.