updated for new test1

This commit is contained in:
mes5k 2004-10-22 01:13:16 +00:00
parent b1cb010ec6
commit 403c23972a

View File

@ -104,91 +104,91 @@ calls to the <i>getValue()</i> methods of the argument objects.<br />
Here is a simple <a href="test1.cpp">example</a> ... Here is a simple <a href="test1.cpp">example</a> ...
<br/> <br/>
<pre> <pre>
#include &lt; string &gt; #include &lt;string&gt;
#include &lt; iostream &gt; #include &lt;iostream&gt;
#include &lt; tclap/CmdLine.h &gt; #include &lt;algorithm&gt;
#include &lt;tclap/CmdLine.h&gt;
using namespace TCLAP;
using namespace std; using namespace std;
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
// Wrap everything in a try block. Do this every time, // Wrap everything in a try block. Do this every time,
// because exceptions will be thrown for problems. // because exceptions will be thrown for problems.
try { try {
// Define the command line object. // Define the command line object.
CmdLine cmd("Command description message", ' ', "0.9"); CmdLine cmd("Command description message", ' ', "0.9");
// (deprecated, but still functional) // Define a value argument and add it to the command line.
// CmdLine cmd(argv[0], "Command description message", "0.9"); ValueArg&lt;string&gt; nameArg("n","name","Name to print",true,"homer","string");
cmd.add( nameArg );
// Define a value argument and add it to the command line. // Define a switch and add it to the command line.
ValueArg &lt; string &gt; nameArg("n","name","Name to print",true,"homer", SwitchArg reverseSwitch("r","reverse","Print name backwards", false);
"nameString"); cmd.add( reverseSwitch );
cmd.add( nameArg );
// Define a switch and add it to the command line. // Parse the args.
SwitchArg caseSwitch("u","upperCase","Print in upper case", false); cmd.parse( argc, argv );
cmd.add( caseSwitch );
// Parse the args. // Get the value parsed by each arg.
cmd.parse( argc, argv ); string name = nameArg.getValue();
bool reverseName = reverseSwitch.getValue();
// Get the value parsed by each arg. // Do what you intend too...
string name = nameArg.getValue(); if ( reverseName )
bool upperCase = caseSwitch.getValue(); {
reverse(name.begin(),name.end());
cout &lt;&lt; "My name (spelled backwards) is: " &lt;&lt; name &lt;&lt; endl;
}
else
cout &lt;&lt; "My name is: " &lt;&lt; name &lt;&lt; endl;
// Do what you intend to...
if ( upperCase )
transform(name.begin(),name.end(),name.begin(),::toupper);
else
transform(name.begin(),name.end(),name.begin(),::tolower);
cout &lt;&lt; "My name is " &lt;&lt; name &lt;&lt; endl; } catch (ArgException &e) // catch any exceptions
{ cerr &lt;&lt; "error: " &lt;&lt; e.error() &lt;&lt; " for arg " &lt;&lt; e.argId() &lt;&lt; endl; }
} catch (ArgException e) // catch any exceptions
{ cerr &lt;&lt; "error: " &lt;&lt; e.error() &lt;&lt; " for arg " &lt;&lt; e.argId() &lt;&lt; endl; }
} }
</pre> </pre>
<br /> <br />
The output should look like:<br /> The output should look like:<br />
<br /> <br />
<pre> <pre>
% tester -u -n mike % test1 -n mike
My name is MIKE My name is: mike
% tester -n mike -u % test1 -n mike -r
My name is MIKE My name (spelled backwards) is: ekim
% tester -n mike % test1 -r -n mike
My name is mike My name (spelled backwards) is: ekim
% tester -n MIKE % test1 -r
My name is mike
% tester
PARSE ERROR: PARSE ERROR:
One or more required arguments missing! One or more required arguments missing!
Brief USAGE: Brief USAGE:
tester [-u] -n &lt; string &gt; [--] [-v] [-h] test1 [-r] -n &lt;string&gt; [--] [-v] [-h]
For complete USAGE and HELP type: For complete USAGE and HELP type:
tester --help test1 --help
% tester --help
% test1 --help
USAGE: USAGE:
tester [-u] -n &lt; string &gt; [--] [-v] [-h] test1 [-r] -n &lt;string&gt; [--] [-v] [-h]
Where: Where:
-u, --upperCase -r, --reverse
Print in upper case Print name backwards
-n &lt; string &gt;, --name &lt; string &gt; -n &lt;string&gt; --name &lt;string&gt;
(required) (value required) Name to print (required) (value required) Name to print
--, --ignore_rest --, --ignore_rest