2013-05-27 11:52:58 +02:00
|
|
|
/*
|
|
|
|
* Created by Phil on 25/05/2013.
|
|
|
|
* Copyright 2013 Two Blue Cubes Ltd. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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)
|
|
|
|
*/
|
2014-02-10 18:20:30 +01:00
|
|
|
|
2014-02-10 18:30:12 +01:00
|
|
|
// Only use header guard if we are not using an outer namespace
|
2014-03-06 09:16:06 +01:00
|
|
|
#if !defined(TWOBLUECUBES_CLARA_H_INCLUDED) || defined(STITCH_CLARA_OPEN_NAMESPACE)
|
|
|
|
|
|
|
|
#ifndef STITCH_CLARA_OPEN_NAMESPACE
|
2014-03-01 11:38:14 +01:00
|
|
|
#define TWOBLUECUBES_CLARA_H_INCLUDED
|
2014-03-06 09:16:06 +01:00
|
|
|
#define STITCH_CLARA_OPEN_NAMESPACE
|
|
|
|
#define STITCH_CLARA_CLOSE_NAMESPACE
|
|
|
|
#else
|
|
|
|
#define STITCH_CLARA_CLOSE_NAMESPACE }
|
2014-02-10 18:30:12 +01:00
|
|
|
#endif
|
2013-05-27 11:52:58 +02:00
|
|
|
|
2014-03-06 09:16:06 +01:00
|
|
|
|
|
|
|
#define STITCH_TBC_TEXT_FORMAT_OPEN_NAMESPACE STITCH_CLARA_OPEN_NAMESPACE
|
2014-03-01 11:38:14 +01:00
|
|
|
|
|
|
|
// ----------- #included from tbc_text_format.h -----------
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Created by Phil on 18/4/2013.
|
|
|
|
* Copyright 2013 Two Blue Cubes Ltd. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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)
|
|
|
|
*/
|
|
|
|
// Only use header guard if we are not using an outer namespace
|
|
|
|
#if !defined(TBC_TEXT_FORMAT_H_INCLUDED) || defined(STITCH_TBC_TEXT_FORMAT_OUTER_NAMESPACE)
|
|
|
|
#ifndef STITCH_TBC_TEXT_FORMAT_OUTER_NAMESPACE
|
|
|
|
#define TBC_TEXT_FORMAT_H_INCLUDED
|
|
|
|
#endif
|
2014-02-11 07:32:56 +01:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2014-03-01 11:38:14 +01:00
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
// Use optional outer namespace
|
|
|
|
#ifdef STITCH_TBC_TEXT_FORMAT_OUTER_NAMESPACE
|
|
|
|
namespace STITCH_TBC_TEXT_FORMAT_OUTER_NAMESPACE {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace Tbc {
|
|
|
|
|
|
|
|
#ifdef TBC_TEXT_FORMAT_CONSOLE_WIDTH
|
|
|
|
const unsigned int consoleWidth = TBC_TEXT_FORMAT_CONSOLE_WIDTH;
|
|
|
|
#else
|
|
|
|
const unsigned int consoleWidth = 80;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct TextAttributes {
|
|
|
|
TextAttributes()
|
|
|
|
: initialIndent( std::string::npos ),
|
|
|
|
indent( 0 ),
|
|
|
|
width( consoleWidth-1 ),
|
|
|
|
tabChar( '\t' )
|
|
|
|
{}
|
|
|
|
|
|
|
|
TextAttributes& setInitialIndent( std::size_t _value ) { initialIndent = _value; return *this; }
|
|
|
|
TextAttributes& setIndent( std::size_t _value ) { indent = _value; return *this; }
|
|
|
|
TextAttributes& setWidth( std::size_t _value ) { width = _value; return *this; }
|
|
|
|
TextAttributes& setTabChar( char _value ) { tabChar = _value; return *this; }
|
|
|
|
|
|
|
|
std::size_t initialIndent; // indent of first line, or npos
|
|
|
|
std::size_t indent; // indent of subsequent lines, or all if initialIndent is npos
|
|
|
|
std::size_t width; // maximum width of text, including indent. Longer text will wrap
|
|
|
|
char tabChar; // If this char is seen the indent is changed to current pos
|
|
|
|
};
|
|
|
|
|
|
|
|
class Text {
|
|
|
|
public:
|
|
|
|
Text( std::string const& _str, TextAttributes const& _attr = TextAttributes() )
|
|
|
|
: attr( _attr )
|
|
|
|
{
|
|
|
|
std::string wrappableChars = " [({.,/|\\-";
|
|
|
|
std::size_t indent = _attr.initialIndent != std::string::npos
|
|
|
|
? _attr.initialIndent
|
|
|
|
: _attr.indent;
|
|
|
|
std::string remainder = _str;
|
|
|
|
|
|
|
|
while( !remainder.empty() ) {
|
|
|
|
if( lines.size() >= 1000 ) {
|
|
|
|
lines.push_back( "... message truncated due to excessive size" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::size_t tabPos = std::string::npos;
|
|
|
|
std::size_t width = (std::min)( remainder.size(), _attr.width - indent );
|
|
|
|
std::size_t pos = remainder.find_first_of( '\n' );
|
|
|
|
if( pos <= width ) {
|
|
|
|
width = pos;
|
|
|
|
}
|
|
|
|
pos = remainder.find_last_of( _attr.tabChar, width );
|
|
|
|
if( pos != std::string::npos ) {
|
|
|
|
tabPos = pos;
|
|
|
|
if( remainder[width] == '\n' )
|
|
|
|
width--;
|
|
|
|
remainder = remainder.substr( 0, tabPos ) + remainder.substr( tabPos+1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( width == remainder.size() ) {
|
|
|
|
spliceLine( indent, remainder, width );
|
|
|
|
}
|
|
|
|
else if( remainder[width] == '\n' ) {
|
|
|
|
spliceLine( indent, remainder, width );
|
|
|
|
if( width <= 1 || remainder.size() != 1 )
|
|
|
|
remainder = remainder.substr( 1 );
|
|
|
|
indent = _attr.indent;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pos = remainder.find_last_of( wrappableChars, width );
|
|
|
|
if( pos != std::string::npos && pos > 0 ) {
|
|
|
|
spliceLine( indent, remainder, pos );
|
|
|
|
if( remainder[0] == ' ' )
|
|
|
|
remainder = remainder.substr( 1 );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
spliceLine( indent, remainder, width-1 );
|
|
|
|
lines.back() += "-";
|
|
|
|
}
|
|
|
|
if( lines.size() == 1 )
|
|
|
|
indent = _attr.indent;
|
|
|
|
if( tabPos != std::string::npos )
|
|
|
|
indent += tabPos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void spliceLine( std::size_t _indent, std::string& _remainder, std::size_t _pos ) {
|
|
|
|
lines.push_back( std::string( _indent, ' ' ) + _remainder.substr( 0, _pos ) );
|
|
|
|
_remainder = _remainder.substr( _pos );
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef std::vector<std::string>::const_iterator const_iterator;
|
|
|
|
|
|
|
|
const_iterator begin() const { return lines.begin(); }
|
|
|
|
const_iterator end() const { return lines.end(); }
|
|
|
|
std::string const& last() const { return lines.back(); }
|
|
|
|
std::size_t size() const { return lines.size(); }
|
|
|
|
std::string const& operator[]( std::size_t _index ) const { return lines[_index]; }
|
|
|
|
std::string toString() const {
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << *this;
|
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline friend std::ostream& operator << ( std::ostream& _stream, Text const& _text ) {
|
|
|
|
for( Text::const_iterator it = _text.begin(), itEnd = _text.end();
|
|
|
|
it != itEnd; ++it ) {
|
|
|
|
if( it != _text.begin() )
|
|
|
|
_stream << "\n";
|
|
|
|
_stream << *it;
|
|
|
|
}
|
|
|
|
return _stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string str;
|
|
|
|
TextAttributes attr;
|
|
|
|
std::vector<std::string> lines;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace Tbc
|
|
|
|
|
|
|
|
#ifdef STITCH_TBC_TEXT_FORMAT_OUTER_NAMESPACE
|
|
|
|
} // end outer namespace
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif // TBC_TEXT_FORMAT_H_INCLUDED
|
|
|
|
|
|
|
|
// ----------- end of #include from tbc_text_format.h -----------
|
|
|
|
// ........... back in /Users/philnash/Dev/OSS/Clara/srcs/clara.h
|
|
|
|
|
2014-03-06 09:16:06 +01:00
|
|
|
#undef STITCH_TBC_TEXT_FORMAT_OPEN_NAMESPACE
|
2014-03-01 11:38:14 +01:00
|
|
|
|
2014-03-07 07:58:33 +01:00
|
|
|
|
2014-02-11 07:32:56 +01:00
|
|
|
#include <map>
|
2014-02-11 18:40:11 +01:00
|
|
|
#include <algorithm>
|
2014-02-11 18:41:11 +01:00
|
|
|
#include <stdexcept>
|
2014-02-11 18:45:15 +01:00
|
|
|
#include <memory>
|
2013-05-27 11:52:58 +02:00
|
|
|
|
2014-02-10 18:30:12 +01:00
|
|
|
// Use optional outer namespace
|
2014-03-06 09:16:06 +01:00
|
|
|
#ifdef STITCH_CLARA_OPEN_NAMESPACE
|
|
|
|
STITCH_CLARA_OPEN_NAMESPACE
|
2014-02-10 18:20:30 +01:00
|
|
|
#endif
|
|
|
|
|
2013-05-27 11:52:58 +02:00
|
|
|
namespace Clara {
|
2014-03-06 09:16:06 +01:00
|
|
|
|
|
|
|
struct UnpositionalTag {};
|
|
|
|
|
|
|
|
extern UnpositionalTag _;
|
|
|
|
|
|
|
|
#ifdef CLARA_CONFIG_MAIN
|
|
|
|
UnpositionalTag _;
|
|
|
|
#endif
|
|
|
|
|
2013-05-27 11:52:58 +02:00
|
|
|
namespace Detail {
|
2014-02-11 07:32:56 +01:00
|
|
|
|
|
|
|
#ifdef CLARA_CONSOLE_WIDTH
|
|
|
|
const unsigned int consoleWidth = CLARA_CONFIG_CONSOLE_WIDTH;
|
|
|
|
#else
|
|
|
|
const unsigned int consoleWidth = 80;
|
|
|
|
#endif
|
|
|
|
|
2014-03-06 09:16:06 +01:00
|
|
|
using namespace Tbc;
|
|
|
|
|
|
|
|
inline bool startsWith( std::string const& str, std::string const& prefix ) {
|
|
|
|
return str.size() >= prefix.size() && str.substr( 0, prefix.size() ) == prefix;
|
|
|
|
}
|
2014-02-11 07:32:56 +01:00
|
|
|
|
2013-05-27 11:52:58 +02:00
|
|
|
template<typename T> struct RemoveConstRef{ typedef T type; };
|
|
|
|
template<typename T> struct RemoveConstRef<T&>{ typedef T type; };
|
|
|
|
template<typename T> struct RemoveConstRef<T const&>{ typedef T type; };
|
|
|
|
template<typename T> struct RemoveConstRef<T const>{ typedef T type; };
|
|
|
|
|
|
|
|
template<typename T> struct IsBool { static const bool value = false; };
|
|
|
|
template<> struct IsBool<bool> { static const bool value = true; };
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void convertInto( std::string const& _source, T& _dest ) {
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << _source;
|
|
|
|
ss >> _dest;
|
|
|
|
if( ss.fail() )
|
|
|
|
throw std::runtime_error( "Unable to convert " + _source + " to destination type" );
|
|
|
|
}
|
|
|
|
inline void convertInto( std::string const& _source, std::string& _dest ) {
|
|
|
|
_dest = _source;
|
|
|
|
}
|
|
|
|
inline void convertInto( std::string const& _source, bool& _dest ) {
|
|
|
|
std::string sourceLC = _source;
|
|
|
|
std::transform( sourceLC.begin(), sourceLC.end(), sourceLC.begin(), ::tolower );
|
2013-08-16 19:57:57 +02:00
|
|
|
if( sourceLC == "y" || sourceLC == "1" || sourceLC == "true" || sourceLC == "yes" || sourceLC == "on" )
|
2013-05-27 11:52:58 +02:00
|
|
|
_dest = true;
|
2013-08-16 19:57:57 +02:00
|
|
|
else if( sourceLC == "n" || sourceLC == "0" || sourceLC == "false" || sourceLC == "no" || sourceLC == "off" )
|
2013-05-27 11:52:58 +02:00
|
|
|
_dest = false;
|
|
|
|
else
|
2013-08-16 19:57:57 +02:00
|
|
|
throw std::runtime_error( "Expected a boolean value but did not recognise:\n '" + _source + "'" );
|
2013-05-27 11:52:58 +02:00
|
|
|
}
|
|
|
|
inline void convertInto( bool _source, bool& _dest ) {
|
|
|
|
_dest = _source;
|
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
inline void convertInto( bool, T& ) {
|
|
|
|
throw std::runtime_error( "Invalid conversion" );
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename ConfigT>
|
|
|
|
struct IArgFunction {
|
|
|
|
virtual ~IArgFunction() {}
|
2014-03-20 12:48:19 +01:00
|
|
|
# ifdef CATCH_CPP11_OR_GREATER
|
|
|
|
IArgFunction() = default;
|
|
|
|
IArgFunction( IArgFunction const& ) = default;
|
|
|
|
# endif
|
2013-05-27 11:52:58 +02:00
|
|
|
virtual void set( ConfigT& config, std::string const& value ) const = 0;
|
|
|
|
virtual void setFlag( ConfigT& config ) const = 0;
|
|
|
|
virtual bool takesArg() const = 0;
|
|
|
|
virtual IArgFunction* clone() const = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename ConfigT>
|
|
|
|
class BoundArgFunction {
|
|
|
|
public:
|
2014-03-06 09:16:06 +01:00
|
|
|
BoundArgFunction() : functionObj( NULL ) {}
|
2013-05-27 11:52:58 +02:00
|
|
|
BoundArgFunction( IArgFunction<ConfigT>* _functionObj ) : functionObj( _functionObj ) {}
|
2014-03-07 07:58:33 +01:00
|
|
|
BoundArgFunction( BoundArgFunction const& other ) : functionObj( other.functionObj ? other.functionObj->clone() : NULL ) {}
|
2013-05-27 11:52:58 +02:00
|
|
|
BoundArgFunction& operator = ( BoundArgFunction const& other ) {
|
2014-03-07 07:58:33 +01:00
|
|
|
IArgFunction<ConfigT>* newFunctionObj = other.functionObj ? other.functionObj->clone() : NULL;
|
2013-05-27 11:52:58 +02:00
|
|
|
delete functionObj;
|
|
|
|
functionObj = newFunctionObj;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
~BoundArgFunction() { delete functionObj; }
|
|
|
|
|
|
|
|
void set( ConfigT& config, std::string const& value ) const {
|
|
|
|
functionObj->set( config, value );
|
|
|
|
}
|
|
|
|
void setFlag( ConfigT& config ) const {
|
|
|
|
functionObj->setFlag( config );
|
|
|
|
}
|
|
|
|
bool takesArg() const { return functionObj->takesArg(); }
|
2014-03-24 19:15:04 +01:00
|
|
|
|
|
|
|
bool isSet() const {
|
|
|
|
return functionObj != NULL;
|
|
|
|
}
|
2013-05-27 11:52:58 +02:00
|
|
|
private:
|
|
|
|
IArgFunction<ConfigT>* functionObj;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-06-04 09:37:28 +02:00
|
|
|
template<typename C>
|
|
|
|
struct NullBinder : IArgFunction<C>{
|
|
|
|
virtual void set( C&, std::string const& ) const {}
|
|
|
|
virtual void setFlag( C& ) const {}
|
|
|
|
virtual bool takesArg() const { return true; }
|
|
|
|
virtual IArgFunction<C>* clone() const { return new NullBinder( *this ); }
|
|
|
|
};
|
|
|
|
|
2013-05-27 11:52:58 +02:00
|
|
|
template<typename C, typename M>
|
|
|
|
struct BoundDataMember : IArgFunction<C>{
|
|
|
|
BoundDataMember( M C::* _member ) : member( _member ) {}
|
|
|
|
virtual void set( C& p, std::string const& stringValue ) const {
|
|
|
|
convertInto( stringValue, p.*member );
|
|
|
|
}
|
|
|
|
virtual void setFlag( C& p ) const {
|
|
|
|
convertInto( true, p.*member );
|
|
|
|
}
|
|
|
|
virtual bool takesArg() const { return !IsBool<M>::value; }
|
|
|
|
virtual IArgFunction<C>* clone() const { return new BoundDataMember( *this ); }
|
|
|
|
M C::* member;
|
|
|
|
};
|
|
|
|
template<typename C, typename M>
|
|
|
|
struct BoundUnaryMethod : IArgFunction<C>{
|
|
|
|
BoundUnaryMethod( void (C::*_member)( M ) ) : member( _member ) {}
|
|
|
|
virtual void set( C& p, std::string const& stringValue ) const {
|
|
|
|
typename RemoveConstRef<M>::type value;
|
|
|
|
convertInto( stringValue, value );
|
|
|
|
(p.*member)( value );
|
|
|
|
}
|
|
|
|
virtual void setFlag( C& p ) const {
|
|
|
|
typename RemoveConstRef<M>::type value;
|
|
|
|
convertInto( true, value );
|
|
|
|
(p.*member)( value );
|
|
|
|
}
|
|
|
|
virtual bool takesArg() const { return !IsBool<M>::value; }
|
|
|
|
virtual IArgFunction<C>* clone() const { return new BoundUnaryMethod( *this ); }
|
|
|
|
void (C::*member)( M );
|
|
|
|
};
|
|
|
|
template<typename C>
|
|
|
|
struct BoundNullaryMethod : IArgFunction<C>{
|
|
|
|
BoundNullaryMethod( void (C::*_member)() ) : member( _member ) {}
|
|
|
|
virtual void set( C& p, std::string const& stringValue ) const {
|
|
|
|
bool value;
|
|
|
|
convertInto( stringValue, value );
|
|
|
|
if( value )
|
|
|
|
(p.*member)();
|
|
|
|
}
|
|
|
|
virtual void setFlag( C& p ) const {
|
|
|
|
(p.*member)();
|
|
|
|
}
|
|
|
|
virtual bool takesArg() const { return false; }
|
|
|
|
virtual IArgFunction<C>* clone() const { return new BoundNullaryMethod( *this ); }
|
|
|
|
void (C::*member)();
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename C>
|
|
|
|
struct BoundUnaryFunction : IArgFunction<C>{
|
|
|
|
BoundUnaryFunction( void (*_function)( C& ) ) : function( _function ) {}
|
|
|
|
virtual void set( C& obj, std::string const& stringValue ) const {
|
|
|
|
bool value;
|
|
|
|
convertInto( stringValue, value );
|
|
|
|
if( value )
|
|
|
|
function( obj );
|
|
|
|
}
|
|
|
|
virtual void setFlag( C& p ) const {
|
|
|
|
function( p );
|
|
|
|
}
|
|
|
|
virtual bool takesArg() const { return false; }
|
|
|
|
virtual IArgFunction<C>* clone() const { return new BoundUnaryFunction( *this ); }
|
|
|
|
void (*function)( C& );
|
|
|
|
};
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2013-05-27 11:52:58 +02:00
|
|
|
template<typename C, typename T>
|
|
|
|
struct BoundBinaryFunction : IArgFunction<C>{
|
|
|
|
BoundBinaryFunction( void (*_function)( C&, T ) ) : function( _function ) {}
|
|
|
|
virtual void set( C& obj, std::string const& stringValue ) const {
|
|
|
|
typename RemoveConstRef<T>::type value;
|
|
|
|
convertInto( stringValue, value );
|
|
|
|
function( obj, value );
|
|
|
|
}
|
|
|
|
virtual void setFlag( C& obj ) const {
|
|
|
|
typename RemoveConstRef<T>::type value;
|
|
|
|
convertInto( true, value );
|
|
|
|
function( obj, value );
|
|
|
|
}
|
|
|
|
virtual bool takesArg() const { return !IsBool<T>::value; }
|
|
|
|
virtual IArgFunction<C>* clone() const { return new BoundBinaryFunction( *this ); }
|
|
|
|
void (*function)( C&, T );
|
|
|
|
};
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2013-05-27 11:52:58 +02:00
|
|
|
} // namespace Detail
|
|
|
|
|
|
|
|
struct Parser {
|
|
|
|
Parser() : separators( " \t=:" ) {}
|
|
|
|
|
|
|
|
struct Token {
|
|
|
|
enum Type { Positional, ShortOpt, LongOpt };
|
|
|
|
Token( Type _type, std::string const& _data ) : type( _type ), data( _data ) {}
|
|
|
|
Type type;
|
|
|
|
std::string data;
|
|
|
|
};
|
|
|
|
|
2013-05-31 19:48:31 +02:00
|
|
|
void parseIntoTokens( int argc, char const * const * argv, std::vector<Parser::Token>& tokens ) const {
|
2013-12-23 11:24:06 +01:00
|
|
|
const std::string doubleDash = "--";
|
|
|
|
for( int i = 1; i < argc && argv[i] != doubleDash; ++i )
|
2013-05-27 11:52:58 +02:00
|
|
|
parseIntoTokens( argv[i] , tokens);
|
2013-07-03 20:14:59 +02:00
|
|
|
}
|
2013-05-27 11:52:58 +02:00
|
|
|
void parseIntoTokens( std::string arg, std::vector<Parser::Token>& tokens ) const {
|
|
|
|
while( !arg.empty() ) {
|
|
|
|
Parser::Token token( Parser::Token::Positional, arg );
|
|
|
|
arg = "";
|
|
|
|
if( token.data[0] == '-' ) {
|
|
|
|
if( token.data.size() > 1 && token.data[1] == '-' ) {
|
|
|
|
token = Parser::Token( Parser::Token::LongOpt, token.data.substr( 2 ) );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
token = Parser::Token( Parser::Token::ShortOpt, token.data.substr( 1 ) );
|
|
|
|
if( token.data.size() > 1 && separators.find( token.data[1] ) == std::string::npos ) {
|
|
|
|
arg = "-" + token.data.substr( 1 );
|
|
|
|
token.data = token.data.substr( 0, 1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( token.type != Parser::Token::Positional ) {
|
|
|
|
std::size_t pos = token.data.find_first_of( separators );
|
|
|
|
if( pos != std::string::npos ) {
|
|
|
|
arg = token.data.substr( pos+1 );
|
|
|
|
token.data = token.data.substr( 0, pos );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tokens.push_back( token );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::string separators;
|
|
|
|
};
|
|
|
|
|
2014-03-17 19:40:58 +01:00
|
|
|
template<typename ConfigT>
|
|
|
|
struct CommonArgProperties {
|
|
|
|
CommonArgProperties() {}
|
|
|
|
CommonArgProperties( Detail::BoundArgFunction<ConfigT> const& _boundField ) : boundField( _boundField ) {}
|
|
|
|
|
|
|
|
Detail::BoundArgFunction<ConfigT> boundField;
|
|
|
|
std::string description;
|
2014-03-24 19:15:04 +01:00
|
|
|
std::string detail;
|
2014-03-17 19:40:58 +01:00
|
|
|
std::string placeholder; // Only value if boundField takes an arg
|
|
|
|
|
|
|
|
bool takesArg() const {
|
|
|
|
return !placeholder.empty();
|
|
|
|
}
|
2014-03-24 19:15:04 +01:00
|
|
|
void validate() const {
|
|
|
|
if( !boundField.isSet() )
|
|
|
|
throw std::logic_error( "option not bound" );
|
|
|
|
}
|
2014-03-17 19:40:58 +01:00
|
|
|
};
|
|
|
|
struct OptionArgProperties {
|
|
|
|
std::vector<std::string> shortNames;
|
|
|
|
std::string longName;
|
|
|
|
|
|
|
|
bool hasShortName( std::string const& shortName ) const {
|
|
|
|
return std::find( shortNames.begin(), shortNames.end(), shortName ) != shortNames.end();
|
|
|
|
}
|
|
|
|
bool hasLongName( std::string const& _longName ) const {
|
|
|
|
return _longName == longName;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
struct PositionalArgProperties {
|
|
|
|
PositionalArgProperties() : position( -1 ) {}
|
|
|
|
int position; // -1 means non-positional (floating)
|
|
|
|
|
|
|
|
bool isFixedPositional() const {
|
|
|
|
return position != -1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-27 11:52:58 +02:00
|
|
|
template<typename ConfigT>
|
|
|
|
class CommandLine {
|
|
|
|
|
2014-03-17 19:40:58 +01:00
|
|
|
struct Arg : CommonArgProperties<ConfigT>, OptionArgProperties, PositionalArgProperties {
|
|
|
|
Arg() {}
|
|
|
|
Arg( Detail::BoundArgFunction<ConfigT> const& _boundField ) : CommonArgProperties<ConfigT>( _boundField ) {}
|
|
|
|
|
|
|
|
using CommonArgProperties<ConfigT>::placeholder; // !TBD
|
2013-05-27 11:52:58 +02:00
|
|
|
|
|
|
|
std::string dbgName() const {
|
|
|
|
if( !longName.empty() )
|
|
|
|
return "--" + longName;
|
|
|
|
if( !shortNames.empty() )
|
|
|
|
return "-" + shortNames[0];
|
|
|
|
return "positional args";
|
|
|
|
}
|
|
|
|
std::string commands() const {
|
|
|
|
std::ostringstream oss;
|
|
|
|
bool first = true;
|
|
|
|
std::vector<std::string>::const_iterator it = shortNames.begin(), itEnd = shortNames.end();
|
|
|
|
for(; it != itEnd; ++it ) {
|
|
|
|
if( first )
|
|
|
|
first = false;
|
|
|
|
else
|
|
|
|
oss << ", ";
|
|
|
|
oss << "-" << *it;
|
|
|
|
}
|
|
|
|
if( !longName.empty() ) {
|
|
|
|
if( !first )
|
|
|
|
oss << ", ";
|
|
|
|
oss << "--" << longName;
|
|
|
|
}
|
2014-03-06 09:16:06 +01:00
|
|
|
if( !placeholder.empty() )
|
|
|
|
oss << " <" << placeholder << ">";
|
2013-05-27 11:52:58 +02:00
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-11-15 17:21:22 +01:00
|
|
|
// NOTE: std::auto_ptr is deprecated in c++11/c++0x
|
|
|
|
#if defined(__cplusplus) && __cplusplus > 199711L
|
|
|
|
typedef std::unique_ptr<Arg> ArgAutoPtr;
|
|
|
|
#else
|
|
|
|
typedef std::auto_ptr<Arg> ArgAutoPtr;
|
|
|
|
#endif
|
|
|
|
|
2014-03-17 19:40:58 +01:00
|
|
|
friend void addOptName( Arg& arg, std::string const& optName )
|
|
|
|
{
|
|
|
|
if( optName.empty() )
|
|
|
|
return;
|
|
|
|
if( Detail::startsWith( optName, "--" ) ) {
|
|
|
|
if( !arg.longName.empty() )
|
|
|
|
throw std::logic_error( "Only one long opt may be specified. '"
|
|
|
|
+ arg.longName
|
|
|
|
+ "' already specified, now attempting to add '"
|
|
|
|
+ optName + "'" );
|
|
|
|
arg.longName = optName.substr( 2 );
|
|
|
|
}
|
|
|
|
else if( Detail::startsWith( optName, "-" ) )
|
|
|
|
arg.shortNames.push_back( optName.substr( 1 ) );
|
|
|
|
else
|
|
|
|
throw std::logic_error( "option must begin with - or --. Option was: '" + optName + "'" );
|
|
|
|
}
|
|
|
|
friend void setPositionalArg( Arg& arg, int position )
|
|
|
|
{
|
|
|
|
arg.position = position;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-06 09:16:06 +01:00
|
|
|
class ArgBuilder {
|
2013-05-27 11:52:58 +02:00
|
|
|
public:
|
2014-03-24 19:15:04 +01:00
|
|
|
ArgBuilder( Arg* arg ) : m_arg( arg ) {}
|
2014-03-17 19:40:58 +01:00
|
|
|
|
|
|
|
// Bind a non-boolean data member (requires placeholder string)
|
|
|
|
template<typename C, typename M>
|
|
|
|
void bind( M C::* field, std::string const& placeholder ) {
|
2014-03-24 19:15:04 +01:00
|
|
|
m_arg->boundField = new Detail::BoundDataMember<C,M>( field );
|
|
|
|
m_arg->placeholder = placeholder;
|
2014-03-17 19:40:58 +01:00
|
|
|
}
|
|
|
|
// Bind a boolean data member (no placeholder required)
|
|
|
|
template<typename C>
|
|
|
|
void bind( bool C::* field ) {
|
2014-03-24 19:15:04 +01:00
|
|
|
m_arg->boundField = new Detail::BoundDataMember<C,bool>( field );
|
2013-05-27 11:52:58 +02:00
|
|
|
}
|
2014-03-06 09:16:06 +01:00
|
|
|
|
2014-03-17 19:40:58 +01:00
|
|
|
// Bind a method taking a single, non-boolean argument (requires a placeholder string)
|
|
|
|
template<typename C, typename M>
|
2014-03-24 19:15:04 +01:00
|
|
|
void bind( void (C::* unaryMethod)( M ), std::string const& placeholder ) {
|
|
|
|
m_arg->boundField = new Detail::BoundUnaryMethod<C,M>( unaryMethod );
|
|
|
|
m_arg->placeholder = placeholder;
|
2013-05-27 11:52:58 +02:00
|
|
|
}
|
2014-03-06 09:16:06 +01:00
|
|
|
|
2014-03-17 19:40:58 +01:00
|
|
|
// Bind a method taking a single, boolean argument (no placeholder string required)
|
|
|
|
template<typename C>
|
2014-03-24 19:15:04 +01:00
|
|
|
void bind( void (C::* unaryMethod)( bool ) ) {
|
|
|
|
m_arg->boundField = new Detail::BoundUnaryMethod<C,bool>( unaryMethod );
|
2013-05-27 11:52:58 +02:00
|
|
|
}
|
2014-03-17 19:40:58 +01:00
|
|
|
|
|
|
|
// Bind a method that takes no arguments (will be called if opt is present)
|
|
|
|
template<typename C>
|
2014-03-24 19:15:04 +01:00
|
|
|
void bind( void (C::* nullaryMethod)() ) {
|
|
|
|
m_arg->boundField = new Detail::BoundNullaryMethod<C>( nullaryMethod );
|
2014-03-06 09:16:06 +01:00
|
|
|
}
|
|
|
|
|
2014-03-17 19:40:58 +01:00
|
|
|
// Bind a free function taking a single argument - the object to operate on (no placeholder string required)
|
|
|
|
template<typename C>
|
2014-03-24 19:15:04 +01:00
|
|
|
void bind( void (* unaryFunction)( C& ) ) {
|
|
|
|
m_arg->boundField = new Detail::BoundUnaryFunction<C>( unaryFunction );
|
2014-03-17 19:40:58 +01:00
|
|
|
}
|
2014-03-06 09:16:06 +01:00
|
|
|
|
2014-03-17 19:40:58 +01:00
|
|
|
// Bind a free function taking a single argument - the object to operate on (requires a placeholder string)
|
|
|
|
template<typename C, typename T>
|
2014-03-24 19:15:04 +01:00
|
|
|
void bind( void (* binaryFunction)( C&, T ), std::string const& placeholder ) {
|
|
|
|
m_arg->boundField = new Detail::BoundBinaryFunction<C, T>( binaryFunction );
|
|
|
|
m_arg->placeholder = placeholder;
|
2013-05-27 11:52:58 +02:00
|
|
|
}
|
2014-03-06 09:16:06 +01:00
|
|
|
|
|
|
|
ArgBuilder& describe( std::string const& description ) {
|
2014-03-24 19:15:04 +01:00
|
|
|
m_arg->description = description;
|
2013-05-27 11:52:58 +02:00
|
|
|
return *this;
|
|
|
|
}
|
2014-03-24 19:15:04 +01:00
|
|
|
ArgBuilder& detail( std::string const& detail ) {
|
|
|
|
m_arg->detail = detail;
|
2013-05-27 11:52:58 +02:00
|
|
|
return *this;
|
|
|
|
}
|
2014-03-06 09:16:06 +01:00
|
|
|
|
2014-03-17 19:40:58 +01:00
|
|
|
protected:
|
2014-03-24 19:15:04 +01:00
|
|
|
Arg* m_arg;
|
2013-05-27 11:52:58 +02:00
|
|
|
};
|
2014-03-17 19:40:58 +01:00
|
|
|
|
2014-03-06 09:16:06 +01:00
|
|
|
class OptBuilder : public ArgBuilder {
|
|
|
|
public:
|
2014-03-24 19:15:04 +01:00
|
|
|
OptBuilder( Arg* arg ) : ArgBuilder( arg ) {}
|
2014-03-06 09:16:06 +01:00
|
|
|
OptBuilder( OptBuilder& other ) : ArgBuilder( other ) {}
|
|
|
|
|
|
|
|
OptBuilder& operator[]( std::string const& optName ) {
|
2014-03-24 19:15:04 +01:00
|
|
|
addOptName( *ArgBuilder::m_arg, optName );
|
2014-03-06 09:16:06 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
2013-05-27 11:52:58 +02:00
|
|
|
|
|
|
|
public:
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2013-06-04 09:37:28 +02:00
|
|
|
CommandLine()
|
|
|
|
: m_boundProcessName( new Detail::NullBinder<ConfigT>() ),
|
2013-12-20 20:06:02 +01:00
|
|
|
m_highestSpecifiedArgPosition( 0 ),
|
|
|
|
m_throwOnUnrecognisedTokens( false )
|
2013-06-04 09:37:28 +02:00
|
|
|
{}
|
|
|
|
CommandLine( CommandLine const& other )
|
|
|
|
: m_boundProcessName( other.m_boundProcessName ),
|
|
|
|
m_options ( other.m_options ),
|
|
|
|
m_positionalArgs( other.m_positionalArgs ),
|
2013-12-20 20:06:02 +01:00
|
|
|
m_highestSpecifiedArgPosition( other.m_highestSpecifiedArgPosition ),
|
|
|
|
m_throwOnUnrecognisedTokens( other.m_throwOnUnrecognisedTokens )
|
2013-06-04 09:37:28 +02:00
|
|
|
{
|
2014-03-17 19:40:58 +01:00
|
|
|
if( other.m_floatingArg.get() )
|
|
|
|
m_floatingArg = ArgAutoPtr( new Arg( *other.m_floatingArg ) );
|
2013-06-04 09:37:28 +02:00
|
|
|
}
|
2013-05-27 11:52:58 +02:00
|
|
|
|
2013-12-20 20:06:02 +01:00
|
|
|
CommandLine& setThrowOnUnrecognisedTokens( bool shouldThrow = true ) {
|
|
|
|
m_throwOnUnrecognisedTokens = shouldThrow;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-03-06 09:16:06 +01:00
|
|
|
|
|
|
|
OptBuilder operator[]( std::string const& optName ) {
|
2014-03-17 19:40:58 +01:00
|
|
|
m_options.push_back( Arg() );
|
|
|
|
addOptName( m_options.back(), optName );
|
2014-03-24 19:15:04 +01:00
|
|
|
OptBuilder builder( &m_options.back() );
|
2014-03-06 09:16:06 +01:00
|
|
|
return builder;
|
|
|
|
}
|
|
|
|
|
|
|
|
ArgBuilder operator[]( int position ) {
|
2014-03-17 19:40:58 +01:00
|
|
|
m_positionalArgs.insert( std::make_pair( position, Arg() ) );
|
|
|
|
if( position > m_highestSpecifiedArgPosition )
|
|
|
|
m_highestSpecifiedArgPosition = position;
|
|
|
|
setPositionalArg( m_positionalArgs[position], position );
|
2014-03-24 19:15:04 +01:00
|
|
|
ArgBuilder builder( &m_positionalArgs[position] );
|
2014-03-06 09:16:06 +01:00
|
|
|
return builder;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invoke this with the _ instance
|
|
|
|
ArgBuilder operator[]( UnpositionalTag ) {
|
2014-03-17 19:40:58 +01:00
|
|
|
if( m_floatingArg.get() )
|
|
|
|
throw std::logic_error( "Only one unpositional argument can be added" );
|
|
|
|
m_floatingArg = ArgAutoPtr( new Arg() );
|
2014-03-24 19:15:04 +01:00
|
|
|
ArgBuilder builder( m_floatingArg.get() );
|
2014-03-06 09:16:06 +01:00
|
|
|
return builder;
|
2013-05-27 11:52:58 +02:00
|
|
|
}
|
2014-03-06 09:16:06 +01:00
|
|
|
|
2014-03-17 19:40:58 +01:00
|
|
|
template<typename C, typename M>
|
|
|
|
void bindProcessName( M C::* field ) {
|
|
|
|
m_boundProcessName = new Detail::BoundDataMember<C,M>( field );
|
|
|
|
}
|
|
|
|
template<typename C, typename M>
|
|
|
|
void bindProcessName( void (C::*_unaryMethod)( M ) ) {
|
|
|
|
m_boundProcessName = new Detail::BoundUnaryMethod<C,M>( _unaryMethod );
|
2013-06-04 09:37:28 +02:00
|
|
|
}
|
2013-05-27 11:52:58 +02:00
|
|
|
|
2014-02-11 07:32:56 +01:00
|
|
|
void optUsage( std::ostream& os, std::size_t indent = 0, std::size_t width = Detail::consoleWidth ) const {
|
2013-05-27 11:52:58 +02:00
|
|
|
typename std::vector<Arg>::const_iterator itBegin = m_options.begin(), itEnd = m_options.end(), it;
|
|
|
|
std::size_t maxWidth = 0;
|
|
|
|
for( it = itBegin; it != itEnd; ++it )
|
|
|
|
maxWidth = (std::max)( maxWidth, it->commands().size() );
|
|
|
|
|
|
|
|
for( it = itBegin; it != itEnd; ++it ) {
|
2014-02-11 07:32:56 +01:00
|
|
|
Detail::Text usage( it->commands(), Detail::TextAttributes()
|
2013-05-27 11:52:58 +02:00
|
|
|
.setWidth( maxWidth+indent )
|
|
|
|
.setIndent( indent ) );
|
2014-02-11 07:32:56 +01:00
|
|
|
Detail::Text desc( it->description, Detail::TextAttributes()
|
2014-03-24 19:15:04 +01:00
|
|
|
.setWidth( width - maxWidth - 3 ) );
|
2013-05-27 11:52:58 +02:00
|
|
|
|
2013-06-29 13:11:32 +02:00
|
|
|
for( std::size_t i = 0; i < (std::max)( usage.size(), desc.size() ); ++i ) {
|
2013-05-27 11:52:58 +02:00
|
|
|
std::string usageCol = i < usage.size() ? usage[i] : "";
|
|
|
|
os << usageCol;
|
|
|
|
|
|
|
|
if( i < desc.size() && !desc[i].empty() )
|
|
|
|
os << std::string( indent + 2 + maxWidth - usageCol.size(), ' ' )
|
|
|
|
<< desc[i];
|
|
|
|
os << "\n";
|
2013-07-03 20:14:59 +02:00
|
|
|
}
|
|
|
|
}
|
2013-05-27 11:52:58 +02:00
|
|
|
}
|
|
|
|
std::string optUsage() const {
|
|
|
|
std::ostringstream oss;
|
|
|
|
optUsage( oss );
|
|
|
|
return oss.str();
|
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2013-05-27 11:52:58 +02:00
|
|
|
void argSynopsis( std::ostream& os ) const {
|
|
|
|
for( int i = 1; i <= m_highestSpecifiedArgPosition; ++i ) {
|
|
|
|
if( i > 1 )
|
|
|
|
os << " ";
|
|
|
|
typename std::map<int, Arg>::const_iterator it = m_positionalArgs.find( i );
|
|
|
|
if( it != m_positionalArgs.end() )
|
2014-03-06 09:16:06 +01:00
|
|
|
os << "<" << it->second.placeholder << ">";
|
2014-03-17 19:40:58 +01:00
|
|
|
else if( m_floatingArg.get() )
|
|
|
|
os << "<" << m_floatingArg->placeholder << ">";
|
2013-05-27 11:52:58 +02:00
|
|
|
else
|
|
|
|
throw std::logic_error( "non consecutive positional arguments with no floating args" );
|
|
|
|
}
|
|
|
|
// !TBD No indication of mandatory args
|
2014-03-17 19:40:58 +01:00
|
|
|
if( m_floatingArg.get() ) {
|
2013-05-27 11:52:58 +02:00
|
|
|
if( m_highestSpecifiedArgPosition > 1 )
|
|
|
|
os << " ";
|
2014-03-17 19:40:58 +01:00
|
|
|
os << "[<" << m_floatingArg->placeholder << "> ...]";
|
2013-05-27 11:52:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
std::string argSynopsis() const {
|
|
|
|
std::ostringstream oss;
|
|
|
|
argSynopsis( oss );
|
|
|
|
return oss.str();
|
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2013-05-27 11:52:58 +02:00
|
|
|
void usage( std::ostream& os, std::string const& procName ) const {
|
2014-03-24 19:15:04 +01:00
|
|
|
validate();
|
2013-05-27 11:52:58 +02:00
|
|
|
os << "usage:\n " << procName << " ";
|
|
|
|
argSynopsis( os );
|
|
|
|
if( !m_options.empty() ) {
|
|
|
|
os << " [options]\n\nwhere options are: \n";
|
2013-07-03 20:14:59 +02:00
|
|
|
optUsage( os, 2 );
|
2013-05-27 11:52:58 +02:00
|
|
|
}
|
|
|
|
os << "\n";
|
|
|
|
}
|
|
|
|
std::string usage( std::string const& procName ) const {
|
|
|
|
std::ostringstream oss;
|
|
|
|
usage( oss, procName );
|
|
|
|
return oss.str();
|
2013-07-03 20:14:59 +02:00
|
|
|
}
|
|
|
|
|
2014-03-24 19:15:04 +01:00
|
|
|
ConfigT parse( int argc, char const * const * argv ) const {
|
2014-03-01 11:38:14 +01:00
|
|
|
ConfigT config;
|
|
|
|
parseInto( argc, argv, config );
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2013-05-31 19:48:31 +02:00
|
|
|
std::vector<Parser::Token> parseInto( int argc, char const * const * argv, ConfigT& config ) const {
|
2013-06-05 09:18:52 +02:00
|
|
|
std::string processName = argv[0];
|
|
|
|
std::size_t lastSlash = processName.find_last_of( "/\\" );
|
|
|
|
if( lastSlash != std::string::npos )
|
|
|
|
processName = processName.substr( lastSlash+1 );
|
|
|
|
m_boundProcessName.set( config, processName );
|
2013-05-27 11:52:58 +02:00
|
|
|
std::vector<Parser::Token> tokens;
|
|
|
|
Parser parser;
|
|
|
|
parser.parseIntoTokens( argc, argv, tokens );
|
|
|
|
return populate( tokens, config );
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Parser::Token> populate( std::vector<Parser::Token> const& tokens, ConfigT& config ) const {
|
2014-03-24 19:15:04 +01:00
|
|
|
validate();
|
2013-05-27 11:52:58 +02:00
|
|
|
std::vector<Parser::Token> unusedTokens = populateOptions( tokens, config );
|
|
|
|
unusedTokens = populateFixedArgs( unusedTokens, config );
|
|
|
|
unusedTokens = populateFloatingArgs( unusedTokens, config );
|
|
|
|
return unusedTokens;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Parser::Token> populateOptions( std::vector<Parser::Token> const& tokens, ConfigT& config ) const {
|
|
|
|
std::vector<Parser::Token> unusedTokens;
|
2013-12-20 20:06:02 +01:00
|
|
|
std::vector<std::string> errors;
|
2013-05-27 11:52:58 +02:00
|
|
|
for( std::size_t i = 0; i < tokens.size(); ++i ) {
|
|
|
|
Parser::Token const& token = tokens[i];
|
|
|
|
typename std::vector<Arg>::const_iterator it = m_options.begin(), itEnd = m_options.end();
|
|
|
|
for(; it != itEnd; ++it ) {
|
|
|
|
Arg const& arg = *it;
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2013-06-04 09:37:28 +02:00
|
|
|
try {
|
|
|
|
if( ( token.type == Parser::Token::ShortOpt && arg.hasShortName( token.data ) ) ||
|
|
|
|
( token.type == Parser::Token::LongOpt && arg.hasLongName( token.data ) ) ) {
|
|
|
|
if( arg.takesArg() ) {
|
|
|
|
if( i == tokens.size()-1 || tokens[i+1].type != Parser::Token::Positional )
|
2013-12-20 20:06:02 +01:00
|
|
|
errors.push_back( "Expected argument to option: " + token.data );
|
|
|
|
else
|
|
|
|
arg.boundField.set( config, tokens[++i].data );
|
2013-06-04 09:37:28 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
arg.boundField.setFlag( config );
|
|
|
|
}
|
|
|
|
break;
|
2013-05-27 11:52:58 +02:00
|
|
|
}
|
2013-06-04 09:37:28 +02:00
|
|
|
}
|
|
|
|
catch( std::exception& ex ) {
|
2013-12-20 20:06:02 +01:00
|
|
|
errors.push_back( std::string( ex.what() ) + "\n- while parsing: (" + arg.commands() + ")" );
|
2013-05-27 11:52:58 +02:00
|
|
|
}
|
|
|
|
}
|
2013-12-20 20:06:02 +01:00
|
|
|
if( it == itEnd ) {
|
|
|
|
if( token.type == Parser::Token::Positional || !m_throwOnUnrecognisedTokens )
|
|
|
|
unusedTokens.push_back( token );
|
|
|
|
else if( m_throwOnUnrecognisedTokens )
|
|
|
|
errors.push_back( "unrecognised option: " + token.data );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( !errors.empty() ) {
|
|
|
|
std::ostringstream oss;
|
|
|
|
for( std::vector<std::string>::const_iterator it = errors.begin(), itEnd = errors.end();
|
|
|
|
it != itEnd;
|
|
|
|
++it ) {
|
|
|
|
if( it != errors.begin() )
|
|
|
|
oss << "\n";
|
|
|
|
oss << *it;
|
|
|
|
}
|
|
|
|
throw std::runtime_error( oss.str() );
|
2013-05-27 11:52:58 +02:00
|
|
|
}
|
|
|
|
return unusedTokens;
|
|
|
|
}
|
|
|
|
std::vector<Parser::Token> populateFixedArgs( std::vector<Parser::Token> const& tokens, ConfigT& config ) const {
|
|
|
|
std::vector<Parser::Token> unusedTokens;
|
|
|
|
int position = 1;
|
|
|
|
for( std::size_t i = 0; i < tokens.size(); ++i ) {
|
|
|
|
Parser::Token const& token = tokens[i];
|
|
|
|
typename std::map<int, Arg>::const_iterator it = m_positionalArgs.find( position );
|
|
|
|
if( it != m_positionalArgs.end() )
|
|
|
|
it->second.boundField.set( config, token.data );
|
|
|
|
else
|
|
|
|
unusedTokens.push_back( token );
|
|
|
|
if( token.type == Parser::Token::Positional )
|
2013-07-03 20:14:59 +02:00
|
|
|
position++;
|
2013-05-27 11:52:58 +02:00
|
|
|
}
|
|
|
|
return unusedTokens;
|
|
|
|
}
|
|
|
|
std::vector<Parser::Token> populateFloatingArgs( std::vector<Parser::Token> const& tokens, ConfigT& config ) const {
|
2014-03-17 19:40:58 +01:00
|
|
|
if( !m_floatingArg.get() )
|
2013-05-27 11:52:58 +02:00
|
|
|
return tokens;
|
|
|
|
std::vector<Parser::Token> unusedTokens;
|
|
|
|
for( std::size_t i = 0; i < tokens.size(); ++i ) {
|
|
|
|
Parser::Token const& token = tokens[i];
|
|
|
|
if( token.type == Parser::Token::Positional )
|
2014-03-17 19:40:58 +01:00
|
|
|
m_floatingArg->boundField.set( config, token.data );
|
2013-05-27 11:52:58 +02:00
|
|
|
else
|
|
|
|
unusedTokens.push_back( token );
|
|
|
|
}
|
|
|
|
return unusedTokens;
|
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2014-03-24 19:15:04 +01:00
|
|
|
void validate() const
|
|
|
|
{
|
|
|
|
if( m_options.empty() && m_positionalArgs.empty() && !m_floatingArg.get() )
|
|
|
|
throw std::logic_error( "No options or arguments specified" );
|
|
|
|
|
|
|
|
for( typename std::vector<Arg>::const_iterator it = m_options.begin(),
|
|
|
|
itEnd = m_options.end();
|
|
|
|
it != itEnd; ++it )
|
|
|
|
it->validate();
|
|
|
|
}
|
|
|
|
|
2013-05-27 11:52:58 +02:00
|
|
|
private:
|
2013-06-04 09:37:28 +02:00
|
|
|
Detail::BoundArgFunction<ConfigT> m_boundProcessName;
|
2013-05-27 11:52:58 +02:00
|
|
|
std::vector<Arg> m_options;
|
|
|
|
std::map<int, Arg> m_positionalArgs;
|
2014-03-17 19:40:58 +01:00
|
|
|
ArgAutoPtr m_floatingArg;
|
2013-05-27 11:52:58 +02:00
|
|
|
int m_highestSpecifiedArgPosition;
|
2013-12-20 20:06:02 +01:00
|
|
|
bool m_throwOnUnrecognisedTokens;
|
2013-05-27 11:52:58 +02:00
|
|
|
};
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2013-05-27 11:52:58 +02:00
|
|
|
} // end namespace Clara
|
|
|
|
|
2014-03-06 09:16:06 +01:00
|
|
|
|
|
|
|
STITCH_CLARA_CLOSE_NAMESPACE
|
|
|
|
#undef STITCH_CLARA_OPEN_NAMESPACE
|
|
|
|
#undef STITCH_CLARA_CLOSE_NAMESPACE
|
|
|
|
|
2014-02-10 18:20:30 +01:00
|
|
|
|
2014-03-01 11:38:14 +01:00
|
|
|
#endif // TWOBLUECUBES_CLARA_H_INCLUDED
|