Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8583129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:27:39+00:00 2026-06-11T21:27:39+00:00

So I have just started adding option support to my code. I can either

  • 0

So I have just started adding option support to my code. I can either do it myself or use boost’s program options. The only thing that holds me back from using boost’s is yet another dependency that I’m adding to the project. However, if it can handle parsing complex objects with ease, I’m more willing to pay the price.

I just tried something like this based on the examples and it does not work:

#include <boost/program_options.hpp>
namespace po = boost::program_options;


#include <iostream>
#include <fstream>
#include <iterator>
using namespace std;


struct Grid{
    double xmin, xmax;
};

int main(int ac, char* av[])
{
    try {
        Grid g;

        po::options_description cmd("Allowed options");
        cmd.add_options()
            ("help", "produce help message")
            ("Grid", "grid information")
            ;

        po::variables_map vm;
        store(parse_command_line(ac, av, cmd), vm);
        notify(vm);

        if (vm.count("help")) {
            cout << cmd << "\n";
            return 0;
        }

        g = vm["Grid"].as<Grid>();
        cout << g.xmin << " " << g.xmax << endl;

    }
    catch(exception& e)
    {
        cout << e.what() << "\n";
        return 1;
    }    
return 0;

When I run the code with ./a.out --Grid {-1, 1}, I get boost::bad_any_cast: failed conversion using boost::any_cast. I do not understand what that means but my guess is I cannot properly tell boost of my object type Grid. How can I do this correctly with boost?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T21:27:40+00:00Added an answer on June 11, 2026 at 9:27 pm

    First, the simple way is to use po::value<std::vector<double>>()->multitoken(),:
    However, you need to pass arguments like this: --Grid -1 1

    int main(int ac, char* av[])
    {
        try {
    
            po::options_description cmd("Allowed options");
            cmd.add_options()
                ("help", "produce help message")
                ("Grid", po::value<std::vector<double>>()->multitoken(),
                 "grid information")
                ;
            po::variables_map vm;
            store(parse_command_line(ac, av, cmd), vm);
            notify(vm);
    
            if (vm.count("help")) {
                cout << cmd << "\n";
                return 0;
            }
    
            Grid g{vm["Grid"].as<std::vector<double>>()[0],
                   vm["Grid"].as<std::vector<double>>()[1]};
            cout << g.xmin << " " << g.xmax << endl;
    
        }
        catch(exception& e)
        {
            cout << e.what() << "\n";
            return 1;
        }
    }
    

    If you want to pass arguments like --Grid {-1,1}, you could add a operator>> and parse the std::string yourself:

    std::istream& operator>>(std::istream &in, Grid &g)
    {
        // Note that this code does not do any error checking, etc.
        // It is made simple on purpose to use as an example
        // A real program would be much more robust than this
        std::string line;
        std::getline(in, line);
        std::stringstream ss(line);
        char bracket, comma;
        double xmin, xmax;
        ss >> bracket >> xmin >> comma >> xmax;
        g = Grid{xmin, xmax};
        return in;
    }
    
    //...
    Grid g;
    try {
    
    //...
    cmd.add_options()
       ("help", "produce help message")
       ("Grid", po::value(&g), "grid information")
    ;
    //...
    
    cout << g.xmin << " " << g.xmax << endl;
    

    Also, notice that in --Grid {-1,1}, there is no space: -1,1, not -1, 1. This is because line will only contain {-1,.

    If you want the space, here is an example that can parse --Grid {-1, 1}, using a custom validator and multitoken:

    // Showing only changes, rest of the code is the same
    void validate(boost::any& v, const std::vector<std::string>& val,
                  Grid*, double)
    {
        std::stringstream ss(val[0]);
        char bracket;
        double xmin, xmax;
        ss >> bracket >> xmin;
        ss.str(val[1]);
        ss >> xmax;
        v = Grid{xmin, xmax};
    }
    
    po::options_description cmd("Allowed options");
            cmd.add_options()
            ("help", "produce help message")
            ("Grid", po::value(&g)->multitoken(), "grid information")
            ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just started to learn how to code iOS apps. I have made
I have just started using flexigrid and wanted to use the resource string to
I have just started to code for Mac OS X and I'm very excited
I have been developing iPhone application and just started adding CoreData for persistence. However
I'm just getting started with adding admob to my android app and have started
Just started learning MVVM. I have a tabcontrol where I am adding multiple instances
I have just started learning pointers, and after much adding and removing * s
I just started working with SQL Server and I have some troubles with adding
I am developing a GPS-based app and have just started adding in my UX
we have just started using a git account of our Django website project so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.