Boost logo

Boost Users :

Subject: Re: [Boost-users] [program options] vectro from config file
From: Vladimir Prus (vladimir_at_[hidden])
Date: 2009-07-13 11:58:16


Diederick C. Niehorster wrote:

> Hi All,
>
> I can't find an answer to this before, so I'll ask here.
>
> I would like to read a vector from a config file using program_options.
>
> example definition of option:
> ("Expt.ControlOrd", po::value<vector<int>>(&_pcData->SetExptOrders()),
> "Expt.ControlOrd")
>
> In the config file, the following works:
> ControlOrd = 1
> ControlOrd = 2
>
> But i would much prefer to use:
> ControlOrd = 1 2 or
> ControlOrd = (1 2)
> or something like that.
>
> I have made this work by editing the sourcecode, diff:
> ----
> Index: config_file.cpp
> ===================================================================
> --- config_file.cpp (revision 54915)
> +++ config_file.cpp (working copy)
> @@ -12,6 +12,8 @@
> #include <boost/program_options/detail/convert.hpp>
> #include <boost/throw_exception.hpp>
>
> +#include <boost/algorithm/string.hpp>
> +
> #include <iostream>
> #include <fstream>
> #include <cassert>
> @@ -112,8 +114,17 @@
> found = true;
> this->value().string_key = name;
> this->value().value.clear();
> - this->value().value.push_back(value);
> this->value().unregistered = !registered;
> +
> + if (*value.begin() == '(' && *value.rbegin() == ')')
> + {
> + value = value.substr(1, value.size()-2);
> + vector<string> SplitVec;
> + split( SplitVec, value, is_any_of(" ") );
> + this->value().value.swap(SplitVec);
> + }
> + else
> + this->value().value.push_back(value);
> break;
>
> } else {
> ----
>
> Of course, this is not a great solution as it doesn't allow you to
> read strings starting with ( and ending with ). Well, it is missing
> one thing. If we could somehow check that the datatype requested for
> this options is a vector, we could add that check to the if.
> I think that leaves only one problem, that of reading a vector of strings.
>
> Please tell me if it is possible to check for the requested type at
> this point in the code and how to do it. Any other comments also very
> welcome!

It's not possible at this point of code. Config file parser is purely syntactic, so
it does not know about types of options. Overriding 'validate' to handle () might be
easier solution, though current design with validate as global function does not make
it very convenient. There's proposal, on boost-devel, to allow specifying validate
function per-option, that will help.

- Volodya


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