Boost logo

Boost Users :

Subject: Re: [Boost-users] commandline args
From: Diederick C. Niehorster (dcnieho_at_[hidden])
Date: 2009-08-24 00:14:43


Oops, the situation is not as bad as i noted in my last message,

bk::generate(ostream_iterator<char>(cout), "Command line options not
recognized:\n" << (" \"" << ba::string % ("\"" << bk::eol)) << "\""
<< bk::eol, vsUnrecog);

Does almost what it should, except the prefix is only diplayed for the
first item in the list (it thus doesn't seem to belong to the list.

Output wanted:
Options not recognized:
 "dd"
 "-e"
 "rrty"

Output got:
Options not recognized:
 "dd"
-e"
rrty"

It seems unneccesarily complex though, and I have to put "\"" <<
bk::eol in the code twice, is that really necessary?

Thank you for your time,
Best,
Diederick

On Mon, Aug 24, 2009 at 12:03 PM, Diederick C.
Niehorster<dcnieho_at_[hidden]> wrote:
> Hi Harmut,
>
> Thank you for the answers. However, I am still having some trouble and
> i figure it out following the documentation on the list operator.
>
> I am trying to produce an output like below:
> Options not recognized:
>  "dd"
>  "-e"
>  "rrty"
>
> However,
> bk::generate(ostream_iterator<char>(cout), "Command line options not
> recognized:\n" << ("  \"" << ba::string % "\"" << bk::eol) << bk::eol,
> vsUnrecog);
>
> does not do the trick and
>
> bk::generate(ostream_iterator<char>(cout), "Command line options not
> recognized:\n" << ("  \"" % ba::string % "\"" << bk::eol) << bk::eol,
> vsUnrecog);
>
> fails with "..\Dependency\boost\boost/spirit/home/karma/string/lit.hpp(108)
> : error C2664: 'boost::mpl::assertion_failed' : cannot convert
> parameter 1 from 'boost::mpl::failed ************(__thiscall
> boost::spirit::karma::any_string<CharEncoding,Tag>::generate::string_not_usable_without_attribute::*
> ***********)(void)' to 'boost::mpl::assert<false>::type'" (which I
> guess is not the way it should fail? Shouldnt there be a nice
> assertion message or is this it? no experience with it really)
>
> What am I missing here?
>
> Best and thanks,
> Dee
>
> On Mon, Aug 24, 2009 at 11:21 AM, Hartmut
> Kaiser<hartmut.kaiser_at_[hidden]> wrote:
>>> Really? Thank you for that tip! That brings me to a few questions (I
>>> was just testing it and couldn't get it to work quickly). What is the
>>> sink? I think i can just replace that with cout, right? Actually, no,
>>> that gets me an compile error
>>> "boost/spirit/home/karma/detail/output_iterator.hpp(295) : error
>>> C2675: unary '++' : 'std::ostream' does not define this operator or a
>>> conversion to a type acceptable to the predefined operator".
>>>
>>> ok, what i got now is:
>>>
>>> #include <boost/spirit/include/karma.hpp>
>>> namespace bk  = boost::spirit::karma;
>>> namespace ba  = boost::spirit::ascii;
>>> vector<string> vsUnrecog =
>>> collect_unrecognized(parsed.options,po::exclude_positional);
>>> bk::generate(cout, "Option \"" << ba::string % '\" not recognized' <<
>>> bk::eol, vsUnrecog);
>>>
>>> Where do those sinks come from and how do I make a simple sink that
>>> writes to stdout, just like cout does?
>>
>> I used 'sink' as a placeholder for any output iterator, so in your case the easiest is to construct an ostream_iterator<char>(cout).
>>
>>> By the way, doen't your code both use ";" and newline (eol) as
>>> delimiters?
>>
>> ';' is used as the delimiter in between the elements of the vector (string % ';': in Spirit operator% is used as the shortcut for 'list'), and eol is used as the closing newline.
>>
>> Regards Hartmut
>>
>>>
>>> Best,
>>> Dee
>>>
>>>
>>> On Mon, Aug 24, 2009 at 10:42 AM, OvermindDL1<overminddl1_at_[hidden]>
>>> wrote:
>>> > On Sun, Aug 23, 2009 at 8:20 PM, Diederick C.
>>> > Niehorster<dcnieho_at_[hidden]> wrote:
>>> >>> The same way that you would stream any normal vector:
>>> >>> vector<string> &files = vm["file"].as< vector<string> >();
>>> >>> vector<string>::const_iterator iter = files.begin();
>>> >>> cout << "Files selected: ";
>>> >>> for(;iter!=files.end();++iter)
>>> >>> {
>>> >>>    cout << *iter << ";";
>>> >>> }
>>> >>> cout << "\n";
>>> >>>
>>> >>
>>> >>> Consequently, if you have Boost trunk installed, then you could do
>>> >>> this as well since it does support streaming vectors:
>>> >>> #include <boost/spirit/include/karma.hpp>
>>> >>> using namespace boost::spirit::karma;
>>> >>> using namespace boost::spirit::ascii;
>>> >>>
>>> >>> generate(cout, "Files selected: " << (*char_)%';' << eol,
>>> >>> vm["file"].as< vector<string> >());
>>> >>
>>> >> Or you could simply do:
>>> >> #include <boost/foreach.hpp>
>>> >> cout << "Files selected: ";
>>> >> BOOST_FOREACH(string& sFile, vm["file"].as< vector<string> >())
>>> >>            cout << sFile.c_str() << endl;
>>> >
>>> > That is assuming you wanted an endl at the end of each name, a usual
>>> > delimiter is something like ';' or so, so this instead:
>>> > cout << "Files selected: ";
>>> > BOOST_FOREACH(string& sFile, vm["file"].as< vector<string> >())
>>> >           cout << sFile.c_str() << ";";
>>> > cout << "\n";
>>> >
>>> > Although the karma version that Hartmut posted is still the fastest
>>> > way to do it (in terms of execution speed):
>>> > generate(sink,
>>> >   "Files selected: " << string % ';' << eol,
>>> >   vm["file"].as< vector<string> >());
>>> > _______________________________________________
>>> > Boost-users mailing list
>>> > Boost-users_at_[hidden]
>>> > http://lists.boost.org/mailman/listinfo.cgi/boost-users
>>> >
>>> _______________________________________________
>>> Boost-users mailing list
>>> Boost-users_at_[hidden]
>>> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>>
>> _______________________________________________
>> Boost-users mailing list
>> Boost-users_at_[hidden]
>> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>>
>


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net