Boost Program Options: Any way to have variable number of "objects" each with parameters?

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

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@mcn.org> 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@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

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@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Diederick C. Niehorster Sent: Wednesday, June 20, 2012 9:14 PM To: boost-users@lists.boost.org 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@mcn.org> 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@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Hi Charles, On Fri, Jun 22, 2012 at 1:33 AM, Charles Mills <charlesm@mcn.org> wrote:
rectangle={width=5 height=10} rectangle={width=3 height=8} circle={diameter=3}
Thats not going to be straightforward indeed, I guess what you presented in the other email is the best way without much extra work. Best, Dee

On 22.06.2012 04:47, Diederick C. Niehorster wrote:
Hi Charles,
On Fri, Jun 22, 2012 at 1:33 AM, Charles Mills<charlesm@mcn.org> wrote:
rectangle={width=5 height=10} rectangle={width=3 height=8} circle={diameter=3}
Thats not going to be straightforward indeed, I guess what you presented in the other email is the best way without much extra work.
I would imagine this to be not hard. Either define operator>> for the class you use to represent rectangles, over override 'validate'? - Volodya

Vladimir, first off, thank you for the PO library. A *great* productivity aid.
define operator>> for the class
I hear you, but I'm rolling now on the rectangle.height approach. I'm a very experienced programmer, but my C++ experience is only about three years now, and taking on templated operator overrides would be a little bit of a project of its own. Charles -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Vladimir Prus Sent: Thursday, June 21, 2012 10:58 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] Boost Program Options: Any way to have variable number of "objects" each with parameters? On 22.06.2012 04:47, Diederick C. Niehorster wrote:
Hi Charles,
On Fri, Jun 22, 2012 at 1:33 AM, Charles Mills<charlesm@mcn.org> wrote:
rectangle={width=5 height=10} rectangle={width=3 height=8} circle={diameter=3}
Thats not going to be straightforward indeed, I guess what you presented in the other email is the best way without much extra work.
I would imagine this to be not hard. Either define operator>> for the class you use to represent rectangles, over override 'validate'? - Volodya _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Hi Charles, On 22.06.2012 17:49, Charles Mills wrote:
Vladimir, first off, thank you for the PO library. A *great* productivity aid.
Thanks!
define operator>> for the class
I hear you, but I'm rolling now on the rectangle.height approach.
I'm a very experienced programmer, but my C++ experience is only about three years now, and taking on templated operator overrides would be a little bit of a project of its own.
Well, if your Rectangle is a usual class, then I would have imagine non-template overloaded operator>> will work. However, it's fully up to you. Thanks, Volodya
participants (3)
-
Charles Mills
-
Diederick C. Niehorster
-
Vladimir Prus