catchorg / Clara

A simple to use, composable, command line parser for C++ 11 and beyond
Boost Software License 1.0
648 stars 67 forks source link

Bug - whitespace in a boost::path option #55

Open ronen opened 6 years ago

ronen commented 6 years ago

Hi, thanks for the cool library, I'm enjoying using it and look forward to its ongoing development!

I seem to have encountered a bug, though: if the argument to a boost::path option has white space in it, the resulting path only includes the first word. Here's a small example:

#include <boost/filesystem/path.hpp> // using boost 1.66
#include <clara.hpp>                 // using v1.1.1
#include <iostream>
#include <string>

using path = boost::filesystem::path;
using namespace clara;

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

  path path_opt;
  std::string string_opt;

  const auto cli = Opt(path_opt, "path")["--path"]("boost path argument") |
                   Opt(string_opt, "string")["--str"]("string argument");
  const auto result = cli.parse(Args(argc, argv));
  if (!result) {
    std::cerr << "Error: " << result.errorMessage() << std::endl;
    exit(1);
  }

  std::cout << "path_opt=" << path_opt << std::endl;
  std::cout << "path(string_opt)=" << path(string_opt) << std::endl;
}

Here's what happens when I compile and run... notice below that the path "with a space" comes out to just "with".

$ c++ --std=c++11 -I. -o bug bug.cpp -lboost_system   # Apple LLVM version 9.0.0 (clang-900.0.39.2)
$ ./bug --path "with a space" --str "with a space"
path_opt="with"
path(string_opt)="with a space"
$

Of course the workaround of using a string opt then creating a path from that is viable, just not nearly as pretty.

It's possible that this isn't a Clara bug per se, but just that boost::path don't have the right construction semantics to work as a Clara Opt. If so that's a bummer -- and a little insidious, since it seems to work fine as long as there's no whitespace. And when/if you do use a path with whitespace there's no warning, just elsewhere in the app you get unexpected behavior because the path isn't what you think it is :)