0

I am using Boost 1.62.0 on windows with Visual studio 2013 and pre compiled libraries.

I have this sample code:

#include "boost/program_options.hpp"
#include "boost/filesystem.hpp"
#include <iostream>


namespace po = boost::program_options;

int main(int argc, char *argv[])
{
    po::options_description desc("Allowed options");
    for (int i = 0; i < argc; i++)
    {
        std::cout << i << "->" << argv[i] << std:: endl;
    }
    try
    {

        std::string outputPath;
        boost::filesystem::path workingDir;
        // get program options and check them that they are valid
        {
            desc.add_options()
                ("input-path,i", po::value< std::string >(), "input path")
                ("output_path,o", po::value< std::string >(&outputPath)->implicit_value(""), "output path.")
                ;
            po::positional_options_description p;
            p.add("input-path", -1);
            po::variables_map vm;
            po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
            po::notify(vm);

            if (vm.count("input-path") == 0)
            {
                std::cout << "No input directory specified.\n";
                return -1;
            }


            workingDir = vm["input-path"].as<std::string>();

        }
    }
    catch (std::runtime_error &err)
    {
        std::cout << "Error: " << err.what() << std::endl;
    }
    catch (const boost::program_options::error &ex)
    {
        std::cerr << ex.what() << '\n';

    }

}

And I am running it inside VS using this command line:

"C:\Temp\test\in" -o "C:\Temp\test\out"

the application print this:

0->C:\MyData\TestCodes\testBoostProgramOption\build\Debug\test.exe
1->C:\Temp\test\in
2->-o
3->C:\Temp\test\in

which seems correct, but I am getting this error:

option '--input-path' cannot be specified more than once

What is wrong with code or program options?

mans
  • 17,104
  • 45
  • 172
  • 321
  • `-o` has an implicit value set, you have a space separating your short-form option and the value, and you have a positional parameter defined (that can occur multiple times due to the `-1`). That means that the `-o` parses correctly, taking the implicit value, and the second path is treated as a positional parameter. You should have `-o"C:\Temp\test\out"`. – Dan Mašek Feb 28 '17 at 23:12
  • @DanMašek: thanks, that is solved my problem. Can you please raise it as an answer and I will accept it. – mans Mar 01 '17 at 08:43

0 Answers0