Boost logo

Boost :

From: Alexander Kondratyuk (alexander.kondratyuk_at_[hidden])
Date: 2006-02-15 10:15:04


Have a problem with using additional parser feature:
I want to allow user to use double dashes for short options, so user will
use "-h" or "--h".
But using additional parser the code throws an error about syntax. It seems
there is a bug in additional parser wrapper - in case then my option has no
parameters defined and declared in description it adds an empty value to the
vector of values when additional paser returns pair of strings.

Here is an example that illustrates the problem:

#define BOOST_WHATEVER_DYN_LINK
#define BOOST_LIB_DIAGNOSTIC
#include <boost/program_options/variables_map.hpp>
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/detail/utf8_codecvt_facet.hpp>
#include <boost/token_iterator.hpp>
#include <boost/shared_ptr.hpp>
using namespace boost::program_options;

#include <string>
#include <vector>
using namespace std;

pair<string, string> parserDashDashShort(const string& s)
{
    string str = s.substr( 0, s.find('=') );
    if( str.length()==3 && str[0]=='-' && str[1]=='-' && str[2]<='z' &&
str[2]>='a')
    {
        string val = s.substr(str.length());
        return make_pair( str.substr(1), val );
    }
    return make_pair(string(), string());
}

int _tmain(int , _TCHAR* argv[])
{
    int style =
        command_line_style::allow_short|
        command_line_style::allow_dash_for_short |
        command_line_style::short_allow_adjacent;

   options_description desc;
    desc.add_options()
        ("help,h", "help message")
    ;

    char* args[]={"program.exe", "--h"};
    int argc = sizeof( args ) / sizeof( args[0] );
    parse_command_line<char>( argc, (char**)args, desc, style,
parserDashDashShort );
 return 0;
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk