Clara: Added support for compact short opts (e.g. -peb)

This commit is contained in:
Phil Nash 2013-05-16 08:21:13 +01:00
parent 372a8b018d
commit 58846c395c

View File

@ -262,6 +262,8 @@ namespace Clara {
}; };
public: public:
CommandLine() : seperators( " \t=:" ) {}
template<typename F> template<typename F>
ArgBinder bind( F f ) { ArgBinder bind( F f ) {
ArgBinder binder( this ); ArgBinder binder( this );
@ -320,13 +322,19 @@ namespace Clara {
ArgToken token( ArgToken::Positional, arg ); ArgToken token( ArgToken::Positional, arg );
arg = ""; arg = "";
if( token.arg[0] == '-' ) { if( token.arg[0] == '-' ) {
if( token.arg.size() > 1 && token.arg[1] == '-' ) if( token.arg.size() > 1 && token.arg[1] == '-' ) {
token = ArgToken( ArgToken::LongOpt, token.arg.substr( 2 ) ); token = ArgToken( ArgToken::LongOpt, token.arg.substr( 2 ) );
else }
else {
token = ArgToken( ArgToken::ShortOpt, token.arg.substr( 1 ) ); token = ArgToken( ArgToken::ShortOpt, token.arg.substr( 1 ) );
if( token.arg.size() > 1 && seperators.find( token.arg[1] ) == std::string::npos ) {
arg = "-" + token.arg.substr( 1 );
token.arg = token.arg.substr( 0, 1 );
}
}
} }
if( token.type != ArgToken::Positional ) { if( token.type != ArgToken::Positional ) {
std::size_t pos = token.arg.find_first_of( " \t=:" ); std::size_t pos = token.arg.find_first_of( seperators );
if( pos != std::string::npos ) { if( pos != std::string::npos ) {
arg = token.arg.substr( pos+1 ); arg = token.arg.substr( pos+1 );
token.arg = token.arg.substr( 0, pos ); token.arg = token.arg.substr( 0, pos );
@ -365,6 +373,7 @@ namespace Clara {
} }
} }
std::string seperators;
std::vector<Opt> opts; std::vector<Opt> opts;
mutable std::vector<ArgToken> unhandledTokens; // !TBD mutable std::vector<ArgToken> unhandledTokens; // !TBD
}; };
@ -507,6 +516,16 @@ TEST_CASE( "cmdline", "" ) {
} }
struct Config { struct Config {
Config()
: listTests( false ),
listTags( false ),
showPassingTests( false ),
breakIntoDebugger( false ),
noThrow( false ),
showHelp( false ),
abortAfter( 0 )
{}
bool listTests; bool listTests;
bool listTags; bool listTags;
bool showPassingTests; bool showPassingTests;
@ -590,6 +609,21 @@ TEST_CASE( "growing new Catch cli" ) {
.argName( "warning name" ); .argName( "warning name" );
std::cout << parser << std::endl; std::cout << parser << std::endl;
Config config;
const char* argv[] = { "test", "-peb" };
int argc = sizeof(argv)/sizeof(char*);
parser.parseInto( argc, argv, config );
CHECK( config.showPassingTests );
CHECK( config.noThrow );
CHECK( config.breakIntoDebugger );
//
// REQUIRE_THROWS( parser.parseInto( sizeof(argv)/sizeof(char*), argv, config ) );
} }
// !TBD still support this? // !TBD still support this?