2013-04-27 07:26:13 -04: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 05:52:58 -04:00
|
|
|
#include "internal/clara.h"
|
2013-05-16 14:07:50 -04:00
|
|
|
|
2013-05-27 05:52:58 -04:00
|
|
|
#include "catch.hpp"
|
2013-05-16 14:07:50 -04:00
|
|
|
|
2013-04-27 07:26:13 -04:00
|
|
|
|
2013-05-17 03:00:57 -04:00
|
|
|
// Helper to deduce size from array literals and pass on to parser
|
|
|
|
template<size_t size, typename ConfigT>
|
2013-05-31 13:48:31 -04:00
|
|
|
std::vector<Clara::Parser::Token> parseInto( Clara::CommandLine<ConfigT>& cli, char const * (&argv)[size], ConfigT& config ) {
|
2013-05-17 03:00:57 -04:00
|
|
|
return cli.parseInto( size, argv, config );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-27 07:26:13 -04:00
|
|
|
struct TestOpt {
|
2013-05-07 13:53:14 -04:00
|
|
|
TestOpt() : number( 0 ), index( 0 ), flag( false ) {}
|
2013-04-27 07:26:13 -04:00
|
|
|
|
2013-06-04 03:37:28 -04:00
|
|
|
std::string processName;
|
2013-04-27 07:26:13 -04:00
|
|
|
std::string fileName;
|
|
|
|
int number;
|
2013-05-01 14:01:48 -04:00
|
|
|
int index;
|
2013-05-07 13:53:14 -04:00
|
|
|
bool flag;
|
2013-05-21 03:15:50 -04:00
|
|
|
std::string firstPos;
|
|
|
|
std::string secondPos;
|
|
|
|
std::string unpositional;
|
2013-05-01 14:01:48 -04: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 07:26:13 -04:00
|
|
|
};
|
|
|
|
|
2013-04-29 14:26:18 -04:00
|
|
|
struct TestOpt2 {
|
|
|
|
std::string description;
|
|
|
|
};
|
|
|
|
|
2013-05-14 14:31:21 -04:00
|
|
|
#ifdef CATCH_CONFIG_VARIADIC_MACROS
|
|
|
|
|
2013-05-16 14:07:50 -04:00
|
|
|
TEST_CASE( "cmdline" ) {
|
2013-04-27 07:26:13 -04:00
|
|
|
|
2013-05-02 13:41:08 -04:00
|
|
|
TestOpt config;
|
2013-05-16 14:07:50 -04:00
|
|
|
Clara::CommandLine<TestOpt> cli;
|
2013-06-04 03:37:28 -04:00
|
|
|
cli.bindProcessName( &TestOpt::processName );
|
2013-05-16 14:07:50 -04:00
|
|
|
cli.bind( &TestOpt::fileName )
|
2013-05-15 14:25:34 -04:00
|
|
|
.describe( "specifies output file" )
|
|
|
|
.shortOpt( "o" )
|
|
|
|
.longOpt( "output" )
|
2013-08-16 13:57:57 -04:00
|
|
|
.hint( "filename" );
|
2013-04-27 07:26:13 -04:00
|
|
|
|
2013-06-04 03:37:28 -04:00
|
|
|
SECTION( "process name" ) {
|
|
|
|
char const * argv[] = { "test", "-o filename.ext" };
|
|
|
|
parseInto( cli, argv, config );
|
|
|
|
|
|
|
|
CHECK( config.processName == "test" );
|
|
|
|
}
|
2013-05-23 14:01:10 -04:00
|
|
|
SECTION( "arg separated by spaces" ) {
|
2013-05-31 13:48:31 -04:00
|
|
|
char const * argv[] = { "test", "-o filename.ext" };
|
2013-05-17 03:00:57 -04:00
|
|
|
parseInto( cli, argv, config );
|
|
|
|
|
2013-05-03 03:08:46 -04:00
|
|
|
CHECK( config.fileName == "filename.ext" );
|
|
|
|
}
|
2013-05-23 14:01:10 -04:00
|
|
|
SECTION( "arg separated by colon" ) {
|
2013-04-27 07:26:13 -04:00
|
|
|
const char* argv[] = { "test", "-o:filename.ext" };
|
2013-05-17 03:00:57 -04:00
|
|
|
parseInto( cli, argv, config );
|
2013-04-27 07:26:13 -04:00
|
|
|
|
|
|
|
CHECK( config.fileName == "filename.ext" );
|
|
|
|
}
|
2013-05-23 14:01:10 -04:00
|
|
|
SECTION( "arg separated by =" ) {
|
2013-05-03 03:08:46 -04:00
|
|
|
const char* argv[] = { "test", "-o=filename.ext" };
|
2013-05-17 03:00:57 -04:00
|
|
|
parseInto( cli, argv, config );
|
2013-05-03 03:08:46 -04:00
|
|
|
|
|
|
|
CHECK( config.fileName == "filename.ext" );
|
2013-04-27 07:26:13 -04:00
|
|
|
}
|
|
|
|
SECTION( "long opt" ) {
|
2013-05-03 03:08:46 -04:00
|
|
|
const char* argv[] = { "test", "--output %stdout" };
|
2013-05-17 03:00:57 -04:00
|
|
|
parseInto( cli, argv, config );
|
2013-04-27 07:26:13 -04:00
|
|
|
|
2013-05-07 13:53:14 -04:00
|
|
|
CHECK( config.fileName == "%stdout" );
|
2013-04-27 07:26:13 -04:00
|
|
|
}
|
|
|
|
|
2013-05-16 14:07:50 -04:00
|
|
|
cli.bind( &TestOpt::number )
|
2013-05-15 14:25:34 -04:00
|
|
|
.shortOpt( "n" )
|
2013-08-16 13:57:57 -04:00
|
|
|
.hint( "an integral value" );
|
2013-04-27 07:26:13 -04:00
|
|
|
|
|
|
|
SECTION( "a number" ) {
|
2013-05-03 03:08:46 -04:00
|
|
|
const char* argv[] = { "test", "-n 42" };
|
2013-05-17 03:00:57 -04:00
|
|
|
parseInto( cli, argv, config );
|
2013-04-27 07:26:13 -04:00
|
|
|
|
|
|
|
CHECK( config.number == 42 );
|
|
|
|
}
|
|
|
|
SECTION( "not a number" ) {
|
2013-05-03 03:08:46 -04:00
|
|
|
const char* argv[] = { "test", "-n forty-two" };
|
2013-05-17 03:00:57 -04:00
|
|
|
CHECK_THROWS( parseInto( cli, argv, config ) );
|
2013-04-27 07:26:13 -04:00
|
|
|
|
|
|
|
CHECK( config.number == 0 );
|
|
|
|
}
|
|
|
|
|
2013-04-29 14:26:18 -04:00
|
|
|
SECTION( "two parsers" ) {
|
|
|
|
|
|
|
|
TestOpt config1;
|
|
|
|
TestOpt2 config2;
|
2013-05-16 14:07:50 -04:00
|
|
|
Clara::CommandLine<TestOpt2> cli2;
|
2013-05-02 13:41:08 -04:00
|
|
|
|
2013-05-16 14:07:50 -04:00
|
|
|
cli2.bind( &TestOpt2::description )
|
2013-05-15 14:25:34 -04:00
|
|
|
.describe( "description" )
|
|
|
|
.shortOpt( "d" )
|
|
|
|
.longOpt( "description" )
|
2013-08-16 13:57:57 -04:00
|
|
|
.hint( "some text" );
|
2013-04-29 14:26:18 -04:00
|
|
|
|
2013-05-03 03:08:46 -04:00
|
|
|
const char* argv[] = { "test", "-n 42", "-d some text" };
|
2013-05-17 03:00:57 -04:00
|
|
|
std::vector<Clara::Parser::Token> unusedTokens = parseInto( cli, argv, config1 );
|
2013-04-29 14:26:18 -04:00
|
|
|
|
|
|
|
CHECK( config1.number == 42 );
|
2013-05-15 14:25:34 -04:00
|
|
|
|
2013-05-16 14:07:50 -04:00
|
|
|
REQUIRE_FALSE( unusedTokens.empty() );
|
|
|
|
cli2.populate( unusedTokens, config2 );
|
|
|
|
CHECK( config2.description == "some text" );
|
2013-04-29 14:26:18 -04:00
|
|
|
}
|
2013-05-01 14:01:48 -04:00
|
|
|
|
|
|
|
SECTION( "methods" ) {
|
2013-05-16 14:07:50 -04:00
|
|
|
cli.bind( &TestOpt::setValidIndex )
|
2013-05-15 14:25:34 -04:00
|
|
|
.describe( "An index, which is an integer between 0 and 10, inclusive" )
|
|
|
|
.shortOpt( "i" )
|
2013-08-16 13:57:57 -04:00
|
|
|
.hint( "index" );
|
2013-05-01 14:01:48 -04:00
|
|
|
|
|
|
|
SECTION( "in range" ) {
|
2013-05-03 03:08:46 -04:00
|
|
|
const char* argv[] = { "test", "-i 3" };
|
2013-05-17 03:00:57 -04:00
|
|
|
parseInto( cli, argv, config );
|
2013-05-01 14:01:48 -04:00
|
|
|
|
|
|
|
REQUIRE( config.index == 3 );
|
|
|
|
}
|
|
|
|
SECTION( "out of range" ) {
|
2013-05-03 03:08:46 -04:00
|
|
|
const char* argv[] = { "test", "-i 42" };
|
2013-05-01 14:01:48 -04:00
|
|
|
|
2013-05-17 03:00:57 -04:00
|
|
|
REQUIRE_THROWS( parseInto( cli, argv, config ) );
|
2013-05-01 14:01:48 -04:00
|
|
|
}
|
2013-05-07 13:53:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION( "flags" ) {
|
2013-05-16 14:07:50 -04:00
|
|
|
cli.bind( &TestOpt::flag )
|
2013-05-15 14:25:34 -04:00
|
|
|
.describe( "A flag" )
|
|
|
|
.shortOpt( "f" );
|
2013-05-01 14:01:48 -04:00
|
|
|
|
2013-05-07 13:53:14 -04:00
|
|
|
SECTION( "set" ) {
|
|
|
|
const char* argv[] = { "test", "-f" };
|
2013-05-17 03:00:57 -04:00
|
|
|
parseInto( cli, argv, config );
|
2013-05-07 13:53:14 -04:00
|
|
|
|
|
|
|
REQUIRE( config.flag );
|
|
|
|
}
|
|
|
|
SECTION( "not set" ) {
|
|
|
|
const char* argv[] = { "test" };
|
2013-05-17 03:00:57 -04:00
|
|
|
parseInto( cli, argv, config );
|
2013-05-07 13:53:14 -04:00
|
|
|
|
|
|
|
REQUIRE( config.flag == false );
|
|
|
|
}
|
2013-05-01 14:01:48 -04:00
|
|
|
}
|
2013-05-21 03:15:50 -04:00
|
|
|
SECTION( "positional" ) {
|
|
|
|
cli.bind( &TestOpt::secondPos )
|
|
|
|
.describe( "Second position" )
|
2013-08-16 13:57:57 -04:00
|
|
|
.hint( "second arg" )
|
2013-05-21 03:15:50 -04:00
|
|
|
.position( 2 );
|
|
|
|
cli.bind( &TestOpt::unpositional )
|
2013-08-16 13:57:57 -04:00
|
|
|
.hint( "any arg" )
|
2013-05-21 03:15:50 -04:00
|
|
|
.describe( "Unpositional" );
|
|
|
|
cli.bind( &TestOpt::firstPos )
|
|
|
|
.describe( "First position" )
|
2013-08-16 13:57:57 -04:00
|
|
|
.hint( "first arg" )
|
2013-05-21 03:15:50 -04:00
|
|
|
.position( 1 );
|
|
|
|
|
2013-06-04 03:37:28 -04:00
|
|
|
// std::cout << cli.usage( "testApp" ) << std::endl;
|
2013-05-21 14:21:24 -04:00
|
|
|
|
2013-05-21 03:15:50 -04: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 13:53:14 -04:00
|
|
|
}
|
|
|
|
|
2013-07-23 03:15:34 -04:00
|
|
|
|
2013-05-14 14:31:21 -04:00
|
|
|
#endif
|