From:  http://www.radmangames.com/programming/how-to-use-boost-program_options

If it so happens that you’re writing a console application, then chances are you will (or should) want to pass it some parameters at the command line. By default C++ has only the argc and argv parameters to the main function to facilitate this and I can assure you that working with them directly is less than fun. If you have worked with any number of command line programs you would be well aware of a common form that they take and that users expect. To get this expected behavior you should use another favourite boost library of mine: Program options.

Quite simply, Boost.Program_options is the way to do industry standard command line processing in C++, if you’re trying to implement command line arguments in your application then you should be using this library. This tutorial will take you through how to get set up and productive using boost program options with a minimum of fuss.

Obtaining, compiling and linking to boost is not covered.

EDIT: Aug 16 2014 
Spurred on by a comment on this post I have written a library that extends Boost.Program_options, providing support for subcommand syntax (think SVN or Mercurial) and including convenient formatting utilities for posix style help output. It's free, open source and awesome! You can check it out here.

Application Template

So you have an program that you want to be able to accept command line parameters? Then replace (or merge) the contents of your main.cpp with the code below. This code forms the foundation of the rest of the tutorial and is boiler plate that you will find yourself writing every time you create a new command line application.

Note: the rest of the examples reference the names of objects in the code below.

#include "boost/program_options.hpp" 
 
#include <iostream> 
#include <string> 
 
namespace 

  const size_t ERROR_IN_COMMAND_LINE = 1; 
  const size_t SUCCESS = 0; 
  const size_t ERROR_UNHANDLED_EXCEPTION = 2; 
 
} // namespace 
 
int main(int argc, char** argv) 

  try 
  { 
    /** Define and parse the program options 
     */ 
    namespace po = boost::program_options; 
    po::options_description desc("Options"); 
    desc.add_options() 
      ("help", "Print help messages") 
      ("add", "additional options") 
      ("like", "this"); 
 
    po::variables_map vm; 
    try 
    { 
      po::store(po::parse_command_line(argc, argv, desc),  
                vm); // can throw 
 
      /** --help option 
       */ 
      if ( vm.count("help")  ) 
      { 
        std::cout << "Basic Command Line Parameter App" << std::endl 
                  << desc << std::endl; 
        return SUCCESS; 
      } 
 
      po::notify(vm); // throws on error, so do after help in case 
                      // there are any problems 
    } 
    catch(po::error& e) 
    { 
      std::cerr << "ERROR: " << e.what() << std::endl << std::endl; 
      std::cerr << desc << std::endl; 
      return ERROR_IN_COMMAND_LINE; 
    } 
 
    // application code here // 
 
  } 
  catch(std::exception& e) 
  { 
    std::cerr << "Unhandled Exception reached the top of main: " 
              << e.what() << ", application will now exit" << std::endl; 
    return ERROR_UNHANDLED_EXCEPTION; 
 
  } 
 
  return SUCCESS; 
 
} // main 

Basic option configuration

All of the below options should be added as additional lines in the

desc.add_options()

call in the application template.

Add the most simple of options

--option

("option", "Info message about option") 

Add a shorthand for an option

--option or -o

("option,o", "Info message about option") // can use -o 

Add an option with shorthand only

-o

(",o", "Info message about option") // must use -o 

NOTE: To access a shorthand only value in a variables_map format the name as you would on the command line e.g. -o. See the Accessing Option Values section for more details.

Add an option that has an associated value

--option <value>

("option", po::value<arg_type>(), "Info message about option") 

Specify that an option is required

The call to

po::notify(vm)

will throw if the option is not specified.

("option", po::value<arg_type>()->required(), "Info message") 

Specify an option that can be specified multiple times

--option <value1> --option <value2> --option <value3>

("option", po::value<std::vector<arg_type> >(), "a list of values") 

Accessing option values

Have the option set directly into an existing variable

I find this to be the most convenient method to use

("option", po::value<arg_type>(&existingVariable), "info message") 

