[Boost-users] [boost] [program options] Recommended way to specify a list of strings

1,428 views
Skip to first unread message

Sanjit Jhala

unread,
Dec 21, 2009, 5:45:08 PM12/21/09
to boost...@lists.boost.org
Say I want to specify a list of servers via the boost program_options library. Something like:
--total-servers N --server srv1 --server srv2 ... --server srvN

How would I do this? It doesn't look like multitoken would work for this case, so would it be better for me to just have something like:
--total-servers N --serverlist srv1 srv2 ... srvN

and use a single string to store the serverlist and parse it myself?

-Sanjit

Ryan Gallagher

unread,
Dec 21, 2009, 7:21:32 PM12/21/09
to boost...@lists.boost.org
Sanjit Jhala <sjhalaz <at> gmail.com> writes:
> Say I want to specify a list of servers via the boost program_options
> library. Something like:
> --total-servers N --server srv1 --server srv2 ... --server srvN
> How would I do this?

Have you tried sending a std::vector<std::string> as the option value?

("server", po::value< vector<string> >()

See the "Option Details" section in the tutorial:

http://tinyurl.com/yd95h2r

HTH,

-Ryan

_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Sanjit Jhala

unread,
Dec 21, 2009, 7:37:53 PM12/21/09
to boost...@lists.boost.org
Thanks Ryan, this should work for me.

-Sanjit

Sanjit Jhala

unread,
Dec 22, 2009, 9:29:30 PM12/22/09
to boost...@lists.boost.org
How would I specify an empty vector of strings as the default value for this option?
po::value< vector<string> >()->default_value(??)

-Sanjit

Vladimir Prus

unread,
Dec 23, 2009, 1:04:19 AM12/23/09
to boost...@lists.boost.org
Sanjit Jhala wrote:

> How would I specify an empty vector of strings as the default value for this
> option?
>
> po::value< vector<string> >()->default_value(??)

Use vector<string>() as the default value.

- Volodya

TSalm

unread,
Dec 30, 2009, 1:10:24 PM12/30/09
to boost...@lists.boost.org
Le Wed, 23 Dec 2009 07:04:19 +0100, Vladimir Prus
<vlad...@codesourcery.com> a écrit:

> Sanjit Jhala wrote:
>
>> How would I specify an empty vector of strings as the default value for
>> this
>> option?
>>
>> po::value< vector<string> >()->default_value(??)
>
> Use vector<string>() as the default value.
>

Hi,

I have the same problem.
But when I'm using :

configuration.add_options()
("DEFAULT.bytecompile",program_options::value< vector<string>
>()->default_value(vector<string>()))
;

G++ won't compile.

Here is the complete source code :


#include <vector>
#include <boost/program_options.hpp>


int main(int argc, char** argv)
{
using namespace std;
using namespace boost;

program_options::options_description configuration;
boost::program_options::variables_map vm;
vector<string> empty_vector;

// Set options and default values
configuration.add_options()
("DEFAULT.bytecompile",program_options::value< vector<string>
>()->default_value(vector<string>()))
;

program_options::store (
program_options::parse_command_line(argc, argv, configuration)
, vm );

program_options::notify( vm );

return 0;
}


And the error message :

/usr/include/boost/lexical_cast.hpp: In member function ‘bool
boost::detail::lexical_stream<Target, Source>::operator<<(const Source&)
[with Target = std::basic_string<char, std::char_traits<char>,
std::allocator<char> >, Source = std::vector<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >,
std::allocator<std::basic_string<char, std::char_traits<char>,
std::allocator<char> > > >]’:
/usr/include/boost/lexical_cast.hpp:222: instantiated from ‘Target
boost::lexical_cast(const Source&) [with Target = std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, Source =
std::vector<std::basic_string<char, std::char_traits<char>,
std::allocator<char> >, std::allocator<std::basic_string<char,
std::char_traits<char>, std::allocator<char> > > >]’
/usr/include/boost/program_options/value_semantic.hpp:190: instantiated
from ‘boost::program_options::typed_value<T, charT>*
boost::program_options::typed_value<T, charT>::default_value(const T&)
[with T = std::vector<std::basic_string<char, std::char_traits<char>,
std::allocator<char> >, std::allocator<std::basic_string<char,
std::char_traits<char>, std::allocator<char> > > >, charT = char]’
config_file__vector.cpp:16: instantiated from here
/usr/include/boost/lexical_cast.hpp:151: error: no match for ‘operator<<’
in ‘((boost::detail::lexical_stream<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >,
std::vector<std::basic_string<char, std::char_traits<char>,
std::allocator<char> >, std::allocator<std::basic_string<char,
std::char_traits<char>, std::allocator<char> > > >
>*)this)->boost::detail::lexical_stream<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >,
std::vector<std::basic_string<char, std::char_traits<char>,
std::allocator<char> >, std::allocator<std::basic_string<char,
std::char_traits<char>, std::allocator<char> > > > >::stream << input’
/usr/include/c++/4.3/ostream:112: note: candidates are:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(std::basic_ostream<_CharT, _Traits>&
(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/4.3/ostream:121: note:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(std::basic_ios<_CharT, _Traits>&
(*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/4.3/ostream:131: note:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT =
char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/ostream:169: note:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(long int) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/4.3/ostream:173: note:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/4.3/ostream:177: note:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(bool) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/4.3/bits/ostream.tcc:97: note:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(short int) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/4.3/ostream:184: note:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/4.3/bits/ostream.tcc:111: note:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(int) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/4.3/ostream:195: note:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(unsigned int) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/4.3/ostream:204: note:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(long long int) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/4.3/ostream:208: note:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits
= std::char_traits<char>]
/usr/include/c++/4.3/ostream:213: note:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(double) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/4.3/ostream:217: note:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(float) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/4.3/ostream:225: note:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(long double) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/4.3/ostream:229: note:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(const void*) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/4.3/bits/ostream.tcc:125: note:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT
= char, _Traits = std::char_traits<char>]

--
Utilisant le client e-mail révolutionnaire d'Opera :
http://www.opera.com/mail/

TSalm

unread,
Jan 1, 2010, 11:20:54 AM1/1/10
to boost...@lists.boost.org

Ok. The solution is to specify a "textual" parameter :


("DEFAULT.bytecompile",program_options::value< vector<string>

>()->default_value(vector<string>(),"")


/** Specifies default value, which will be used
if none is explicitly specified. Unlike the above overload,
the type 'T' need not provide operator<< for ostream,
but textual representation of default value must be provided
by the user.
*/
typed_value* default_value(const T& v, const std::string& textual);

Reply all
Reply to author
Forward
0 new messages