Skip to content
This repository has been archived by the owner on Oct 28, 2021. It is now read-only.

Interpret a single dash or slash as positional argument #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/clara.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) } );
Expand Down
2 changes: 1 addition & 1 deletion single_include/clara.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) } );
Expand Down
25 changes: 25 additions & 0 deletions src/ClaraTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down