zoukankan      html  css  js  c++  java
  • program_options

    program_options

      The program_options library allows program developers to obtain program options, that is (name, value) pairs from the user, via conventional methods such as command line and config file.

    1、添加 options_description.

    // Declare the supported options.
    po::options_description desc("Allowed options");
    desc.add_options()
        ("help", "produce help message")
        ("compression", po::value<int>(), "set compression level")
    ;
    
    po::variables_map vm;
    po::store(po::parse_command_line(ac, av, desc), vm);
    po::notify(vm);
    
    if (vm.count("help")) {
        cout << desc << "
    ";
        return 1;
    }
    
    if (vm.count("compression")) {
        cout << vm["compression"].as<int>() << ".
    ";
    }

      使用示例:

      

    2、po::value

    int opt;
    po::options_description desc("Allowed options");
    desc.add_options()
        ("help", "produce help message")
        ("optimization", po::value<int>(&opt)->default_value(10), 
      "optimization level")
        ("include-path,I", po::value< vector<string> >(), 
      "include path")
        ("input-file", po::value< vector<string> >(), "input file")
    ;

      使用示例:
      

    3、po::positional_options_description

    po::positional_options_description p;
    p.add("input-file", -1);
    
    po::variables_map vm;
    po::store(po::command_line_parser(ac, av).
              options(desc).positional(p).run(), vm);
    po::notify(vm);

      

    4、config file

         int opt;
            string config_file;
        
            // Declare a group of options that will be 
            // allowed only on command line
            po::options_description generic("Generic options");
            generic.add_options()
                ("version,v", "print version string")
                ("help", "produce help message")
                ("config,c", po::value<string>(&config_file)->default_value("multiple_sources.cfg"),
                      "name of a file of a configuration.")
                ;
        
            // Declare a group of options that will be 
            // allowed both on command line and in
            // config file
            po::options_description config("Configuration");
            config.add_options()
                ("optimization", po::value<int>(&opt)->default_value(10), 
                      "optimization level")
                ("include-path,I", 
                     po::value< vector<string> >()->composing(), 
                     "include path")
                ;
    
            // Hidden options, will be allowed both on command line and
            // in config file, but will not be shown to the user.
            po::options_description hidden("Hidden options");
            hidden.add_options()
                ("input-file", po::value< vector<string> >(), "input file")
                ;
    
            
            po::options_description cmdline_options;
            cmdline_options.add(generic).add(config).add(hidden);
    
            po::options_description config_file_options;
            config_file_options.add(config).add(hidden);
    
            po::options_description visible("Allowed options");
            visible.add(generic).add(config);
            
            po::positional_options_description p;
            p.add("input-file", -1);
            
            po::variables_map vm;
            store(po::command_line_parser(ac, av).
                  options(cmdline_options).positional(p).run(), vm);
            notify(vm);
            
            ifstream ifs(config_file.c_str());
            if (!ifs){
                cout << "can not open config file: " << config_file << "
    ";
                return 0;
            }
            else{
                store(parse_config_file(ifs, config_file_options), vm);
                notify(vm);
            }
        
            if (vm.count("help")) {
                cout << visible << "
    ";
                return 0;
            }
    
            if (vm.count("version")) {
                cout << "Multiple sources example, version 1.0
    ";
                return 0;
            }
    
            if (vm.count("include-path")){
                cout << vm["include-path"].as< vector<string> >() << "
    ";
            }
    
            if (vm.count("input-file")){
                cout << vm["input-file"].as< vector<string> >() << "
    ";
            }
    
            cout << opt << "
    ";      

      

    参考:

    1、https://www.boost.org/doc/libs/1_69_0/doc/html/program_options.html

  • 相关阅读:
    Linux中使用dd制作文件的.img
    python正则表达式
    使用@property
    Win10添加删除虚拟打印机方法
    jenkins权限
    RedHat7.2下Jenkins的安装配置
    jenkins忘记管理员账号密码的补救方法
    RHEL软件安装
    docker 常用指令(RHLE)
    /var/run/yum.pid 已被锁定,PID 为 4242 的另一个程序正在运行
  • 原文地址:https://www.cnblogs.com/tekkaman/p/10183724.html
Copyright © 2011-2022 走看看