[wxWidgets]_[0基础]_[不常见但有用的类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




posted @ 2017-05-18 14:07  clnchanpin  阅读(551)  评论(0编辑  收藏  举报