program_options variable_value as cast explodes

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I'm using program_options in a few programs that I'm doing, and I'd like to clarify what's going on at certain points. vm.count("key") appears to tell us the number of times a flag is specified, but the documentation here: <http://www.boost.org/doc/html/variables_map.html> doesn't seem to say anything about it. Where is it specified? Does abstract_variables_map actually inherit off of std map? Why don't we use !vm["key"].empty() instead? (slightly more verbose, but it's clearer about what it's doing). The vm["term"].as<int>() template seems to combust spectacularly (as in cause a stack dump) when it turns out that vm["term"] doesn't actually exist. This doesn't seem very robust, and according to the documentation, an exception ought to be thrown. Am I just misunderstanding the docs? What is the preferred method determining that no parameters were passed? I'm currently using argc == 1, which seems a bit hacky, and doesn't catch errant parameters. Thanks. This might seem a little pedantic, but I'm trying to do things right. :-) -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFFf2sIqTO+fYacSNoRAqz5AJ9nBOEz87v8H4HjRB6mM2NSvNSllQCaA+SE s0kcqXiQXy7+ID7ugotbDKc= =NvIj -----END PGP SIGNATURE-----

Edward Z. Yang wrote:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
I'm using program_options in a few programs that I'm doing, and I'd like to clarify what's going on at certain points.
vm.count("key") appears to tell us the number of times a flag is specified, but the documentation here: <http://www.boost.org/doc/html/variables_map.html> doesn't seem to say anything about it. Where is it specified? Does abstract_variables_map actually inherit off of std map? Why don't we use !vm["key"].empty() instead? (slightly more verbose, but it's clearer about what it's doing).
This catches me out too, IMHO it is the most annoying aspect of an otherwise great library. The variables map is an ordinary map, not a multimap, so the count() only ever returns 1 or zero. Instead, multiple occurances of a flag are specified by using a parameter of type std::vector<T>, so it should be vm["key"].as<std::vector<T> >().size().
The vm["term"].as<int>() template seems to combust spectacularly (as in cause a stack dump) when it turns out that vm["term"] doesn't actually exist. This doesn't seem very robust, and according to the documentation, an exception ought to be thrown. Am I just misunderstanding the docs?
I don't know about that one.
What is the preferred method determining that no parameters were passed? I'm currently using argc == 1, which seems a bit hacky, and doesn't catch errant parameters.
Nor that one. Cheers, Ian
participants (2)
-
Edward Z. Yang
-
Ian McCulloch