ENH: Condense help string, add support for setting line length and make it work

This commit is contained in:
Andy Cedilnik 2004-09-16 10:27:17 -04:00
parent 444dbd3309
commit a2c19a4d46
2 changed files with 31 additions and 10 deletions

View File

@ -464,6 +464,17 @@ const char* CommandLineArguments::GetHelp(const char* arg)
return 0; return 0;
} }
//----------------------------------------------------------------------------
void CommandLineArguments::SetLineLength(unsigned int ll)
{
if ( ll < 9 || ll > 1000 )
{
return;
}
this->LineLength = ll;
this->GenerateHelp();
}
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
void CommandLineArguments::GenerateHelp() void CommandLineArguments::GenerateHelp()
{ {
@ -528,9 +539,9 @@ void CommandLineArguments::GenerateHelp()
switch ( this->Internals->Callbacks[*sit].ArgumentType ) switch ( this->Internals->Callbacks[*sit].ArgumentType )
{ {
case CommandLineArguments::NO_ARGUMENT: clen += 0; break; case CommandLineArguments::NO_ARGUMENT: clen += 0; break;
case CommandLineArguments::CONCAT_ARGUMENT: clen += 6; break; case CommandLineArguments::CONCAT_ARGUMENT: clen += 3; break;
case CommandLineArguments::SPACE_ARGUMENT: clen += 7; break; case CommandLineArguments::SPACE_ARGUMENT: clen += 4; break;
case CommandLineArguments::EQUAL_ARGUMENT: clen += 7; break; case CommandLineArguments::EQUAL_ARGUMENT: clen += 4; break;
} }
if ( clen > maxlen ) if ( clen > maxlen )
{ {
@ -539,9 +550,11 @@ void CommandLineArguments::GenerateHelp()
} }
} }
maxlen += 2; // For the space after option
// Create format for that string // Create format for that string
char format[80]; char format[80];
sprintf(format, "%%%ds", static_cast<unsigned int>(maxlen)); sprintf(format, "%%%ds ", static_cast<unsigned int>(maxlen-2));
// Print help for each option // Print help for each option
@ -558,15 +571,14 @@ void CommandLineArguments::GenerateHelp()
switch ( this->Internals->Callbacks[*sit].ArgumentType ) switch ( this->Internals->Callbacks[*sit].ArgumentType )
{ {
case CommandLineArguments::NO_ARGUMENT: break; case CommandLineArguments::NO_ARGUMENT: break;
case CommandLineArguments::CONCAT_ARGUMENT: strcat(argument, "option"); break; case CommandLineArguments::CONCAT_ARGUMENT: strcat(argument, "opt"); break;
case CommandLineArguments::SPACE_ARGUMENT: strcat(argument, " option"); break; case CommandLineArguments::SPACE_ARGUMENT: strcat(argument, " opt"); break;
case CommandLineArguments::EQUAL_ARGUMENT: strcat(argument, "=option"); break; case CommandLineArguments::EQUAL_ARGUMENT: strcat(argument, "=opt"); break;
} }
char buffer[80]; char buffer[80];
sprintf(buffer, format, argument); sprintf(buffer, format, argument);
str << buffer; str << buffer;
} }
str << "\t";
const char* ptr = this->Internals->Callbacks[mpit->first].Help; const char* ptr = this->Internals->Callbacks[mpit->first].Help;
int len = strlen(ptr); int len = strlen(ptr);
int cnt = 0; int cnt = 0;
@ -589,7 +601,6 @@ void CommandLineArguments::GenerateHelp()
{ {
str << " "; str << " ";
} }
str << "\t";
} }
CommandLineArguments::Internal::String::size_type skip = len; CommandLineArguments::Internal::String::size_type skip = len;
if ( skip > this->LineLength - maxlen ) if ( skip > this->LineLength - maxlen )
@ -614,6 +625,16 @@ void CommandLineArguments::GenerateHelp()
cnt ++; cnt ++;
} }
} }
/*
// This can help debugging help string
str << endl;
unsigned int cc;
for ( cc = 0; cc < this->LineLength; cc ++ )
{
str << cc % 10;
}
str << endl;
*/
this->Help = str.str(); this->Help = str.str();
} }

View File

@ -134,7 +134,7 @@ public:
/** /**
* Get / Set the help line length. Default length is 80. * Get / Set the help line length. Default length is 80.
*/ */
void SetLineLength(); void SetLineLength(unsigned int);
unsigned int GetLineLength(); unsigned int GetLineLength();
protected: protected: