Tuesday, 15 March 2011

c++ - Read relative file path with Boost Program options -


I am working on a command line tool that can read the options:

    < Li>
  • An optional configuration file

I used to read those options and it works mostly well.

My code is like this:

Namespace = po = boost :: program_options; Name space fs = boost :: filesystem; Po :: variables_map vm; Po :: store (po :: parse_command_line (argc, argv, option), vm); Po :: store (po :: parse_config_file (ifs, options, true), vm); Po :: Notify (VM);

However, some of these options are file paths and I want them to be relative to the origin of the option. Here's an example of what I mean:

Assume that my tool is located in: / usr / local / bin

  mytool - After parsing - Foo.path ../config/assets/toto.txt  

, the foo.path option is the absolute path of the / usr Should be /local/config/assets/toto.txt .

Now if I specify foo.path in a configuration file, which is located at / usr / local /config/myconf.cfg The same happens:

  foo.path = assets / toto.txt  

I like the path, not according to the configuration file (execution path) And as a result the full path will be the same as before.

Does the value of an option come from so that I can adjust the path when completely transforming the relative path?

is a working directory that is set to the path of execution by default (all the related paths Are trying to use in your application) interpreted according to the working directory. You'll have the option to reach your goals:

  1. Change the work directory before interpreting the relative path from the config file. If necessary, you should restore the default value of the work directory after reading your configuration.
  2. Once you get some paths from config, you have to check whether this is the relative path. If so, then you need to create a correct path using the home directory of the config file Should write.

No comments:

Post a Comment