raise ExitException instead of calling exit

This commit is contained in:
macbishop 2007-02-17 14:57:53 +00:00
parent ce79a6fa1c
commit 0953ea9976
4 changed files with 99 additions and 91 deletions

View File

@ -1,3 +1,4 @@
// -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
/****************************************************************************** /******************************************************************************
* *
@ -189,10 +190,10 @@ inline void DocBookOutput::usage(CmdLineInterface& _cmd )
} }
inline void DocBookOutput::failure( CmdLineInterface& _cmd, inline void DocBookOutput::failure( CmdLineInterface& _cmd,
ArgException& e ) ArgException& e )
{ {
std::cout << e.what() << std::endl; std::cout << e.what() << std::endl;
exit(1); throw ExitException(1);
} }
inline void DocBookOutput::substituteSpecialChars( std::string& s, inline void DocBookOutput::substituteSpecialChars( std::string& s,

View File

@ -60,7 +60,7 @@ class HelpVisitor: public Visitor
* Calls the usage method of the CmdLineOutput for the * Calls the usage method of the CmdLineOutput for the
* specified CmdLine. * specified CmdLine.
*/ */
void visit() { (*_out)->usage(*_cmd); exit(0); } void visit() { (*_out)->usage(*_cmd); throw ExitException(0); }
}; };

View File

