C++ : Using Boost Program Options (Positional Options)

Boost is a collection of very powerful libraries for C++ that offers a rich set of functionalities. We could use boost to parse program options and positional options.

In the below program ( example.cpp ) we pass program options some of which are positional options to the program.
Boost library used : 1_77_0

Compilation :

g++ example.cpp -lboost_program_options -o program

Program : example.cpp

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

int main (int argc, char * argv[]) {

    boost::program_options::options_description desc
        ("\nMandatory arguments marked with '*'.\n"
           "Invocation : <program> --host <hostname> --port <port> <web_app_name> <web_app_schema_file> \nAgruments");

    desc.add_options ()
    ("host", boost::program_options::value<std::string>()->required(),
                 "* Hostname.")
    ("port",  boost::program_options::value<std::string>()->required(),
                 "* Port")
    ("web_app_name",  boost::program_options::value<std::string>()->required(),
                 "* Web App Name")
    ("web_app_schema_file",  boost::program_options::value<std::string>()->required(),
                 "* Web App Schema File");

    // Positional arguments don't need a parameter flag
    boost::program_options::positional_options_description pos_desc;
    pos_desc.add("web_app_name", 1);
    pos_desc.add("web_app_schema_file", 1);

    boost::program_options::variables_map vm;

    try {
        boost::program_options::store(boost::program_options::command_line_parser(argc, argv).
                                                              options(desc).
                                                              positional(pos_desc).
                                                              run(), vm);
        boost::program_options::notify(vm);

    } catch (boost::program_options::error& e) {
        std::cout << "ERROR: " << e.what() << "\n";
        std::cout << desc << "\n";
        return 1;
    }

    std :: string host(vm["host"].as<std::string>());
    std :: string port(vm["port"].as<std::string>());
    std :: string app_name(vm["web_app_name"].as<std::string>());
    std :: string schema_file(vm["web_app_schema_file"].as<std::string>());

    std :: cout << "Entered Parameters..." << std :: endl;
    std :: cout << "Hostname : " << host << std :: endl;
    std :: cout << "Port : " << port << std :: endl;
    std :: cout << "Web Application : " << app_name << std :: endl;
    std :: cout << "Web Application Schema File : " << schema_file << std :: endl;

    return 0;
}

Output

$ ./program 
ERROR: the option '--host' is required but missing

Mandatory arguments marked with '*'.
Invocation : <program> --host <hostname> --port <port> <web_app_name> <web_app_schema_file> 
Agruments:
  --host arg                * Hostname.
  --port arg                * Port
  --web_app_name arg        * Web App Name
  --web_app_schema_file arg * Web App Schema File

$ ./program --host localhost --port 8080 
ERROR: the option '--web_app_name' is required but missing

Mandatory arguments marked with '*'.
Invocation : <program> --host <hostname> --port <port> <web_app_name> <web_app_schema_file> 
Agruments:
  --host arg                * Hostname.
  --port arg                * Port
  --web_app_name arg        * Web App Name
  --web_app_schema_file arg * Web App Schema File

$ ./program --host localhost --port 8080 G.O.T 
ERROR: the option '--web_app_schema_file' is required but missing

Mandatory arguments marked with '*'.
Invocation : <program> --host <hostname> --port <port> <web_app_name> <web_app_schema_file> 
Agruments:
  --host arg                * Hostname.
  --port arg                * Port
  --web_app_name arg        * Web App Name
  --web_app_schema_file arg * Web App Schema File


$ ./program --host localhost --port 8080 G.O.T all_kingdoms.txt
Entered Parameters...
Hostname : localhost
Port : 8080
Web Application : G.O.T
Web Application Schema File : all_kingdoms.txt


Copyright (c) 2019-2024, Algotree.org.
All rights reserved.