Boost logo

Boost :

From: Janek Kozicki (janek_listy_at_[hidden])
Date: 2006-07-18 07:12:08


Hello

I've been writing a small program that expects certain number arguments
from command line for any given option. <getopt.h> supports only single
argument for each option, while I needed to supply three arguments, like
that:

  ./program -f foo bar baz

The easiest solution was to use Boost.Tokenizer library, which indeed is
a very small and nify tool. However I was surprised to discover that
Boost.Tokenizer is exception unaware!

Consider following two examples. One with current version of
Boost.Tokenizer, that is exception unaware. Please note that the code
looks a bit dirty, and extra bool flag is needed.

#include<iostream>
#include<boost/tokenizer.hpp>
#include<string>

int main()
{
        std::string arguments = "foo bar";
        // not receiveing all three arguments is an exception.
        std::string arg1,arg2,arg3;

        boost::tokenizer<> tok(arguments);
        boost::tokenizer<>::iterator pos=tok.begin();

        bool error=false;
        if(pos!=tok.end()) arg1=*pos++; else error=true;
        if(pos!=tok.end()) arg2=*pos++; else error=true;
        if(pos!=tok.end()) arg3=*pos++; else error=true;

        if(error)
        {
                std::cout << "not enough arguments\n";
                exit(1);
        }

        std::cout << arg1 << " " << arg2 << " " << arg3 << "\n";
}

Without all those checks if(pos!=tok.end()), the program crashes on assert_fail().

This is how the code would look like, if Boost.Tokenizer was correctly
throwing exceptions:

#include<iostream>
#include<boost/tokenizer.hpp>
#include<string>

int main()
{
        std::string arguments = "foo bar baz";
        // not receiveing all three arguments is an exception.
        std::string arg1,arg2,arg3;

        boost::tokenizer<> tok(arguments);
        boost::tokenizer<>::iterator pos=tok.begin();
        try
        {
                arg1=*pos++;
                arg2=*pos++;
                arg3=*pos++;
        }
        catch(boost::tokenizer_out_of_range&)
        {
                std::cout << "not enough arguments\n";
                exit(1);
        }

        std::cout << arg1 << " " << arg2 << " " << arg3 << "\n";
}

Questions:

- is this IMO bad design intentional?

- if it's not, I'll gladly make a patch for that - then would there be a
  chance that it will get accepted? ;)

-- 
Janek Kozicki                                                         |

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