boost::program_options demo

A Simple Demo

  1. //
  2. // Created by Water on 2018/6/9.
  3. //
  4.  
  5. #include <iostream>
  6. #include <string>
  7. #include <boost/program_options.hpp>
  8. namespace  bpo = boost::program_options;
  9.  
  10. int main(int argc, char const *argv[])
  11. {
  12.     bpo::options_description opts("all options");
  13.     bpo::variables_map vm;
  14.  
  15.     opts.add_options()
  16.             ("filename", bpo::value<std::string>(), "the file name which want to be found")
  17.             ("help", "this is a program to find a specified file");
  18.  
  19.     try{
  20.         bpo::store(bpo::parse_command_line(argc, argv, opts), vm);
  21.     }
  22.     catch(...){
  23.         std::cout << "undefined option!\n";
  24.         return 0;
  25.     }
  26.  
  27.     if(vm.count("help") ){
  28.         std::cout << opts << std::endl;
  29.     }
  30.     if(vm.count("filename") ){
  31.         std::cout << "find " << vm["filename"].as<std::string>() << std::endl;
  32.     }
  33.     if(vm.empty() ){
  34.         std::cout << "no options found \n";
  35.     }
  36.     return 0;
  37. }

Test

How to Set Default Value

  1. //
  2. // Created by Water on 2018/6/9.
  3. //
  4.  
  5. #include <iostream>
  6. #include <vector>
  7. #include <string>
  8. #include <boost/program_options.hpp>
  9. namespace bpo = boost::program_options;
  10.  
  11. int main(int argc, char const *argv[])
  12. {
  13.     std::vector<std::string> v_para;
  14.     std::string port;
  15.     int baud = 0;
  16.     int timeout = 0;
  17.     bpo::options_description opt("all options");
  18.  
  19.     opt.add_options()
  20.             ("v_para,v", bpo::value<std::vector<std::string> >()->multitoken(), "Serial v_para")
  21.             ("port,p", bpo::value<std::string>(&port)->default_value("COM1"), "Serial Port")
  22.             ("baud,b", bpo::value<int>(&baud)->default_value(115200), "Serial Baud")
  23.             ("timeout,t", bpo::value<int>(&timeout)->default_value(5000), "Timeout")
  24.             ("help", "a demo ( default value )");
  25.  
  26.     bpo::variables_map vm;
  27.  
  28.     try{
  29.         bpo::store(parse_command_line(argc, argv, opt), vm);
  30.     }
  31.     catch(...){
  32.         std::cout << "undefined option!\n";
  33.         return 0;
  34.     }
  35.  
  36.     bpo::notify(vm);
  37.  
  38.     if(vm.count("help") ){
  39.         std::cout << opt << std::endl;
  40.         return 0;
  41.     }
  42.     if(vm.count("v_para") ){
  43.         std::cout << "v_para:";
  44.         for(auto& str : vm["v_para"].as<std::vector<std::string> >() )
  45.             std::cout << str << " ";
  46.         std::cout << std::endl;
  47.     }
  48.     std::cout << "port:" << port << std::endl;
  49.     std::cout << "baud:" << baud << std::endl;
  50.     std::cout << "timeout:" << timeout << std::endl;
  51.     return 0;
  52. }

Test

QT5 International

Create Project & Generate .ts

  1. lupdate TestInternational.pro -ts zh_CN.ts

Translate with Linguist

Save & Release to Generate .qm

Add Code to Choose Language

  1. #include "widget.h"
  2. #include <QApplication>
  3.  
  4. #include <QTranslator>
  5.  
  6. #include <QDebug>
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10.     QApplication a(argc, argv);
  11.  
  12.     QTranslator trans;
  13.      if(trans.load(a.applicationDirPath() + "/../../../zh_CN.qm"))
  14.         a.installTranslator(&trans);
  15.  
  16.     Widget w;
  17.     w.show();
  18.  
  19.     return a.exec();
  20. }

The Result

Google ProtoBuf C++ Example

SerializeToString & SerializeToArray

  1. syntax = "proto3";
  2.  
  3. package test;
  4.  
  5. message People {
  6.   string name = 1;
  7.   int32 age = 2;
  8.   int32 steps = 3;
  9. }
protoc Usage :
  1. water@waters-MacBook-Pro:~/Documents/GitHub/CppQt5/TestProtobuf$ protoc --cpp_out=. people.proto
  2. water@waters-MacBook-Pro:~/Documents/GitHub/CppQt5/TestProtobuf$ ls people.p*
  3. people.pb.cc people.pb.h  people.proto
  1. #include <iostream>
  2.  
  3. #include "people.pb.h"
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7.     test::People pep;
  8.     pep.set_name("heheda");
  9.     pep.set_age(5);
  10.     pep.set_steps(1304);
  11.  
  12.     std::string buff;
  13.     pep.SerializeToString(&buff);
  14.  
  15.     test::People pep2;
  16.     pep2.ParseFromString(buff);
  17.  
  18.     std::cout << pep2.name() << std::endl;
  19.     std::cout << pep2.age() << std::endl;
  20.     std::cout << pep2.steps() << std::endl;
  21.  
  22.     int data_len = pep.ByteSize();
  23.     unsigned char data[1024] = "\0";
  24.     pep.SerializeToArray(data, data_len);
  25.     for(auto i = 0; i < data_len; i ++)
  26.         printf("%02X ", data[i]);
  27.     printf("\n");
  28.  
  29.     test::People pep3;
  30.     pep3.ParseFromArray(data, data_len);
  31.  
  32.     std::cout << pep3.name() << std::endl;
  33.     std::cout << pep3.age() << std::endl;
  34.     std::cout << pep3.steps() << std::endl;
  35.  
  36.     return 0;
  37. }

How to build Google’s protobuf in MacOS

Build

  1. water@waters-MacBook-Pro:~/Downloads/protobuf-3.5.1$ ./autogen.sh
  2. water@waters-MacBook-Pro:~/Downloads/protobuf-3.5.1$ ./configure
  3. water@waters-MacBook-Pro:~/Downloads/protobuf-3.5.1$ make
  4. water@waters-MacBook-Pro:~/Downloads/protobuf-3.5.1$ make install
  5. water@waters-MacBook-Pro:~/Downloads/protobuf-3.5.1$ protoc --version
  6. libprotoc 3.5.1