0

I am writing a program that would receive as parameters a filename followed by multiple strings. Optionally it could take a -count argument;

./program [-count n] <filename> <string1> <string2> ...

This is the code I wrote:

    PO::positional_options_description l_positionalOptions;
    l_positionalOptions.add("file", 1);
    l_positionalOptions.add("strings", -1);

    PO::options_description l_optionsDescription;
    l_optionsDescription.add_options()
        ("count", PO::value<int>()->default_value(1), "How many times to write"),
        ("file", PO::value<std::string>(), "Output file name"),
        ("strings", PO::value<std::vector<std::string>>()->multitoken()->zero_tokens()->composing(), "Strings to be written to the file");

    PO::command_line_parser l_parser {argc, argv};
    l_parser.options(l_optionsDescription)
            .positional(l_positionalOptions)
            .allow_unregistered();

    PO::variables_map l_userOptions;

    try {
        PO::store(l_parser.run(), l_userOptions);
    }
    catch (std::exception &ex) {
        std::cerr << ex.what() << std::endl;
        exit(1);
    }

However, when I run ./program file.out str1 str2 str3 it fails with:

unrecognised option 'str1'

What am I doing wrong? Is this even possible with boost::program_options?

  • Is this a simplified example? Does the problem persist if you remove `"count"`? If you remove `"file"`? If you remove both? – JaMiT Oct 29 '22 at 18:08
  • @JaMiT Yes, this is a simplified example. And yes, the problem persists even if I remove the count parameter. It works with just one positional parameter (either the file name or the string array) – Cristian Rotaru Oct 30 '22 at 10:04
  • *"the problem persists even if I remove the count parameter."* -- then I would say the example has not been simplified enough. If you don't need the count parameter in the question, then don't have it in the question. The simpler your setup, the better (more widely applicable) your question, and usually the easier to answer. – JaMiT Oct 31 '22 at 05:48

1 Answers1

1

I figured it out. It's as dumb as it can get.

The issue was that I had a , after every entry in add_options(). This made it so only the first entry would get saved.