Boost logo

Boost Users :

Subject: Re: [Boost-users] Boost Program Options: Any way to have variable number of "objects" each with parameters?
From: Charles Mills (charlesm_at_[hidden])
Date: 2012-06-21 13:33:01


Dee, thanks.

If you look at my follow-up to my OP I decided I did not like this general organization of the options. My thinking is that

[rectangles]
width=5 3 # or (5 3)
height=10 8 # or (10 8)

is not very intuitive as meaning "two rectangles, one 5 by 10 and one 3 by 8."

I like what you have proposed and will keep it in mind, but I would like to "factor" (if you will) my parameters the other way. I guess the *ideal* syntax from my point of view would be something like

rectangle={width=5 height=10}
rectangle={width=3 height=8}
circle={diameter=3}
etc.

I have no idea how that might be specified in add_options()! I guess perhaps with po::value<myStructWithHeightAndWidth> and a custom parser, but that's way more than I think I want to take on.

Thanks again,

Charles

-----Original Message-----
From: boost-users-bounces_at_[hidden] [mailto:boost-users-bounces_at_[hidden]] On Behalf Of Diederick C. Niehorster
Sent: Wednesday, June 20, 2012 9:14 PM
To: boost-users_at_[hidden]
Subject: Re: [Boost-users] Boost Program Options: Any way to have variable number of "objects" each with parameters?

Hi Charles,

One way would be as follows. You'd first have to patch some of the boost.PO code as detailed here:
https://svn.boost.org/trac/boost/ticket/3265#comment:1

Then, your init file could look like this:
[rectangle]
width= (5 3)
height=(10)
[circle]
diameter = (3 5)

etc. You need to make sure yourself that the vectors are of equal length. Empty vector would mean no objects of that type of course Make sure of course that in the options definition used when parsing the init file, your output variables are vectors:

std::vector<double> rectangle_widths;
po::options_description poInit;
poInit.add_options()
("rectangle.width" ,
po::value<std::vector<double>>(&rectangle_widths) ->required(),
"rectangle.width")
("rectangle.height", [...]);

And an extra vote to get my proposed change into the library would be nice to, let me know if it works for you!
Right now, all my code that uses this carries the following message:
// NB: correct reading of the vectors of values from the configuration file // depends on some local modifications in the boost source.
// see: https://svn.boost.org/trac/boost/ticket/3265#comment:1

Hope that helps,
Best,
Dee

On Thu, Jun 21, 2012 at 9:19 AM, Charles Mills <charlesm_at_[hidden]> wrote:
> I really like the Boost Program Options library. I’ve used it for one
> program now with great effect and I’d like to use it for another, but
> can’t figure out how to structure what I need to do so that Boost PO
> could handle it.
>
>
>
> I am solely interested in the “.ini file” for this – command line is
> irrelevant.
>
>
>
> The problem is “how does a program allow the specification of a
> variable number of ‘objects’ each of which has parameters of its own?”
>
>
>
> Here is a hypothetical program that exemplifies the problem. Suppose a
> program were going to create multiple geometric shapes of some sort.
> An ideal (unsupported by PO) .ini file might look like
>
>
>
> rectangles=3
>
> rectangle[0].width=5
>
> rectangle[0].height=10
>
> rectangle[1].width=3
>
> etc.
>
> circles=2
>
> circle[0].diameter=4
>
> etc.
>
>
>
> Can anyone suggest a way to accomplish this with Boost PO? I’m real
> flexible on what the .ini file might look like so long as it solves
> the problem and is reasonably explicable to a user. (No need for it to
> look almost like the
> above.)
>
>
>
> Thanks,
>
>
>
> Charles
>
>
> _______________________________________________
> 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