From 9314694a51613a9e40956291c95daff3ba957268 Mon Sep 17 00:00:00 2001 From: Aivars Kalvans Date: Tue, 6 Mar 2018 12:56:54 +0200 Subject: [PATCH] Interpret a single dash or slash as positional argument --- include/clara.hpp | 2 +- single_include/clara.hpp | 2 +- src/ClaraTests.cpp | 25 +++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/include/clara.hpp b/include/clara.hpp index 13a7981..a2ad38d 100644 --- a/include/clara.hpp +++ b/include/clara.hpp @@ -102,7 +102,7 @@ namespace detail { if( it != itEnd ) { auto const &next = *it; - if( isOptPrefix( next[0] ) ) { + if( isOptPrefix( next[0] ) && next.size() > 1 ) { auto delimiterPos = next.find_first_of( " :=" ); if( delimiterPos != std::string::npos ) { m_tokenBuffer.push_back( { TokenType::Option, next.substr( 0, delimiterPos ) } ); diff --git a/single_include/clara.hpp b/single_include/clara.hpp index aa429e7..f67e36a 100644 --- a/single_include/clara.hpp +++ b/single_include/clara.hpp @@ -433,7 +433,7 @@ namespace detail { if( it != itEnd ) { auto const &next = *it; - if( isOptPrefix( next[0] ) ) { + if( isOptPrefix( next[0] ) && next.size() > 1 ) { auto delimiterPos = next.find_first_of( " :=" ); if( delimiterPos != std::string::npos ) { m_tokenBuffer.push_back( { TokenType::Option, next.substr( 0, delimiterPos ) } ); diff --git a/src/ClaraTests.cpp b/src/ClaraTests.cpp index f0f1af7..5d84b8f 100644 --- a/src/ClaraTests.cpp +++ b/src/ClaraTests.cpp @@ -330,6 +330,31 @@ std::string toString( Opt const& opt ) { return oss.str(); } +TEST_CASE( "dash as positional argument" ) { + std::string name; + bool showHelp = false; + auto parser + = Help( showHelp ) + | Arg( name, "input file" ) + ( "Input file" ); + + SECTION( "args" ) { + auto result = parser.parse( Args{ "cat", "filename" } ); + CHECK( result ); + REQUIRE( name == "filename" ); + } + SECTION( "dash arg" ) { + auto result = parser.parse( Args{ "cat", "-" } ); + CHECK( result ); + REQUIRE( name == "-" ); + } + SECTION( "slash arg" ) { + auto result = parser.parse( Args{ "cat", "/" } ); + CHECK( result ); + REQUIRE( name == "/" ); + } +} + TEST_CASE( "different widths" ) { std::string s;