[program_options] Documentation of configuration file format?
I can't seem to find any documentation on what kind of file formats are accepted when calling boost::program_options::parse_config_file(...) Am is missing it or is it missing? :) -- Tarjei
On Mon, 2007-02-26 at 10:49 +0100, Tarjei Knapstad wrote:
I can't seem to find any documentation on what kind of file formats are accepted when calling boost::program_options::parse_config_file(...)
Am is missing it or is it missing? :)
I think your are right about that missing piece of documentation. However, the format is quite easy: long-variable-name = value At least that's how i use it. Hope this helps. Nevertheless, it seems that you can not use unknown variables in your cfg-files. Greetings, Sebastian
-- Tarjei _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On 2/26/07, Sebastian Weber
On Mon, 2007-02-26 at 10:49 +0100, Tarjei Knapstad wrote:
I can't seem to find any documentation on what kind of file formats are accepted when calling boost::program_options::parse_config_file(...)
Am is missing it or is it missing? :)
I think your are right about that missing piece of documentation. However, the format is quite easy:
long-variable-name = value
At least that's how i use it. Hope this helps. Nevertheless, it seems that you can not use unknown variables in your cfg-files.
Greetings,
Sebastian
Is it possible to somehow use an option which selects a configuration file: program --configuration-file my_config.conf with the same easiness as common tasks demonstrated in examples? Thank you. -el
Is it possible to somehow use an option which selects a configuration file:
program --configuration-file my_config.conf
Sure, have a look at the "multi-source" example which is provided in libs/program_options/examples I'm doing this all the time ... Sebastian
with the same easiness as common tasks demonstrated in examples?
Thank you.
-el _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Is it possible to somehow use an option which selects a configuration file:
program --configuration-file my_config.conf
with the same easiness as common tasks demonstrated in examples?
Thank you.
-el
Yes, it's possible, and documented in the "multi-source" section as Sebastian already mentioned. The options_description that is used to parse the command line should contain an option for a configuration file: string configFileName; options_description optdesc("Options"); optdesc.add_options() // add whatever options you have, plus ("config", po::value< string >( &configFileName ), "The name of a file to read for default options. Command-line options" " override the ones in the config file. A config file may contain lines with syntax" "\n'long_option_name = value'\nand comment lines that begin with '#'." ) ; po::store(po::command_line_parser(argc,argv).options(optdesc).positional(popt).run(), varmap); po::notify( varmap ); if( varmap.count("config") ) { cout << "Trying to read options from " << configFileName << "..."; cout.flush(); ifstream ifs( configFileName.c_str() ); if( !ifs.is_open() ) cout << "no such file." << endl; else { po::store( po::parse_config_file( ifs, optdesc ), varmap ); po::notify( varmap ); cout << "done." << endl; } }
On 2/26/07, Sebastian Weber
On Mon, 2007-02-26 at 10:49 +0100, Tarjei Knapstad wrote:
I can't seem to find any documentation on what kind of file formats are accepted when calling boost::program_options::parse_config_file(...)
Am is missing it or is it missing? :)
I think your are right about that missing piece of documentation. However, the format is quite easy:
long-variable-name = value
At least that's how i use it. Hope this helps. Nevertheless, it seems that you can not use unknown variables in your cfg-files.
Thanks Sebastian. I would consider not allowing unknown variables in a config file to be correct behaviour. (The program should fail if you specify unknown arguments on the command line, and the config file is really just a substitue for the command line options). -- Tarjei
Hi!
Thanks Sebastian.
I would consider not allowing unknown variables in a config file to be correct behaviour. (The program should fail if you specify unknown arguments on the command line, and the config file is really just a substitue for the command line options).
Well, I would need this feature quite urgent, because depending upon some parameters, my program loads different objects which have again different program options. Thus I need 2 runs to process all options. In the first run I decide which program modules I have to load. During this first pass, unknown options are no problem so far. Only in the second run, all options have to be processed. Right now I just catch the unknown option exception which works, but leaves me with possibly unparsed program options in the first run. Sebastian
-- Tarjei _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On 2/26/07, Sebastian Weber
Hi!
Thanks Sebastian.
I would consider not allowing unknown variables in a config file to be correct behaviour. (The program should fail if you specify unknown arguments on the command line, and the config file is really just a substitue for the command line options).
Well, I would need this feature quite urgent, because depending upon some parameters, my program loads different objects which have again different program options. Thus I need 2 runs to process all options. In the first run I decide which program modules I have to load. During this first pass, unknown options are no problem so far. Only in the second run, all options have to be processed.
Right now I just catch the unknown option exception which works, but leaves me with possibly unparsed program options in the first run.
Makes sense. Just noticed in the "How-To" section of the manual that this should be possible: http://boost.org/doc/html/program_options/howto.html#id2716386 Hope you can get it to work. -- Tarjei
participants (4)
-
elviin
-
Mikko Vainio
-
Sebastian Weber
-
Tarjei Knapstad