Boost logo

Boost Users :

From: Merrill Cornish (merrill.cornish_at_[hidden])
Date: 2005-11-24 20:09:22


Below is a small program that demonstrates some of the problems I've
been having. It takes one positional parameter, "limit" which is of type
size_t. I'm linking statically with debug symbols.

With the "try" in the position shown, after the declaration of vm, this is
what happens to me for different command line arguments:

* --limit=4 => works and prints limit=4

* --limt=4 => [notice misspelling] throws "unknown option limt" exception;
   but also triggers Dr. Watson: Application Error - The instruction at
   "0x0042eb49" referenced memory at "0x010900fb". The memory
   could not be "written".

* --limit=-4 => [note negative value] never exits store(). Gets Dr.
   Watson "The instruction at "0x0049c090" referenced memory at
   "0x00eab651". The memory could not be "written.

* --limit=xxx => [note string value] never exits store(). Get Dr. Watson
   with same message as above.

Now, move the "try" to immediately after main() so all of the program
options code is included in the try block.

* --limit=4 => works as before

* --limt=4 => [notice misspelling] Does NOT throw expected "unknown
   option" exception, never exits store(), but does trigger Dr. Watson, this
   time trying to read location 0xd.

* --limit=-4 => similar error to above, with slightly different addresses.
   
* --limt=xxx => similar error to above with slightly different addresses.

Enjoy,
Merrill

//==========================================
#include <iostream>
#include "boost/program_options.hpp"

using namespace std;
namespace po = boost::program_options;

int main(int argc, char* argv[]) {
// try {
            po::options_description general("Option");
            general.add_options()
                    ("limit",
                            po::value<size_t>()->default_value(1),
                            " This is the concurrency limit.");
        po::variables_map vm;
    try {

        cout << "before store" << endl;
        po::store(po::command_line_parser(argc, argv).
                  options(general).run(), vm);
        cout << "after store" << endl;
            po::notify(vm);

            if (vm.count("limit")) {
            cout << "limit=" << vm["limit"].as<size_t>() << endl;
            }//--version
    }//try
        catch (exception& e) {
                cerr << e.what() << endl;
                return 1;
        }//exception
        catch (...) {
                cerr << "unknown exception." << endl;
                return 1;
        }//...

        return 0;
}//main()
//=========================================


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