[wxWidgets]_[初级]_[不常见但实用的类wxCmdLineParser]

场景:

1. 有时候需要构造命令行字符串传递給函数调用,比如CreateProcess,如果参数是动态的,那么就得使用类似std::vector<string>添加单个参数,之后拼接为一个string.

wx有给这类处理类,就是 wxCmdLineParser ,它既能拼接为字符串,又可以把字符串分解为一个参数数组。

console.cpp

#include "wx/wxprec.h"

#ifndef WX_PRECOMP
	#include "wx/wx.h"
#endif

#include "wx/cmdline.h"
#include "wx/app.h" 
#include <assert.h>
#include <iostream>
#include <stdio.h>


using namespace std;

void TestParseCmdLine()
{
	cout << "TestParseCmdLine" << endl;
	wxString cmd("gcc -c -o test.exe test.cpp");
	//1.解析字符串
	wxCmdLineParser parser;
	//parser.SetCmdLine(0,(char**)NULL);
	wxArrayString arr = parser.ConvertStringToArgs(cmd);
	for(int i = 0; i < arr.size(); ++i)
	{
		cout << arr[i] << endl;
	}
	//2.生成字符串
	
	parser.AddParam(wxT("gcc"));
	parser.AddParam(wxT("-c"));
	parser.AddParam(wxT("-o"));
	parser.AddParam(wxT("test.exe"));
	parser.AddParam(wxT("test.cpp"));
	//parser.Reset();
	//parser.Parse();
	cout << "parser.GetParamCount(): " << parser.GetParamCount() << endl;
	for(int i = 0; i < parser.GetParamCount(); ++i)
	{
		cout << parser.GetParam(i) << endl;
	}
	cout << "argv: " << wxTheApp->GetAppName() << endl;
	wxString str = parser.GetUsageString();
	wxString appName = wxTheApp->GetAppName();
	cout << "parser: " << str.Mid(str.Find(wxTheApp->GetAppName())+appName.Length()+1) << endl;
}

int main(int argc,char* argv[])
{
	wxInitialize();
	wxEntryStart(argc,argv);
	wxInitAllImageHandlers();

	TestParseCmdLine();

	wxEntryCleanup();
	wxUninitialize();
	return 0;
}

输出:

TestParseCmdLine
gcc
-c
-o
test.exe
test.cpp
parser.GetParamCount(): 0
argv: test_wxCmdLineParser
parser: gcc -c -o test.exe test.cpp
我来评几句
登录后评论

已发表评论数()

相关站点

+订阅
热门文章