wxWidgets 中的命令行解析

#include "wx/wx.h"
#include "wx/cmdline.h"

static const wxCmdLineEntryDesc cmdLineDesc[] =
{
{ wxCMD_LINE_SWITCH, wxT("s"), wxT("switch"), wxT("This is a switch.") },
{ wxCMD_LINE_OPTION, wxT("os"), wxT("option-string"), wxT("This is a option of string type."), wxCMD_LINE_VAL_STRING },
{ wxCMD_LINE_OPTION, wxT("on"), wxT("option-number"), wxT("This is a option of number type."), wxCMD_LINE_VAL_NUMBER },
{ wxCMD_LINE_PARAM, NULL, NULL, wxT("required string"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_OPTION_MANDATORY },
{ wxCMD_LINE_PARAM, NULL, NULL, wxT("a variable number of string"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE },
{ wxCMD_LINE_NONE }
};

int _tmain(int argc, wxChar ** argv)
{
wxInitialize();

wxCmdLineParser parser(argc, argv);
parser.SetDesc(cmdLineDesc);

if (parser.Parse() == 0)
{
if (parser.Found(wxT("s")))
wxPuts(wxT("s switch was found."));

wxString str;
if (parser.Found(wxT("os"), &str))
wxPuts(wxT("os option was found: ") + str);

long n;
if (parser.Found(wxT("on"), &n))
wxPuts(wxT("on option was found: ") + str.Format(wxT("%d"), n));

wxPuts(wxT("required string: ") + parser.GetParam());

n = parser.GetParamCount();
wxPuts(wxT("the number of parameters: ") + str.Format(wxT("%d"), n));

wxPuts(wxT("other parameters:"));
for (int i = 1; i < n; i++)
wxPuts(parser.GetParam(i));
}

wxUninitialize();
return 0;
}

留下评论

您的邮箱地址不会被公开。 必填项已用 * 标注