boost::program_options minimal examples

Examples in this post:

More examples will possibly be added in the future

Two arguments: –input/-i and –output/-o, both strings, both required

/**
 * Compile like this: g++ -o test test.cpp -lboost_program_options
 * Usage examples:
 *      ./test -i infile.txt -o outfile.txt
 *      ./test --input infile.txt --output outfile.txt
 */
#include <boost/program_options.hpp>
#include <string>
#include <iostream>

namespace po = boost::program_options;

int main(int argc, char** argv) {
    // Arguments will be stored here
    std::string input;
    std::string output;
    // Configure options here
    po::options_description desc ("Allowed options");
    desc.add_options ()
        ("help,h", "print usage message")
        ("input,i", po::value(&input), "Input file")
        ("output,o", po::value(&output), "Output file");
    // Parse command line arguments
    po::variables_map vm;
    po::store (po::command_line_parser (argc, argv).options (desc).run (), vm);
    po::notify (vm);
    // Check if there are enough args or if --help is given
    if (vm.count ("help") || !vm.count ("input") || !vm.count ("output")) {
        std::cerr << desc << "\n";
        return 1;
    }
    // Your code goes here
}

Single positional string argument

/**
 * Compile like this: g++ -o test test.cpp -lboost_program_options
 * Usage examples:
 *      ./test infile.txt
 *      ./test --input infile.txt
 */
#include <boost/program_options.hpp>
#include <string>
#include <iostream>

namespace po = boost::program_options;

int main(int argc, char** argv) {
    // Arguments will be stored here
    std::string input;
    // NOTE: Declare the same option in both desc and positionals! 
    // No normal options declared, only positionals!
    po::options_description desc ("Allowed options");
    desc.add_options()
        ("help", "Print usage")
        ("input", po::value(&input), "Input file");
    // Configure options here
    po::positional_options_description positionals;
    positionals.add("input", 1 /* 1 = max num of inputs ; -1: arbitrary number */);
    // Parse command line arguments
    po::variables_map vm;
    po::store (po::command_line_parser (argc, argv)
        .positional(positionals)
        .options(desc).run (), vm);
    po::notify (vm);
    // Check if there are enough args or if --help is given
    if (vm.count ("help") || !vm.count ("input")) {
        std::cerr << desc << "\n";
        return 1;
    }
    // Your code goes here!
    std::cout << "Input is " << input << std::endl;
}

A double-typed argument (with automatic type checking)

/**
 * Compile like this: g++ -o test test.cpp -lboost_program_options
 * Usage examples:
 *      ./test -v 1.23
 * This does not work
 *      ./test -v ABC
 */
#include <boost/program_options.hpp>
#include <string>
#include <iostream>

namespace po = boost::program_options;

int main(int argc, char** argv) {
    // Arguments will be stored here
    double value;
    // Configure options here
    po::options_description desc ("Allowed options");
    desc.add_options ()
        ("help,h", "print usage message")
        ("value,v", po::value(&value), "The value");
    // Parse command line arguments
    po::variables_map vm;
    po::store (po::command_line_parser (argc, argv).options (desc).run (), vm);
    po::notify (vm);
    // Check if there are enough args or if --help is given
    if (vm.count ("help") || !vm.count ("value")) {
        std::cerr << desc << "\n";
        return 1;
    }
    // Your code goes here!
    std::cout << value << std::endl;
}