|
Boost Users : |
Subject: Re: [Boost-users] [program options] vectro from config file
From: Diederick C. Niehorster (dcnieho_at_[hidden])
Date: 2009-07-14 01:46:02
Hi,
I have made another change to the code, in config_file.cpp this time.
I noticed that collect_unrecognized() didn't work on output of the
config file parser as it doesnt set the original_tokens field. Its
doens't error, just doesn't collect anything.
I have thus written the parser such that the keyname is written to the
original_tokens field if the keyname is unregistered, allowing
collect_unrecognized to do its job as expected. diff below.
Is this the right list for this kind of discussion by the way?
Best,
Diederick
------
Index: config_file.cpp
===================================================================
--- config_file.cpp (revision 54915)
+++ config_file.cpp (working copy)
@@ -114,6 +114,15 @@
this->value().value.clear();
this->value().value.push_back(value);
this->value().unregistered = !registered;
+
+ // add name to original_tokens so we can run collect_unregi
stered
+ // over po::parsed_options from po::parse_config_file too
+ this->value().original_tokens.clear();
+ if (!registered)
+ {
+ this->value().original_tokens.push_back(name);
+ }
+
break;
} else {
On Tue, Jul 14, 2009 at 12:53 PM, Diederick C.
Niehorster<dcnieho_at_[hidden]> wrote:
> Hi Volodya,
>
> Thank you for your reply. I have placed my code, improved, into the
> validate() function now (see below). Thats a good idea anyway, this
> way it works on input from any source. I am still stuck with the issue
> of how to test whether users specified a vector or not as return
> target in their options_description, that is:
> ("Tex.GaussSigma", po::value<float>(&fTexGaussSigma) Â , "Tex.GaussSigma") or
> ("Tex.Color" Â Â Â Â Â , po::value<vector<float>>(&vfTexColor), "Expt.Color")
>
> Due to the very complicated flow (if you've just looked at the code
> for a few hours) I find it hard to figure out. I suppose i have to run
> typeid(*something*).name() to get the type of some variable and then
> check if its a vector or not. But what should be in the place of
> *something*?
>
> As a general point, do you think this would be a good addition to the
> parser, that is, would you want to incorporate this kind of extention
> into the Boost release? It is a much easier way of specifying a
> vector, whether on the command line or from another source without
> having to resort to positional options, which are not a good solution
> for every source and situation. Compare (for command line) [--test "(1
> 2)"] with [--test 1 --test 2].
>
> Lastly, in an old post (cant find it now, was 2005 or 2006) a user
> asked whether it was possible to do
> po::parse_config_file(InitFileName,poInit,true); instead of
> po::parse_config_file(ifs,poInit,true);
> as the documentation apparently reflected this at that point. You
> replied that you'd fix that at that time (I believe it was you,
> doesn't really matter), but when i just tried it didnt work. Have you
> decided against it?
>
> Best,
> Diederick
>
> -----
> Index: detail/value_semantic.hpp
> ===================================================================
> --- detail/value_semantic.hpp  (revision 54915)
> +++ detail/value_semantic.hpp  (working copy)
> @@ -8,6 +8,8 @@
>
> Â #include <boost/throw_exception.hpp>
>
> +#include <boost/algorithm/string.hpp>
> +
> Â namespace boost { namespace program_options {
>
> Â Â extern BOOST_PROGRAM_OPTIONS_DECL std::string arg;
> @@ -142,11 +144,28 @@
> Â Â Â Â Â Â Â Â /* We call validate so that if user provided
> Â Â Â Â Â Â Â Â Â Â a validator for class T, we use it even
> Â Â Â Â Â Â Â Â Â Â when parsing vector<T>. Â */
> - Â Â Â Â Â Â Â Â boost::any a;
> - Â Â Â Â Â Â Â Â std::vector<std::basic_string<charT> > v;
> - Â Â Â Â Â Â Â Â v.push_back(s[i]);
> - Â Â Â Â Â Â Â Â validate(a, v, (T*)0, 0);
> - Â Â Â Â Â Â Â Â tv->push_back(boost::any_cast<T>(a));
> +
> + Â Â Â Â Â Â Â Â vector<std::basic_string<charT>> value;
> +
> + Â Â Â Â Â Â Â Â // test if vector notation is used in input
> + Â Â Â Â Â Â Â Â if (*s[i].begin() == '(' && *s[i].rbegin() == ')')
> + Â Â Â Â Â Â Â Â {
> + Â Â Â Â Â Â Â Â Â Â // TODO: test if vector is expected by user, if not, no spl
> itting
> + Â Â Â Â Â Â Â Â Â Â // TODO: if vector expected, test if its vector of strings,
> Â that will need special treatment
> + Â Â Â Â Â Â Â Â Â Â split( value, s[i].substr(1, s[i].size()-2), is_any_of(" ")
> Â );
> + Â Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â Â else
> + Â Â Â Â Â Â Â Â Â Â value.push_back(s[i]);
> +
> + Â Â Â Â Â Â Â Â // validate option values
> + Â Â Â Â Â Â Â Â for (unsigned j = 0; j < value.size(); j++)
> + Â Â Â Â Â Â Â Â {
> + Â Â Â Â Â Â Â Â Â Â boost::any a;
> + Â Â Â Â Â Â Â Â Â Â std::vector<std::basic_string<charT> > v;
> + Â Â Â Â Â Â Â Â Â Â v.push_back(value[j]);
> + Â Â Â Â Â Â Â Â Â Â validate(a, v, (T*)0, 0);
> + Â Â Â Â Â Â Â Â Â Â tv->push_back(boost::any_cast<T>(a));
> + Â Â Â Â Â Â Â Â }
> Â Â Â Â Â Â }
> Â Â Â Â Â Â catch(const bad_lexical_cast& /*e*/) {
> Â Â Â Â Â Â Â Â boost::throw_exception(invalid_option_value(s[i]));
>
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