catchorg / Clara

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

Parser::operator<< fails for long help strings #41

Closed stefansullivan closed 6 years ago

stefansullivan commented 6 years ago

I get a std::bad_alloc for help strings that are too wide for my console when trying to do std::cerr << cli << std::endl;

The problem comes from this line of code:

                auto row =
                        TextFlow::Column( cols.left ).width( optWidth ).indent( 2 ) +
                        TextFlow::Spacer(4) +
                        TextFlow::Column( cols.right ).width( consoleWidth - 7 - optWidth );

where optWidth is not bounded to be less than consoleWidth - 7

stefansullivan commented 6 years ago

Also, note that

        auto width( size_t newWidth ) -> Column& {
            assert( newWidth > 0 );
            m_width = newWidth;
            return *this;
        }

in clara_textflow.hpp won't ever trigger the assert, because size_t is an unsigned type, and its invocations will wrap-around if they aren't called correctly

philsquared commented 6 years ago

Hi, Sorry for the delay in getting to this. I've pushed a fix for it, along with some basic tests (I plan to do more, property-based style, tests of this sort of thing). Could you check if it works for you now?

Regarding that assert - yeah that looks like a hold over from newWidth being a signed type. Not harmful, but I'll remove it - thanks.

philsquared commented 6 years ago

HI @stefansullivan are you able to confirm (or otherwise) that this is fixed for you? As of yesterday it should be part of the v1.1.0 release.

stefansullivan commented 6 years ago

Thanks for the update @philsquared. I'll update soon and let you know.

stefansullivan commented 6 years ago

Sorry for the slow response. Just finally got around to testing newer version. Looks fine. Thanks for the fix!