[英]Command line arguments in wxWidgets
Is there a way I can read the command line arguments passed into a C++ wxWidgets application? 有没有办法可以读取传递给C ++ wxWidgets应用程序的命令行参数? If so, could you please provide an example of how to do so.
如果是这样,请您提供一个如何操作的示例。
In plain C++, there is argc
and argv
. 在普通的C ++中,有
argc
和argv
。 When you are building a wxWidgets application, you can access these using wxApp::argc
, wxApp::argv[]
or wxAppConsole::argc
, wxAppConsole::argv[]
. 在构建wxWidgets应用程序时,可以使用
wxApp::argc
, wxApp::argv[]
或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. 请注意,
wxApp
派生自wxAppConsole
,因此根据您是否拥有控制台应用程序或GUI应用程序而工作。 See wxAppConsole 请参阅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 . 您可能也对wxCmdLineParser感兴趣。
Have a look at these examples ( 1 , 2 ) or: 看看这些例子( 1 , 2 )或:
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;
}
您可以从您访问命令行变量wxApp
,因为它从inherites wxAppConsole
提供wxAppConsole :: ARGC和wxAppConsole :: argv的 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.