Boost logo

Boost Users :

From: Vladimir Prus (ghost_at_[hidden])
Date: 2006-05-19 04:06:48

Christoph Duelli wrote:

> I am trying to parse my options into a QString.
> I have provided streaming operators for QString, so code like the
> following does compile
> QString sProgram;
> desc.add_options()
> ("program,p", po::value<QString>(&sProgram), "...");
> It does even work if I pass 'simple' options:
> -p test will work
> -p "test oops" will not
> The cause for that seems to be that the lexical_cast<QString>("test oops")
> (in program_options) fails (it does fail).
> If I use std::string the above does work.
> Seems providing streaming ops was not enough.
> QString s;
> "a b" >> s;
> now s=="a"
> lexical_cast has special support for string/wstring according to its docs.
> I need something similar for QString...

Hi Christoph,
so the issue is that your stream operator for QString stops on whitespace,
right? And lexical_cast has special workaround to make string->string an
identify conversion and you want string->QString convertion to just call
QString's construct or QString::fromLocal8Bit?

I'm afraid the current design of lexical_cast does not supports this. You've
two approaches:

1. Get lexical_cast fixed somehow. But it's in this state for years...
2. Customize for QStrings at program_options level:

void validate(boost::any& v,
              const std::vector<std::string>& values,
              QString*, int)
    using namespace boost::program_options;

    // Make sure no previous assignment to 'a' was made.

    // Extract the first string from 'values'. If there is more than
    // one string, it's an error, and exception will be thrown.
    const string& s = validators::get_single_string(values);

    v = any(QString::fromLocal8Bit(s));

This is a less nice that providing operator>>, but I think it's the only
solution now.


Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at