@ -1,3 +1,4 @@
// -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
/****************************************************************************** /******************************************************************************
* *
@ -65,7 +66,7 @@ class StdOutput : public CmdLineOutput
* \param e - The ArgException that caused the failure. * \param e - The ArgException that caused the failure.
*/ */
virtual void failure(CmdLineInterface& c, virtual void failure(CmdLineInterface& c,
ArgException& e ); ArgException& e );
protected: protected:
@ -128,7 +129,7 @@ inline void StdOutput::usage(CmdLineInterface& _cmd )
} }
inline void StdOutput::failure( CmdLineInterface& _cmd, inline void StdOutput::failure( CmdLineInterface& _cmd,
ArgException& e ) ArgException& e )
{ {
std::string progName = _cmd.getProgramName(); std::string progName = _cmd.getProgramName();
@ -136,23 +137,24 @@ inline void StdOutput::failure( CmdLineInterface& _cmd,
<< " " << e.error() << std::endl << std::endl; << " " << e.error() << std::endl << std::endl;
if ( _cmd.hasHelpAndVersion() ) if ( _cmd.hasHelpAndVersion() )
{ {
std::cerr << "Brief USAGE: " << std::endl; std::cerr << "Brief USAGE: " << std::endl;
_shortUsage( _cmd, std::cerr ); _shortUsage( _cmd, std::cerr );
std::cerr << std::endl << "For complete USAGE and HELP type: " std::cerr << std::endl << "For complete USAGE and HELP type: "
<< std::endl << " " << progName << " --help" << std::endl << " " << progName << " --help"
<< std::endl << std::endl; << std::endl << std::endl;
} }
else else
usage(_cmd); usage(_cmd);
exit(1); throw ExitException(1);
} }
inline void StdOutput::_shortUsage( CmdLineInterface& _cmd, inline void
std::ostream& os ) const StdOutput::_shortUsage( CmdLineInterface& _cmd,
std::ostream& os ) const
{ {
std::list<Arg*> argList = _cmd.getArgList(); std::list<Arg*> argList = _cmd.getArgList();
std::string progName = _cmd.getProgramName(); std::string progName = _cmd.getProgramName();
@ -163,14 +165,14 @@ inline void StdOutput::_shortUsage( CmdLineInterface& _cmd,
// first the xor // first the xor
for ( int i = 0; static_cast<unsigned int>(i) < xorList.size(); i++ ) for ( int i = 0; static_cast<unsigned int>(i) < xorList.size(); i++ )
{ {
s += " {"; s += " {";
for ( ArgVectorIterator it = xorList[i].begin(); for ( ArgVectorIterator it = xorList[i].begin();
it != xorList[i].end(); it++ ) it != xorList[i].end(); it++ )
s += (*it)->shortID() + "|"; s += (*it)->shortID() + "|";
s[s.length()-1] = '}'; s[s.length()-1] = '}';
} }
// then the rest // then the rest
for (ArgListIterator it = argList.begin(); it != argList.end(); it++) for (ArgListIterator it = argList.begin(); it != argList.end(); it++)
@ -180,13 +182,14 @@ inline void StdOutput::_shortUsage( CmdLineInterface& _cmd,
// if the program name is too long, then adjust the second line offset // if the program name is too long, then adjust the second line offset
int secondLineOffset = static_cast<int>(progName.length()) + 2; int secondLineOffset = static_cast<int>(progName.length()) + 2;
if ( secondLineOffset > 75/2 ) if ( secondLineOffset > 75/2 )
secondLineOffset = static_cast<int>(75/2); secondLineOffset = static_cast<int>(75/2);
spacePrint( std::cout, s, 75, 3, secondLineOffset ); spacePrint( os, s, 75, 3, secondLineOffset );
} }
inline void StdOutput::_longUsage( CmdLineInterface& _cmd, inline void
std::ostream& os ) const StdOutput::_longUsage( CmdLineInterface& _cmd,
std::ostream& os ) const
{ {
std::list<Arg*> argList = _cmd.getArgList(); std::list<Arg*> argList = _cmd.getArgList();
std::string message = _cmd.getMessage(); std::string message = _cmd.getMessage();
@ -195,28 +198,28 @@ inline void StdOutput::_longUsage( CmdLineInterface& _cmd,
// first the xor // first the xor
for ( int i = 0; static_cast<unsigned int>(i) < xorList.size(); i++ ) for ( int i = 0; static_cast<unsigned int>(i) < xorList.size(); i++ )
{
for ( ArgVectorIterator it = xorList[i].begin();
it != xorList[i].end();
it++ )
{ {
spacePrint( os, (*it)->longID(), 75, 3, 3 ); for ( ArgVectorIterator it = xorList[i].begin();
spacePrint( os, (*it)->getDescription(), 75, 5, 0 ); it != xorList[i].end();
it++ )
{
spacePrint( os, (*it)->longID(), 75, 3, 3 );
spacePrint( os, (*it)->getDescription(), 75, 5, 0 );
if ( it+1 != xorList[i].end() ) if ( it+1 != xorList[i].end() )
spacePrint(os, "-- OR --", 75, 9, 0); spacePrint(os, "-- OR --", 75, 9, 0);
}
os << std::endl << std::endl;
} }
os << std::endl << std::endl;
}
// then the rest // then the rest
for (ArgListIterator it = argList.begin(); it != argList.end(); it++) for (ArgListIterator it = argList.begin(); it != argList.end(); it++)
if ( !xorHandler.contains( (*it) ) ) if ( !xorHandler.contains( (*it) ) )
{ {
spacePrint( os, (*it)->longID(), 75, 3, 3 ); spacePrint( os, (*it)->longID(), 75, 3, 3 );
spacePrint( os, (*it)->getDescription(), 75, 5, 0 ); spacePrint( os, (*it)->getDescription(), 75, 5, 0 );
os << std::endl; os << std::endl;
} }
os << std::endl; os << std::endl;
@ -232,63 +235,63 @@ inline void StdOutput::spacePrint( std::ostream& os,
int len = static_cast<int>(s.length()); int len = static_cast<int>(s.length());
if ( (len + indentSpaces > maxWidth) && maxWidth > 0 ) if ( (len + indentSpaces > maxWidth) && maxWidth > 0 )
{
int allowedLen = maxWidth - indentSpaces;
int start = 0;
while ( start < len )
{ {
// find the substring length int allowedLen = maxWidth - indentSpaces;
// int stringLen = std::min<int>( len - start, allowedLen ); int start = 0;
// doing it this way to support a VisualC++ 2005 bug while ( start < len )
using namespace std; {
int stringLen = min<int>( len - start, allowedLen ); // find the substring length
// int stringLen = std::min<int>( len - start, allowedLen );
// doing it this way to support a VisualC++ 2005 bug
using namespace std;
int stringLen = min<int>( len - start, allowedLen );
// trim the length so it doesn't end in middle of a word // trim the length so it doesn't end in middle of a word
if ( stringLen == allowedLen ) if ( stringLen == allowedLen )
while ( s[stringLen+start] != ' ' && while ( s[stringLen+start] != ' ' &&
s[stringLen+start] != ',' && s[stringLen+start] != ',' &&
s[stringLen+start] != '|' && s[stringLen+start] != '|' &&
stringLen >= 0 ) stringLen >= 0 )
stringLen--; stringLen--;
// ok, the word is longer than the line, so just split // ok, the word is longer than the line, so just split
// wherever the line ends // wherever the line ends
if ( stringLen <= 0 ) if ( stringLen <= 0 )
stringLen = allowedLen; stringLen = allowedLen;
// check for newlines // check for newlines
for ( int i = 0; i < stringLen; i++ ) for ( int i = 0; i < stringLen; i++ )
if ( s[start+i] == '\n' ) if ( s[start+i] == '\n' )
stringLen = i+1; stringLen = i+1;
// print the indent // print the indent
for ( int i = 0; i < indentSpaces; i++ )
os << " ";
if ( start == 0 )
{
// handle second line offsets
indentSpaces += secondLineOffset;
// adjust allowed len
allowedLen -= secondLineOffset;
}
os << s.substr(start,stringLen) << std::endl;
// so we don't start a line with a space
while ( s[stringLen+start] == ' ' && start < len )
start++;
start += stringLen;
}
}
else
{
for ( int i = 0; i < indentSpaces; i++ ) for ( int i = 0; i < indentSpaces; i++ )
os << " "; os << " ";
os << s << std::endl;
if ( start == 0 )
{
// handle second line offsets
indentSpaces += secondLineOffset;
// adjust allowed len
allowedLen -= secondLineOffset;
}
os << s.substr(start,stringLen) << std::endl;
// so we don't start a line with a space
while ( s[stringLen+start] == ' ' && start < len )
start++;
start += stringLen;
} }
}
else
{
for ( int i = 0; i < indentSpaces; i++ )
os << " ";
os << s << std::endl;
}
} }
} //namespace TCLAP } //namespace TCLAP

View File

@ -1,3 +1,4 @@
// -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
/****************************************************************************** /******************************************************************************
* *
@ -61,7 +62,10 @@ class VersionVisitor: public Visitor
* Calls the version method of the output object using the * Calls the version method of the output object using the
* specified CmdLine. * specified CmdLine.
*/ */
void visit() { (*_out)->version(*_cmd); exit(0); } void visit() {
(*_out)->version(*_cmd);
throw ExitException(0);
}
}; };