• [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
    




  • 相关阅读:
    简单的总结
    /bin/bash: sshpass: command not found
    ubuntu 16.04 忘记root密码
    ubuntu下QtCreator启动无响应问题解决
    TX1 文字界面启动与root用户自动登录设置
    qt线程睡眠
    TX2 默认root用户启动
    linux下使用文件IO监听GPIO中断
    Linux下指定线程的名字
    [Error] ISO C++ forbids comparison between pointer and integer
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/6873060.html
Copyright © 2020-2023  润新知