Check if an option was passed in the command line

if ( vm.count("option") ) 

To extract the value for an option manually

vm["option"].as<arg_type>() 

Retrieving shorthand only options

Access to shorthand options is done exactly the same way as described above but the "option" string has to be formatted in a particular way. If there is only a shorthand option then the name for that option is considered to be "-X" where X is the letter being used as the short hand. Another way to remember this is to specify the name as it would appear on the command line.

(",t", "Shorthand only option -t") 
if ( vm.count("-t") ) 

  vm["-t"].as<arg_type>() 

Specifying Positional Options

A positional option is a value given on the command line that is not preceded by an option switch like -o or --option. An example is the following:

user@computer:$ g++ -o main main.cpp

In this case main.cpp is the positional option, and as you can see it is not associated with any option explicitly specified. Instead of being identified by an option switch (like --option) they are identified by the lack of one. When multiple positional options are available the option the value is assigned to is designated by the order in which the values appear in the command line.

user@computer:$ hg help commit -v

In this case 'help' is the first positional option and 'commit' is the second. The command will print help info for the commit subcommand. Now if the order were to be reversed:

user@computer:$ hg commit help -v

The position of the options is now reversed and the command will now try to 'commit' the location 'help'. Again the order in which values are specified determines which positional option the value is assigned to.

Example code for positional options

To specify these with boost program options add a

po::positional_options_description

object and specify the names of the options that you want to parse as positional. Add the following code block directly below the last option in the

desc.add_options()

call:

po::positional_options_description positionalOptions; 
positionalOptions.add("option_name_1", <num_occurrences>); 
positionalOptions.add("option_name_2", <num_occurrences>); 

The option name specified as the first parameter must correspond to the name of an option already specified in the regular options description. The second parameter specifies the number of values to be associated with the option, in the simple case you will always set this to 1. The positional order of options is implied by the order in which they are added to the description, in this case 'option_name_1' is first and 'option_name_2' is second.

Next you need to make sure that the positional options are parsed along with the others and stored in the variable map. Change the

po::store()

call to the following:

po::store(po::command_line_parser(argc, argv).options(desc) 
            .positional(positionalOptions).run(), 
          vm); 

Putting it all together

With just the information above you can create quite sophisticated command line applications, but sometimes knowing the parts does not give a good enough picture of what can be achieved. Check out below for a full example of the options implemented in a demo application. This should help to give you an idea of how things look when it’s all put together. Be aware that I have implemented some custom parsing for printing the available options in posix style which I include as part of the source package for the example application and an example of that usage output is shown immediately below. The information is parsed generically from the specified options and this prevents you from having to manually update help text when you update options (which is pretty awesome).

Best of luck, and please let me know if you have any issues.

Please note you will need version 1.50.0 of boost to compile the extended example

Usage Output

user@computer:$ ./test_bed --help
This is just a template app that should be modified and added to in order to create a useful command line application

USAGE: test_bed [-hvt] [-w ARG] [-m ARG] -n ARG add like

-- Option Descriptions --

Positional arguments:
add "additional options"
like "this"

Option Arguments:
-h [ --help ] Print help messages
-v [ --verbose ] print words with verbosity
-w [ --word ] words for the sentence, specify multiple times
-t just a temp option that does very little
-n [ --necessary ] give me anything
-m [ --manual ] extract value manually

Example Application

#include "PrettyOptionPrinter.hpp" 
 
#include "boost/program_options.hpp" 
#include "boost/filesystem.hpp" 
 
#include <iostream> 
#include <string> 
 
namespace 

  const size_t ERROR_IN_COMMAND_LINE = 1; 
  const size_t SUCCESS = 0; 
  const size_t ERROR_UNHANDLED_EXCEPTION = 2; 
 
} // namespace 
 
