Boost logo

Boost Users :

Subject: Re: [Boost-users] Using program_options from a string?
From: Vladimir Prus (vladimir_at_[hidden])
Date: 2009-03-09 03:36:21


boostnewbie wrote:

>
> Hello all. I am using the boost::program_options library for configuration
> files with great success. I'm wondering if it's possible to use the library
> using a string as a source, instead of a config file.
>
> I read the configuration file with code like this:
> if (vm.count("config")) {
> string f = vm["config"].as<string>() ;
> ifstream ifs(f.c_str());
> store(parse_config_file(ifs, config_file_options), vm);
> po::notify(vm);
> }
>
> but what I'd also like to do is something along these lines, with a string
> as the original source instead of a file specified with my 'config' command
> line option:
>
> string str = "port = 3000 \n tmpdir = /tmp \n"; // something like
> this. this is what's in the config file.
> istringstream iss(str, istringstream::in);
> store(parse_config_file(iss, config_file_options), vm);
> po::notify(vm);
>
> These lines compile and execute without error but don't actually appear to
> update the 'vm'. Am I doing something wrong or is this not possible with
> the current incarnation of program_options?

This should be possible. You might want to step into 'store' with a debugger
to check if:

  - parse_config_file actually returns nothing, or
  - store fails to do anything, or
  - the problem is elsewhere.

- Volodya


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