Boost logo

Boost Users :

From: Mikko Vainio (mivainio_at_[hidden])
Date: 2007-02-26 05:42:58


>
> 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;
  }
}


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