How do I detect typo with Boost.program_options?

621 Views Asked by At

I use boost.program_options library. Consider this simplified case.

po::options_description desc("Usage");
desc.add_options()
("uninstall,u", "uninstall program")
("custom,c", po::wvalue<std::wstring>(), "specify custom action");

po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);

I want to produce error on such command-line:

testprog.exe -u c- action1

Note, user made a typo "c-" instead of "-c". But the parser understands this as a single -u option. How do I handle such cases?

3

There are 3 best solutions below

0
On BEST ANSWER

I want to produce error on such command-line:

testprog.exe -u c- action1

Note, user made a typo "c-" instead of "-c". But the parser understands this as a single -u option. How do I handle such cases?

Instruct the program_options library to accept no positional arguments and you get the desired behavior

code & compile:

macmini:stackoverflow samm$ cat po.cc
#include <boost/program_options.hpp>
#include <boost/version.hpp>

#include <iostream>

int
main(int argc, char* argv[])
{
    namespace po = boost::program_options;
    po::options_description desc("Usage");
    desc.add_options()
        ("uninstall,u", "uninstall program")
        ("custom,c", po::wvalue<std::wstring>(), "specify custom action")
        ;

    po::variables_map vm;
    po::command_line_parser cmd_line( argc, argv );
    cmd_line.options( desc );
    cmd_line.positional( po::positional_options_description() );

    try {
        po::store( cmd_line.run(), vm );

        po::notify(vm);
    } catch ( const std::exception& e ) {
        std::cerr << e.what() << std::endl;
        return -1;
    }

    return 0;
}
macmini:stackoverflow samm$ g++ po.cc -I /opt/local/include -L/opt/local/lib -lboost_program_options -Wl,-rpath,/opt/local/lib

run:

macmini:stackoverflow samm$ ./a.out -u c- action1
too many positional options
macmini:stackoverflow samm$ ./a.out -u -c action1
macmini:stackoverflow samm$ 
1
On

Compare argc-1 to the number of arguments found by program_options? If it doesn't match, there is a syntax error.

It won't catch all cases but it may catch those important to you.

5
On

I think the only way you can do this is to ensure that each argument you require is present, for example by testing the count of each type.

if (vm.count("uninstall")) { ... }
if (vm.count("custom")) { ... }

You can generate an error if the options you require are not present (i.e. count is 0) or are present (for example -u and -c cannot be specified would be count of both is >0).