jarro2783 / cxxopts

Lightweight C++ command line option parser
MIT License
4.23k stars 592 forks source link

std::wstring as argument type doesn't work #299

Open AndyWatterman opened 3 years ago

AndyWatterman commented 3 years ago

I use this code:

("r, prefix", "Output file prefix", cxxopts::value<std::wstring>(prefix))

It generates error:

C2679 binary '>>': no operator found which takes a right-hand operand of type 'T' (or there is no acceptable conversion)

In this function:

  template <typename T>
    void stringstream_parser(const std::string& text, T& value)
    {
      std::stringstream in(text);
      in >> value;
      if (!in) {
        throw_or_mimic<argument_incorrect_type>(text);
      }
    }    

I also tried using icu::UnicodeString instead of std::wstring, the same error occurs.

jarro2783 commented 3 years ago

C++ string handling is a bit of a mess and I'm not sure I want to try to support wstring. Can you show me the example with UnicodeString? Because I do have a Unicode mode so I wonder if that should work.

ceztko commented 1 year ago

In Windows, as far as I as I know, the only way to receive arguments using Unicode charset is by using the wmain entrypoint, which will encode them in UTF-16 with a 2-bytes wchar_t backend. One sane behavior would be to temporarily convert them to utf-8.

matttyson commented 1 year ago

I was wanting to use this library on a Windows C++ app I'm working on and it's wchar_t top-to-bottom. I've ported this library to use wide strings throughout, it seems to be working well.

https://github.com/matttyson/cxxopts/tree/wstring3

I wasn't intending to submit this as a PR as the diff is quite horrific and I'm sure the author wouldn't want to maintain it. nevertheless you might find it useful.