//------------------------------------------------------------------------ 
int main(int argc, char** argv) 

  try 
  { 
    std::string appName = boost::filesystem::basename(argv[0]); 
    int add = 0; 
    int like = 0; 
    std::vector<std::string> sentence; 
 
    /** Define and parse the program options 
     */ 
    namespace po = boost::program_options; 
    po::options_description desc("Options"); 
    desc.add_options() 
      ("help,h", "Print help messages") 
      ("verbose,v", "print words with verbosity") 
      ("word,w", po::value<std::vector<std::string> >(&sentence), 
       "words for the sentence, specify multiple times") 
      (",t", "just a temp option that does very little") 
      ("necessary,n", po::value<std::string>()->required(), "necessary!") 
      ("manual,m", po::value<std::string>(), "extract value manually") 
      ("add", po::value<int>(&add)->required(), "additional options") 
      ("like", po::value<int>(&like)->required(), "this"); 
 
    po::positional_options_description positionalOptions; 
    positionalOptions.add("add", 1); 
    positionalOptions.add("like", 1); 
 
    po::variables_map vm; 
 
    try 
    { 
      po::store(po::command_line_parser(argc, argv).options(desc) 
                  .positional(positionalOptions).run(), 
                vm); // throws on error 
 
      /** --help option 
       */ 
      if ( vm.count("help")  ) 
      { 
        std::cout << "This is just a template app that should be modified" 
                  << " and added to in order to create a useful command" 
                  << " line application" << std::endl << std::endl; 
        rad::OptionPrinter::printStandardAppDesc(appName, 
                                                 std::cout, 
                                                 desc, 
                                                 &positionalOptions); 
        return SUCCESS; 
      } 
 
      po::notify(vm); // throws on error, so do after help in case 
                      // there are any problems 
    } 
    catch(boost::program_options::required_option& e) 
    { 
      rad::OptionPrinter::formatRequiredOptionError(e); 
      std::cerr << "ERROR: " << e.what() << std::endl << std::endl; 
      rad::OptionPrinter::printStandardAppDesc(appName, 
                                               std::cout, 
                                               desc, 
                                               &positionalOptions); 
      return ERROR_IN_COMMAND_LINE; 
    } 
    catch(boost::program_options::error& e) 
    { 
      std::cerr << "ERROR: " << e.what() << std::endl << std::endl; 
      rad::OptionPrinter::printStandardAppDesc(appName, 
                                               std::cout, 
                                               desc, 
                                               &positionalOptions); 
      return ERROR_IN_COMMAND_LINE; 
    } 
 
    // can do this without fear because it is required to be present 
    std::cout << "Necessary = " 
              << vm["necessary"].as<std::string>() << std::endl; 
 
    if ( vm.count("verbose") ) 
    { 
      std::cout << "VERBOSE PRINTING" << std::endl; 
    } 
    if (vm.count("verbose") && vm.count("t")) 
    { 
      std::cout << "heeeeyahhhhh" << std::endl; 
    } 
 
    std::cout << "Required Positional, add: " << add 
              << " like: " << like << std::endl; 
 
    if ( sentence.size() > 0 ) 
    { 
      std::cout << "The specified words: "; 
      std::string separator = " "; 
      if (vm.count("verbose")) 
      { 
        separator = "__**__"; 
      } 
      for(size_t i=0; i<sentence.size(); ++i) 
      { 
        std::cout << sentence[i] << separator; 
      } 
      std::cout << std::endl; 
 
    } 
 
    if ( vm.count("manual") ) 
    { 
      std::cout << "Manually extracted value: " 
                << vm["manual"].as<std::string>() << std::endl; 
    } 
 
  } 
  catch(std::exception& e) 
  { 
    std::cerr << "Unhandled Exception reached the top of main: " 
              << e.what() << ", application will now exit" << std::endl; 
    return ERROR_UNHANDLED_EXCEPTION; 
 
  } 
 
  return SUCCESS; 
 
} // main 
 
//------------------------------------------------------------------------ 
 

