Parsing command line arguments in a unicode C++ application

前端 未结 4 1552
春和景丽
春和景丽 2021-01-02 05:37

How can I parse integers passed to an application as command line arguments if the app is unicode?

Unicode apps have a main like this:

int _tmain(int         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-02 06:22

    Dry coded and I don't develop on Windows, but using TCLAP, this should get you running with wide character argv values:

    #include 
    
    #ifdef WINDOWS
    # define TCLAP_NAMESTARTSTRING "~~"
    # define TCLAP_FLAGSTARTSTRING "/"
    #endif
    #include "tclap/CmdLine.h"
    
    int main(int argc, _TCHAR *argv[]) {
      int myInt = -1;
      try {
        TCLAP::ValueArg intArg;
        TCLAP::CmdLine cmd("this is a message", ' ', "0.99" );
        cmd.add(intArg);
        cmd.parse(argc, argv);
        if (intArg.isSet())
          myInt = intArg.getValue();
      } catch (TCLAP::ArgException& e) {
        std::cout << "ERROR: " << e.error() << " " << e.argId() << endl;
      }
      std::cout << "My Int: " << myInt << std::endl;
      return 0;
    }
    

提交回复
热议问题