Clara: Usage strings for args

This commit is contained in:
Phil Nash 2013-05-21 19:21:24 +01:00
parent ed79d726be
commit b7632659f5

View File

@ -315,8 +315,11 @@ namespace Clara {
~ArgBinder() { ~ArgBinder() {
if( m_cl ) { if( m_cl ) {
m_arg.validate(); m_arg.validate();
if( m_arg.isFixedPositional() ) if( m_arg.isFixedPositional() ) {
m_cl->m_positionalArgs.push_back( m_arg ); m_cl->m_positionalArgs.insert( std::make_pair( m_arg.position, m_arg ) );
if( m_arg.position > m_cl->m_highestSpecifiedArgPosition )
m_cl->m_highestSpecifiedArgPosition = m_arg.position;
}
else if( m_arg.isAnyPositional() ) { else if( m_arg.isAnyPositional() ) {
if( m_cl->m_arg.get() ) if( m_cl->m_arg.get() )
throw std::logic_error( "Only one unpositional argument can be added" ); throw std::logic_error( "Only one unpositional argument can be added" );
@ -352,42 +355,88 @@ namespace Clara {
}; };
public: public:
CommandLine() : m_highestSpecifiedArgPosition( 0 ) {}
template<typename F> template<typename F>
ArgBinder bind( F f ) { ArgBinder bind( F f ) {
ArgBinder binder( this, f ); ArgBinder binder( this, f );
return binder; return binder;
} }
void usage( std::ostream& os ) const { void optUsage( std::ostream& os, std::size_t indent = 0, std::size_t width = CATCH_CONFIG_CONSOLE_WIDTH ) const {
typename std::vector<Arg>::const_iterator itBegin = m_options.begin(), itEnd = m_options.end(), it; typename std::vector<Arg>::const_iterator itBegin = m_options.begin(), itEnd = m_options.end(), it;
std::size_t maxWidth = 0; std::size_t maxWidth = 0;
for( it = itBegin; it != itEnd; ++it ) for( it = itBegin; it != itEnd; ++it )
maxWidth = (std::max)( maxWidth, it->commands().size() ); maxWidth = (std::max)( maxWidth, it->commands().size() );
for( it = itBegin; it != itEnd; ++it ) { for( it = itBegin; it != itEnd; ++it ) {
Catch::Text usage( it->commands(), Catch::TextAttributes().setWidth( maxWidth ) ); Catch::Text usage( it->commands(), Catch::TextAttributes()
.setWidth( maxWidth+indent )
.setIndent( indent ) );
// !TBD handle longer usage strings // !TBD handle longer usage strings
Catch::Text desc( it->description, Catch::TextAttributes().setWidth( CATCH_CONFIG_CONSOLE_WIDTH - maxWidth -3 ) ); Catch::Text desc( it->description, Catch::TextAttributes()
.setWidth( width - maxWidth -3 ) );
for( std::size_t i = 0; i < std::max( usage.size(), desc.size() ); ++i ) { for( std::size_t i = 0; i < std::max( usage.size(), desc.size() ); ++i ) {
std::string usageCol = i < usage.size() ? usage[i] : ""; std::string usageCol = i < usage.size() ? usage[i] : "";
os << usageCol; os << usageCol;
if( i < desc.size() && !desc[i].empty() ) if( i < desc.size() && !desc[i].empty() )
os << std::string( 2 + maxWidth - usageCol.size(), ' ' ) os << std::string( indent + 2 + maxWidth - usageCol.size(), ' ' )
<< desc[i]; << desc[i];
os << "\n"; os << "\n";
} }
} }
} }
std::string usage() const { std::string optUsage() const {
std::ostringstream oss; std::ostringstream oss;
usage( oss ); optUsage( oss );
return oss.str(); return oss.str();
} }
friend std::ostream& operator << ( std::ostream& os, CommandLine const& parser ) {
parser.usage( os ); void argSynopsis( std::ostream& os ) const {
return os; 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() )
os << "<" << it->second.argName << ">";
else if( m_arg.get() )
os << "<" << m_arg->argName << ">";
else
throw std::logic_error( "non consecutive positional arguments with no floating args" );
}
// !TBD No indication of mandatory args
if( m_arg.get() ) {
if( m_highestSpecifiedArgPosition > 1 )
os << " ";
os << "[<" << m_arg->argName << "> ...]";
}
}
std::string argSynopsis() const {
std::ostringstream oss;
argSynopsis( oss );
return oss.str();
}
void usage( std::ostream& os, std::string const& procName ) const {
os << "usage:\n";
os << " " << procName << " ";
argSynopsis( os );
if( !m_options.empty() ) {
os << " [options]\n\n";
os << "where options are: \n";
optUsage( os, 2 );
}
os << "\n";
}
std::string usage( std::string const& procName ) const {
std::ostringstream oss;
usage( oss, procName );
return oss.str();
} }
std::vector<Parser::Token> parseInto( int argc, char const* argv[], ConfigT& config ) const { std::vector<Parser::Token> parseInto( int argc, char const* argv[], ConfigT& config ) const {
@ -438,18 +487,13 @@ namespace Clara {
int position = 1; int position = 1;
for( std::size_t i = 0; i < tokens.size(); ++i ) { for( std::size_t i = 0; i < tokens.size(); ++i ) {
Parser::Token const& token = tokens[i]; Parser::Token const& token = tokens[i];
typename std::vector<Arg>::const_iterator it = m_positionalArgs.begin(), itEnd = m_positionalArgs.end(); typename std::map<int, Arg>::const_iterator it = m_positionalArgs.find( position );
for(; it != itEnd; ++it ) { if( it != m_positionalArgs.end() )
Arg const& arg = *it; it->second.boundField.set( config, token.data );
if( token.type == Parser::Token::Positional ) else
if( arg.position == position ) {
position++;
arg.boundField.set( config, token.data );
break;
}
}
if( it == itEnd )
unusedTokens.push_back( token ); unusedTokens.push_back( token );
if( token.type == Parser::Token::Positional )
position++;
} }
return unusedTokens; return unusedTokens;
} }
@ -469,8 +513,9 @@ namespace Clara {
private: private:
std::vector<Arg> m_options; std::vector<Arg> m_options;
std::vector<Arg> m_positionalArgs; std::map<int, Arg> m_positionalArgs;
std::auto_ptr<Arg> m_arg; std::auto_ptr<Arg> m_arg;
int m_highestSpecifiedArgPosition;
}; };
} // end namespace Clara } // end namespace Clara
@ -514,7 +559,7 @@ TEST_CASE( "cmdline" ) {
.describe( "specifies output file" ) .describe( "specifies output file" )
.shortOpt( "o" ) .shortOpt( "o" )
.longOpt( "output" ) .longOpt( "output" )
.argName( "<filename>" ); .argName( "filename" );
SECTION( "plain filename" ) { SECTION( "plain filename" ) {
const char* argv[] = { "test", "-o filename.ext" }; const char* argv[] = { "test", "-o filename.ext" };
@ -543,7 +588,7 @@ TEST_CASE( "cmdline" ) {
cli.bind( &TestOpt::number ) cli.bind( &TestOpt::number )
.shortOpt( "n" ) .shortOpt( "n" )
.argName( "<an integral value>" ); .argName( "an integral value" );
SECTION( "a number" ) { SECTION( "a number" ) {
const char* argv[] = { "test", "-n 42" }; const char* argv[] = { "test", "-n 42" };
@ -568,7 +613,7 @@ TEST_CASE( "cmdline" ) {
.describe( "description" ) .describe( "description" )
.shortOpt( "d" ) .shortOpt( "d" )
.longOpt( "description" ) .longOpt( "description" )
.argName( "<some text>" ); .argName( "some text" );
const char* argv[] = { "test", "-n 42", "-d some text" }; const char* argv[] = { "test", "-n 42", "-d some text" };
std::vector<Clara::Parser::Token> unusedTokens = parseInto( cli, argv, config1 ); std::vector<Clara::Parser::Token> unusedTokens = parseInto( cli, argv, config1 );
@ -584,7 +629,7 @@ TEST_CASE( "cmdline" ) {
cli.bind( &TestOpt::setValidIndex ) cli.bind( &TestOpt::setValidIndex )
.describe( "An index, which is an integer between 0 and 10, inclusive" ) .describe( "An index, which is an integer between 0 and 10, inclusive" )
.shortOpt( "i" ) .shortOpt( "i" )
.argName( "<index>" ); .argName( "index" );
SECTION( "in range" ) { SECTION( "in range" ) {
const char* argv[] = { "test", "-i 3" }; const char* argv[] = { "test", "-i 3" };
@ -630,6 +675,8 @@ TEST_CASE( "cmdline" ) {
.argName( "first arg" ) .argName( "first arg" )
.position( 1 ); .position( 1 );
std::cout << cli.usage( "testApp" ) << std::endl;
const char* argv[] = { "test", "-f", "1st", "-o", "filename", "2nd", "3rd" }; const char* argv[] = { "test", "-f", "1st", "-o", "filename", "2nd", "3rd" };
parseInto( cli, argv, config ); parseInto( cli, argv, config );
@ -747,9 +794,9 @@ SCENARIO( "New Catch commandline interface", "[cli]" ) {
.describe( "which test or tests to use" ) .describe( "which test or tests to use" )
.argName( "test name, pattern or tags" ); .argName( "test name, pattern or tags" );
WHEN( "It is streamed" ) WHEN( "We ask for usage strings" )
THEN( "It prints the usage strings" ) THEN( "It prints the usage strings" )
std::cout << cli << std::endl; std::cout << cli.usage( "CatchTestApp" ) << std::endl;
Config config; Config config;
@ -798,6 +845,7 @@ SCENARIO( "New Catch commandline interface", "[cli]" ) {
} }
} }
// !TBD: print positional args in usage
} }
// !TBD still support this? // !TBD still support this?