0

I have some legacy C++ code that accepts input parameters. It is in the following format:

int main(int argc,char* argv[])
{
    parse_args(argc,argv);
    return 0;
}

The inputs had to be specified in the following format:

$ ./a.out -f sample.txt -o out.xml

Now, I need to modify the above code to be able to accept inputs specified in an input configuration file using the boost::program_options::parse_config_file function such that the input can be specified as:

$ ./a.out -cfg config.cfg

Where the content of the file 'config.cfg' is as follows:

f=sample.txt
o=out.xml

I would like to do this without making any changes to the parse_args() function.

Hence, could you please suggest a method to convert the input arguments parsed from the cfg file (consider it to be a vector of strings) into a format equivalent to that of char* argv[]?

  • 1
    Convert the result of `parse_config_file()` into a `std::vector` of C-style strings, ie `{"f", "sample.txt", "o", "out.xml"}`. Then you can pass `vector.data()` and `vector.size()` to `parse_args()` – Remy Lebeau May 06 '21 at 06:38
  • @RemyLebeau: Don't forget to push a `nullptr`. `argv` is nullptr-terminated – MSalters May 06 '21 at 08:12
  • @MSalters `argc` specifies the number of `char*`s in `argv`, no null is needed – Remy Lebeau May 06 '21 at 08:17
  • @RemyLebeau: You're looking at it from the wrong side of the contract. `main` sets the last element of `argv` to `nullptr`, and the implementation of `parse_args` might rely on that, or it might rely on `argc`. Yes, how `main` does it is technically redundant, but that's how it works. This code tries to emulate the same `argv[]` array, so it should have the same redundancy. – MSalters May 06 '21 at 08:22

0 Answers0