how to use boost program options的更多相关文章

  1. Boost 库Program Options--第二篇

    程式執行參數處理函式庫:Boost Program Options(2/N) 前一篇已經大致解釋了 Boost Program Options 基本上的使用方法.而這一篇,則來細講一下選項描述(opt ...

  2. boost:库program_options--第一篇

    程式執行參數處理函式庫:Boost Program Options(1/N) 一般程式寫得大一點.或是需要比較有彈性,通常都需要在程式執行的時候,從外部讀取一些參數,來做為內部的設定值.一般來說,比較 ...

  3. Boost简介

    原文链接:  吴豆豆http://www.cnblogs.com/gdutbean/archive/2012/03/30/2425201.html Boost库 Boost库是为C++语言标准库提供扩 ...

  4. Boost 1.61.0 Library Documentation

    http://www.boost.org/doc/libs/1_61_0/ Boost 1.61.0 Library Documentation Accumulators Framework for ...

  5. boost库的安装,使用,介绍,库分类

    1)首先去官网下载boost源码安装包:http://www.boost.org/ 选择下载对应的boost源码包.本次下载使用的是 boost_1_60_0.tar.gz (2)解压文件:tar - ...

  6. [C++Boost]程序参数项解析库Program_options使用指南

    介绍 程序参数项(program options)是一系列name=value对,program_options 允许程序开发者获得通过命令行(command line)和配置文件(config fi ...

  7. boost库中的 program_options

    1.阅读rviz中的源码时在rviz/visualizer_app.cpp中遇到如下代码: po::options_description options; options.add_options() ...

  8. C++ Boost库分类总结

    c# 程序员写c++,各种不适应.尤其是被内存操作和几十种字符串类型的转换,简直疯了,大小写转换竟然要手动写代码实现. Boost看介绍不错,也不知道能不能跨平台.过几天要上linux写c++, 也不 ...

  9. boost 介绍

    简介: Boost库是一个可移植.提供源代码的C++库,作为标准库的后备,是C++标准化进程的开发引擎之一. Boost库由C++标准委员会库工作组成员发起,其中有些内容有望成为下一代C++标准库内容 ...

随机推荐

  1. LeetCode(42):接雨水

    Hard! 题目描述: 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水. 上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度 ...

  2. 【python】已安装模块提示ImportError: No module named

    今天遇到了一个问题,运行代码时出现错误 Traceback (most recent call last): File , in <module> import zmq ImportErr ...

  3. zoj3469 区间dp好题

    /* 按坐标排序 以餐厅为起点向两边扩展区间 dp[i][j][0]表示送完区间[i,j]的饭后停留在左边的代价 dp[i][j][1]表示送完区间[i,j]的饭后停留在右边的代价 */ #inclu ...

  4. intellij idea svn 修改文件后,父文件夹也标注修改

    svn文件修改后,默认只有当前文件更改而父文件没有标注,很不直观:查了一顿后,发现,可以设置: File—->settings—->version control—–>勾选show ...

  5. Delphi---TServerSocket和TClientSocket发送和接收大数据包

    https://www.cnblogs.com/zhangzhifeng/p/6065244.html TServerSocket和TClientSocket用非阻塞模式发送和接收比较大的数据时,可能 ...

  6. 【AtCoder】ARC081

    C - Make a Rectangle 每次取两个相同的且最大的边,取两次即可 #include <bits/stdc++.h> #define fi first #define se ...

  7. Kubernetes核心概念总结

    目录贴:Kubernetes学习系列 1.基础架构 1.1 Master Master节点上面主要由四个模块组成:APIServer.scheduler.controller manager.etcd ...

  8. POJ1456 Supermarket 并查集

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ1456 题意概括  一家超市,要卖出N种物品(每种物品各一个),每种物品都有一个卖出截止日期Di(在该 ...

  9. window.location方法获取URL

    window.location方法获取URL 统一资源定位符 (Uniform Resource Locator, URL) 完整的URL由这几个部分构成: scheme://host:port/pa ...

  10. hdu1576(扩展欧几里得)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576 题目:要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能 ...