mirror of
https://github.com/cuberite/TCLAP.git
synced 2025-09-11 05:04:41 -04:00
updated for new test1
This commit is contained in:
parent
b1cb010ec6
commit
403c23972a
@ -104,9 +104,12 @@ 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 < string >
|
#include <string>
|
||||||
#include < iostream >
|
#include <iostream>
|
||||||
#include < tclap/CmdLine.h >
|
#include <algorithm>
|
||||||
|
#include <tclap/CmdLine.h>
|
||||||
|
|
||||||
|
using namespace TCLAP;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
@ -118,77 +121,74 @@ int main(int argc, char** argv)
|
|||||||
// 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)
|
|
||||||
// CmdLine cmd(argv[0], "Command description message", "0.9");
|
|
||||||
|
|
||||||
// Define a value argument and add it to the command line.
|
// Define a value argument and add it to the command line.
|
||||||
ValueArg < string > nameArg("n","name","Name to print",true,"homer",
|
ValueArg<string> nameArg("n","name","Name to print",true,"homer","string");
|
||||||
"nameString");
|
|
||||||
cmd.add( nameArg );
|
cmd.add( nameArg );
|
||||||
|
|
||||||
// Define a switch and add it to the command line.
|
// Define a switch and add it to the command line.
|
||||||
SwitchArg caseSwitch("u","upperCase","Print in upper case", false);
|
SwitchArg reverseSwitch("r","reverse","Print name backwards", false);
|
||||||
cmd.add( caseSwitch );
|
cmd.add( reverseSwitch );
|
||||||
|
|
||||||
// Parse the args.
|
// Parse the args.
|
||||||
cmd.parse( argc, argv );
|
cmd.parse( argc, argv );
|
||||||
|
|
||||||
// Get the value parsed by each arg.
|
// Get the value parsed by each arg.
|
||||||
string name = nameArg.getValue();
|
string name = nameArg.getValue();
|
||||||
bool upperCase = caseSwitch.getValue();
|
bool reverseName = reverseSwitch.getValue();
|
||||||
|
|
||||||
// Do what you intend to...
|
// Do what you intend too...
|
||||||
if ( upperCase )
|
if ( reverseName )
|
||||||
transform(name.begin(),name.end(),name.begin(),::toupper);
|
{
|
||||||
|
reverse(name.begin(),name.end());
|
||||||
|
cout << "My name (spelled backwards) is: " << name << endl;
|
||||||
|
}
|
||||||
else
|
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; }
|
{ cerr << "error: " << e.error() << " for arg " << e.argId() << 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 < string > [--] [-v] [-h]
|
test1 [-r] -n <string> [--] [-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 < string > [--] [-v] [-h]
|
test1 [-r] -n <string> [--] [-v] [-h]
|
||||||
|
|
||||||
|
|
||||||
Where:
|
Where:
|
||||||
|
|
||||||
-u, --upperCase
|
-r, --reverse
|
||||||
Print in upper case
|
Print name backwards
|
||||||
|
|
||||||
-n < string >, --name < string >
|
-n <string> --name <string>
|
||||||
(required) (value required) Name to print
|
(required) (value required) Name to print
|
||||||
|
|
||||||
--, --ignore_rest
|
--, --ignore_rest
|
||||||
|
Loading…
x
Reference in New Issue
Block a user