c++boostboost-program-options

What is the format of BOOST program options command lines?


I have two switches, 'i' and 'p' that represent IPAddress and Port respectively.

What is the format of the command line?

I have tried:

app -i192.168.1.1 -p12345
app -i 192.168.1.1 -p 12345
app -i=192.168.1.1 -p=12345
app -i='192.168.1.1' -p='12345'
app --IPAddress 192.168.1.1 --Port12345

My application is having a problem with the IPAddress, and troubleshooting with DDD is unrevealing as I get for the vm.

Also, the app is running as a daemon, so my cout statements for the IP address and Port are going into oblivion, and printing to the syslog is hindered by the fact that outputting the values is not a const char*.

I plan to use program options for other things as well, but I am in over my head a bit with this.

po::options_description config("Configuration");
        config.add_options()
            ("IPAddress,i","IP Address")
            ("Port,p","Port")
             ;
po::variables_map vm;
        po::store(po::parse_command_line(ac, av, config),
                       vm);

        po::notify(vm);
//...and this is how the values are used

int retval = getaddrinfo((vm["IPAddress"].as< string >()).c_str(),(vm["Port"].as<string>()).c_str(), &hint, &list);

Here is a complete program...nothing is printed to the console after 'Values':

#include <sstream>
#include <algorithm>
#include <stdlib.h>
#include <iterator>
#include <string>

//Using boost program options to read command line and config file data
#include <boost/program_options.hpp>
using namespace std;
using namespace boost;
namespace po = boost::program_options;

int main (int argc, char *argv[])
{
    po::options_description config("Configuration");
    config.add_options()
                ("IPAddress,i","IP Address")
                ("Port,p","Port")
                 ;

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

    cout << "Values\n";

    cout << (vm["IPAddress"].as< string >()).c_str();
    cout << " " << (vm["Port"].as<string>()).c_str();

    return 0;

}

Are the inputted values somehow unprintable?


Here is gdb output, seems to be be cast problem:

28              string address = (vm["IPAddress"].as< string >()).c_str();
(gdb) n
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_any_cast> >'
  what():  boost::bad_any_cast: failed conversion using boost::any_cast

Program received signal SIGABRT, Aborted.
0x0000003afd835935 in raise () from /lib64/libc.so.6

Solution

  • BOOST Program options support the common command line flavours known from Unix systems. Thus those two should work (they are working for me)

    app -i 192.168.1.1 -p 12345
    app --IPAddress=192.168.1.1 --Port=12345
    

    Remarks: