2013-04-27 13:26:13 +02:00
|
|
|
/*
|
|
|
|
* Created by Phil on 22/10/2010.
|
|
|
|
* Copyright 2010 Two Blue Cubes Ltd
|
|
|
|
*
|
|
|
|
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
*/
|
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic ignored "-Wpadded"
|
|
|
|
#endif
|
|
|
|
|
2013-05-27 11:52:58 +02:00
|
|
|
#include "internal/clara.h"
|
2013-05-16 20:07:50 +02:00
|
|
|
|
2013-05-27 11:52:58 +02:00
|
|
|
#include "catch.hpp"
|
2013-05-16 20:07:50 +02:00
|
|
|
|
2013-04-27 13:26:13 +02:00
|
|
|
|
2013-05-17 09:00:57 +02:00
|
|
|
// Helper to deduce size from array literals and pass on to parser
|
|
|
|
template<size_t size, typename ConfigT>
|
2013-05-31 19:48:31 +02:00
|
|
|
std::vector<Clara::Parser::Token> parseInto( Clara::CommandLine<ConfigT>& cli, char const * (&argv)[size], ConfigT& config ) {
|
2013-05-17 09:00:57 +02:00
|
|
|
return cli.parseInto( size, argv, config );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-27 13:26:13 +02:00
|
|
|
struct TestOpt {
|
2013-05-07 19:53:14 +02:00
|
|
|
TestOpt() : number( 0 ), index( 0 ), flag( false ) {}
|
2013-04-27 13:26:13 +02:00
|
|
|
|
2013-06-04 09:37:28 +02:00
|
|
|
std::string processName;
|
2013-04-27 13:26:13 +02:00
|
|
|
std::string fileName;
|
|
|
|
int number;
|
2013-05-01 20:01:48 +02:00
|
|
|
int index;
|
2013-05-07 19:53:14 +02:00
|
|
|
bool flag;
|
2013-05-21 09:15:50 +02:00
|
|
|
std::string firstPos;
|
|
|
|
std::string secondPos;
|
|
|
|
std::string unpositional;
|
2013-05-01 20:01:48 +02:00
|
|
|
|
|
|
|
void setValidIndex( int i ) {
|
|
|
|
if( i < 0 || i > 10 )
|
|
|
|
throw std::domain_error( "index must be between 0 and 10" );
|
|
|
|
index = i;
|
|
|
|
}
|
2013-04-27 13:26:13 +02:00
|
|
|
};
|
|
|
|
|
2013-04-29 20:26:18 +02:00
|
|
|
struct TestOpt2 {
|
|
|
|
std::string description;
|
|
|
|
};
|
|
|
|
|
2013-05-14 20:31:21 +02:00
|
|
|
#ifdef CATCH_CONFIG_VARIADIC_MACROS
|
|
|
|
|
2013-05-16 20:07:50 +02:00
|
|
|
TEST_CASE( "cmdline" ) {
|
2013-04-27 13:26:13 +02:00
|
|
|
|
2013-05-02 19:41:08 +02:00
|
|
|
TestOpt config;
|
2013-05-16 20:07:50 +02:00
|
|
|
Clara::CommandLine<TestOpt> cli;
|
2013-06-04 09:37:28 +02:00
|
|
|
cli.bindProcessName( &TestOpt::processName );
|
2013-05-16 20:07:50 +02:00
|
|
|
cli.bind( &TestOpt::fileName )
|
2013-05-15 20:25:34 +02:00
|
|
|
.describe( "specifies output file" )
|
|
|
|
.shortOpt( "o" )
|
|
|
|
.longOpt( "output" )
|
2013-05-21 20:21:24 +02:00
|
|
|
.argName( "filename" );
|
2013-04-27 13:26:13 +02:00
|
|
|
|
2013-06-04 09:37:28 +02:00
|
|
|
SECTION( "process name" ) {
|
|
|
|
char const * argv[] = { "test", "-o filename.ext" };
|
|
|
|
parseInto( cli, argv, config );
|
|
|
|
|
|
|
|
CHECK( config.processName == "test" );
|
|
|
|
}
|
2013-05-23 20:01:10 +02:00
|
|
|
SECTION( "arg separated by spaces" ) {
|
2013-05-31 19:48:31 +02:00
|
|
|
char const * argv[] = { "test", "-o filename.ext" };
|
2013-05-17 09:00:57 +02:00
|
|
|
parseInto( cli, argv, config );
|
|
|
|
|
2013-05-03 09:08:46 +02:00
|
|
|
CHECK( config.fileName == "filename.ext" );
|
|
|
|
}
|
2013-05-23 20:01:10 +02:00
|
|
|
SECTION( "arg separated by colon" ) {
|
2013-04-27 13:26:13 +02:00
|
|
|
const char* argv[] = { "test", "-o:filename.ext" };
|
2013-05-17 09:00:57 +02:00
|
|
|
parseInto( cli, argv, config );
|
2013-04-27 13:26:13 +02:00
|
|
|
|
|
|
|
CHECK( config.fileName == "filename.ext" );
|
|
|
|
}
|
2013-05-23 20:01:10 +02:00
|
|
|
SECTION( "arg separated by =" ) {
|
2013-05-03 09:08:46 +02:00
|
|
|
const char* argv[] = { "test", "-o=filename.ext" };
|
2013-05-17 09:00:57 +02:00
|
|
|
parseInto( cli, argv, config );
|
2013-05-03 09:08:46 +02:00
|
|
|
|
|
|
|
CHECK( config.fileName == "filename.ext" );
|
2013-04-27 13:26:13 +02:00
|
|
|
}
|
|
|
|
SECTION( "long opt" ) {
|
2013-05-03 09:08:46 +02:00
|
|
|
const char* argv[] = { "test", "--output %stdout" };
|
2013-05-17 09:00:57 +02:00
|
|
|
parseInto( cli, argv, config );
|
2013-04-27 13:26:13 +02:00
|
|
|
|
2013-05-07 19:53:14 +02:00
|
|
|
CHECK( config.fileName == "%stdout" );
|
2013-04-27 13:26:13 +02:00
|
|
|
}
|
|
|
|
|
2013-05-16 20:07:50 +02:00
|
|
|
cli.bind( &TestOpt::number )
|
2013-05-15 20:25:34 +02:00
|
|
|
.shortOpt( "n" )
|
2013-05-21 20:21:24 +02:00
|
|
|
.argName( "an integral value" );
|
2013-04-27 13:26:13 +02:00
|
|
|
|
|
|
|
SECTION( "a number" ) {
|
2013-05-03 09:08:46 +02:00
|
|
|
const char* argv[] = { "test", "-n 42" };
|
2013-05-17 09:00:57 +02:00
|
|
|
parseInto( cli, argv, config );
|
2013-04-27 13:26:13 +02:00
|
|
|
|
|
|
|
CHECK( config.number == 42 );
|
|
|
|
}
|
|
|
|
SECTION( "not a number" ) {
|
2013-05-03 09:08:46 +02:00
|
|
|
const char* argv[] = { "test", "-n forty-two" };
|
2013-05-17 09:00:57 +02:00
|
|
|
CHECK_THROWS( parseInto( cli, argv, config ) );
|
2013-04-27 13:26:13 +02:00
|
|
|
|
|
|
|
CHECK( config.number == 0 );
|
|
|
|
}
|
|
|
|
|
2013-04-29 20:26:18 +02:00
|
|
|
SECTION( "two parsers" ) {
|
|
|
|
|
|
|
|
TestOpt config1;
|
|
|
|
TestOpt2 config2;
|
2013-05-16 20:07:50 +02:00
|
|
|
Clara::CommandLine<TestOpt2> cli2;
|
2013-05-02 19:41:08 +02:00
|
|
|
|
2013-05-16 20:07:50 +02:00
|
|
|
cli2.bind( &TestOpt2::description )
|
2013-05-15 20:25:34 +02:00
|
|
|
.describe( "description" )
|
|
|
|
.shortOpt( "d" )
|
|
|
|
.longOpt( "description" )
|
2013-05-21 20:21:24 +02:00
|
|
|
.argName( "some text" );
|
2013-04-29 20:26:18 +02:00
|
|
|
|
2013-05-03 09:08:46 +02:00
|
|
|
const char* argv[] = { "test", "-n 42", "-d some text" };
|
2013-05-17 09:00:57 +02:00
|
|
|
std::vector<Clara::Parser::Token> unusedTokens = parseInto( cli, argv, config1 );
|
2013-04-29 20:26:18 +02:00
|
|
|
|
|
|
|
CHECK( config1.number == 42 );
|
2013-05-15 20:25:34 +02:00
|
|
|
|
2013-05-16 20:07:50 +02:00
|
|
|
REQUIRE_FALSE( unusedTokens.empty() );
|
|
|
|
cli2.populate( unusedTokens, config2 );
|
|
|
|
CHECK( config2.description == "some text" );
|
2013-04-29 20:26:18 +02:00
|
|
|
}
|
2013-05-01 20:01:48 +02:00
|
|
|
|
|
|
|
SECTION( "methods" ) {
|
2013-05-16 20:07:50 +02:00
|
|
|
cli.bind( &TestOpt::setValidIndex )
|
2013-05-15 20:25:34 +02:00
|
|
|
.describe( "An index, which is an integer between 0 and 10, inclusive" )
|
|
|
|
.shortOpt( "i" )
|
2013-05-21 20:21:24 +02:00
|
|
|
.argName( "index" );
|
2013-05-01 20:01:48 +02:00
|
|
|
|
|
|
|
SECTION( "in range" ) {
|
2013-05-03 09:08:46 +02:00
|
|
|
const char* argv[] = { "test", "-i 3" };
|
2013-05-17 09:00:57 +02:00
|
|
|
parseInto( cli, argv, config );
|
2013-05-01 20:01:48 +02:00
|
|
|
|
|
|
|
REQUIRE( config.index == 3 );
|
|
|
|
}
|
|
|
|
SECTION( "out of range" ) {
|
2013-05-03 09:08:46 +02:00
|
|
|
const char* argv[] = { "test", "-i 42" };
|
2013-05-01 20:01:48 +02:00
|
|
|
|
2013-05-17 09:00:57 +02:00
|
|
|
REQUIRE_THROWS( parseInto( cli, argv, config ) );
|
2013-05-01 20:01:48 +02:00
|
|
|
}
|
2013-05-07 19:53:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION( "flags" ) {
|
2013-05-16 20:07:50 +02:00
|
|
|
cli.bind( &TestOpt::flag )
|
2013-05-15 20:25:34 +02:00
|
|
|
.describe( "A flag" )
|
|
|
|
.shortOpt( "f" );
|
2013-05-01 20:01:48 +02:00
|
|
|
|
2013-05-07 19:53:14 +02:00
|
|
|
SECTION( "set" ) {
|
|
|
|
const char* argv[] = { "test", "-f" };
|
2013-05-17 09:00:57 +02:00
|
|
|
parseInto( cli, argv, config );
|
2013-05-07 19:53:14 +02:00
|
|
|
|
|
|
|
REQUIRE( config.flag );
|
|
|
|
}
|
|
|
|
SECTION( "not set" ) {
|
|
|
|
const char* argv[] = { "test" };
|
2013-05-17 09:00:57 +02:00
|
|
|
parseInto( cli, argv, config );
|
2013-05-07 19:53:14 +02:00
|
|
|
|
|
|
|
REQUIRE( config.flag == false );
|
|
|
|
}
|
2013-05-01 20:01:48 +02:00
|
|
|
}
|
2013-05-21 09:15:50 +02:00
|
|
|
SECTION( "positional" ) {
|
|
|
|
cli.bind( &TestOpt::secondPos )
|
|
|
|
.describe( "Second position" )
|
|
|
|
.argName( "second arg" )
|
|
|
|
.position( 2 );
|
|
|
|
cli.bind( &TestOpt::unpositional )
|
|
|
|
.argName( "any arg" )
|
|
|
|
.describe( "Unpositional" );
|
|
|
|
cli.bind( &TestOpt::firstPos )
|
|
|
|
.describe( "First position" )
|
|
|
|
.argName( "first arg" )
|
|
|
|
.position( 1 );
|
|
|
|
|
2013-06-04 09:37:28 +02:00
|
|
|
// std::cout << cli.usage( "testApp" ) << std::endl;
|
2013-05-21 20:21:24 +02:00
|
|
|
|
2013-05-21 09:15:50 +02:00
|
|
|
const char* argv[] = { "test", "-f", "1st", "-o", "filename", "2nd", "3rd" };
|
|
|
|
parseInto( cli, argv, config );
|
|
|
|
|
|
|
|
REQUIRE( config.firstPos == "1st" );
|
|
|
|
REQUIRE( config.secondPos == "2nd" );
|
|
|
|
REQUIRE( config.unpositional == "3rd" );
|
|
|
|
}
|
2013-05-07 19:53:14 +02:00
|
|
|
}
|
|
|
|
|
2013-07-23 09:15:34 +02:00
|
|
|
|
2013-05-14 20:31:21 +02:00
|
|
|
#endif
|