changed test1 and now catching exceptions by ref

This commit is contained in:
mes5k 2004-10-22 01:01:47 +00:00
parent 74353f79f4
commit 3e2753528f
8 changed files with 17 additions and 16 deletions

View File

@ -1,5 +1,4 @@
#include <string>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <tclap/CmdLine.h>
@ -21,25 +20,27 @@ int main(int argc, char** argv)
cmd.add( nameArg );
// Define a switch and add it to the command line.
SwitchArg caseSwitch("u","upperCase","Print in upper case", false);
cmd.add( caseSwitch );
SwitchArg reverseSwitch("r","reverse","Print name backwards", false);
cmd.add( reverseSwitch );
// Parse the args.
cmd.parse( argc, argv );
// Get the value parsed by each arg.
string name = nameArg.getValue();
bool upperCase = caseSwitch.getValue();
bool reverseName = reverseSwitch.getValue();
// Do what you intend too...
if ( upperCase )
transform(name.begin(),name.end(),name.begin(),::toupper);
if ( reverseName )
{
reverse(name.begin(),name.end());
cout << "My name (spelled backwards) is: " << name << endl;
}
else
transform(name.begin(),name.end(),name.begin(),::tolower);
cout << "My name is: " << name << endl;
cout << "My name is " << name << endl;
} catch (ArgException e) // catch any exceptions
} catch (ArgException &e) // catch any exceptions
{ cerr << "error: " << e.error() << " for arg " << e.argId() << endl; }
}

View File

@ -89,7 +89,7 @@ void parseOptions(int argc, char** argv)
for ( int i = 0; (unsigned int)i < v.size(); i++ )
cout << i << " " << v[i] << endl;
} catch ( ArgException e )
} catch ( ArgException& e )
{ cout << "ERROR: " << e.error() << " " << e.argId() << endl; }
}

View File

@ -86,7 +86,7 @@ void parseOptions(int argc, char** argv)
for ( int i = 0; (unsigned int)i < v.size(); i++ )
cout << "[ ] " << i << " " << v[i] << endl;
} catch ( ArgException e )
} catch ( ArgException& e )
{ cout << "ERROR: " << e.error() << " " << e.argId() << endl; }
}

View File

@ -84,7 +84,7 @@ void parseOptions(int argc, char** argv)
_boolTestA = atest.getValue();
} catch ( ArgException e )
} catch ( ArgException& e )
{ cout << "ERROR: " << e.error() << " " << e.argId() << endl; }
}

View File

@ -88,7 +88,7 @@ void parseOptions(int argc, char** argv)
throw("still bad");
} catch ( ArgException e )
} catch ( ArgException& e )
{ cout << "ERROR: " << e.error() << " " << e.argId() << endl; }
}

View File

@ -42,7 +42,7 @@ int main(int argc, char** argv)
for ( int i = 0; i < num; i++ )
cout << "My name is " << name << endl;
} catch (ArgException e) // catch any exceptions
} catch ( ArgException& e) // catch any exceptions
{ cerr << "error: " << e.error() << " for arg " << e.argId() << endl; }
}

View File

@ -83,7 +83,7 @@ int main(int argc, char** argv)
cout << "Got name " << name[i] << endl;
} catch (ArgException e) // catch any exceptions
} catch (ArgException& e) // catch any exceptions
{ cerr << "error: " << e.error() << " for arg " << e.argId() << endl; }
}

View File

@ -81,7 +81,7 @@ void parseOptions(int argc, char** argv)
for ( int i = 0; (unsigned int)i < v.size(); i++ )
cout << "[ ] " << i << " " << v[i] << endl;
} catch ( ArgException e )
} catch ( ArgException& e )
{ cout << "ERROR: " << e.error() << " " << e.argId() << endl; }
}