Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have an optional option value in boost program options?

I am using the Boost program option and I want to offer an option which has three ways:

  1. If not define
  2. If defined but no value
  3. If defined with a value

For example, I have a program that works on a file such as a.jpg, and I want to offer the user to be able to use it in the following scenarios:

myapp.exe a.jpg  : process jpeg 
myapp.exe a.jpg -e : process jpeg and generate report at the same directory as a.jpg
myapp.exe a.jpg -e c:\tmp\ : process jpeg and generate report at c:\tmp\

How can I do this with Boost program options?

like image 796
mans Avatar asked Aug 10 '15 13:08

mans


People also ask

How do I use Boost program options?

To add options, you use the add_options() function and append all your options, each one surrounded with parenthesis. Here, we declared two options (help and version). They can be set with --help and --version. You can use operator on the description to output all the options of program.

What is boost:: program_ options?

Boost C++ Libraries The program_options library allows program developers to obtain program options, that is (name, value) pairs from the user, via conventional methods such as command line and config file.

Is Boost Program Options header only?

Some boost libraries are header-only, some are not, and for various reasons etc. Is there a specific reason/design decision why Boost.


1 Answers

You can achieve this effect by giving the value both a default_value and an implicit_value.

The default_value will be used when the option is not specified at all. The implicit_value will be used when the option is specific without a value. If a value is specified, it will override the default and implicit.

So, some code to do this could look something like this:

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

using namespace std;

int main(int argc, char** argv)
{
    namespace po = boost::program_options;

    po::options_description desc("Options");
    desc.add_options()
        ("process-jpeg,e", po::value<string>()->default_value("")->implicit_value("./"), "Processes a JPEG.");

    po::variables_map vm;
    try
    {
        po::store(po::parse_command_line(argc, argv, desc), vm);
        po::notify(vm);
    } catch (po::error& e) {
        cerr << "ERROR: " << e.what() << endl << endl << desc << endl;
        return 1;
    }

    string outputDir = vm["process-jpeg"].as<string>();
    if (outputDir.empty()) {
        cout << "-e was not provided on the command line" << endl;
    } else {
        cout << "-e is using directory: " << outputDir << endl;
    }
}

Running this example code prints:

$ ./jpg_processor
-e was not provided on the command line

$ ./jpg_processor -e
-e is using directory: ./

$ ./jpg_processor -e c:\tmp
-e is using directory: c:\tmp
like image 176
Sean Cline Avatar answered Oct 06 '22 00:10

Sean Cline