Boost logo

Boost Users :

From: Allen Cronce (allenslists_at_[hidden])
Date: 2008-08-25 15:10:09


Hi all,

I'm trying to use program-options to specify a filesystem path object
under Boost 1.35.0. The following code works fine when the path
specified on the command line has no spaces in it. But when it does
have spaces, I get an error.

--snip--

#include <iostream>
#include <sstream>

#include <boost/program_options.hpp>
#include <boost/filesystem/path.hpp>

namespace po = boost::program_options;
namespace fs = boost::filesystem;

using namespace std;

int main (int argc, char * argv[])
{
        int result = 0;
        
        try
        {
                fs::path inPath;
                
                po::options_description desc("Allowed options");
                desc.add_options()
                ("in,i", po::value<fs::path>(&inPath), "Input file or directory
<pathname>")
                ;
                
                po::variables_map vm;
                po::store(po::parse_command_line(argc, argv, desc), vm);
                po::notify(vm);
                
                if (0 ==vm.count("in"))
                {
                        throw invalid_argument("What? No input path specifed!");
                }
                
                cout << "Input path = \"" << inPath.file_string() << "\"\n";
        }
        catch(const boost::filesystem::filesystem_error& e)
        {
                cerr << "\nFilessytem error: " << e.what() << endl;
                result = 3;
        }
        catch(const std::exception& e)
        {
                cerr << "\nException error: " << e.what() << endl;
                result = 2;
        }
        catch(...)
        {
                cerr << "\nError: Caught an unknown exception!" << endl;
                result = 1;
        }
        
        return result;
}

--snip--

If I call the above with the following parameters, it works:

        --in "my_file"

        Input path = "my_file"

But if I call it with a path that has a space in it, I get an error:

        --in "my file"

        Exception error: in option 'in': invalid option value 'my file'

I know that I can work around the problem by loading the path into an
interim string first, then use the string to initialize a path object.
But unless I'm missing something, I would have thought that
iteroperability between boost.program-options and boost.filesystem
should be more seamless. It seems very common to want to load options
into path objects.

Best,

--
Allen Cronce

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net