fixed typo in SwitchArg constructors

This commit is contained in:
mes5k 2006-11-26 18:13:18 +00:00
parent 54db78bd97
commit a8c7660076
2 changed files with 258 additions and 258 deletions

View File

@ -1,27 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Templatized C++ Command Line Parser Manual</title><link rel="stylesheet" href="style.css" type="text/css" /><meta name="generator" content="DocBook XSL Stylesheets V1.61.3" /></head><body><div class="book" lang="en" xml:lang="en"><div class="titlepage"><div><div><h1 class="title"><a id="id2868300"></a>Templatized C++ Command Line Parser Manual</h1></div><div><div class="author"><h3 class="author"><span class="firstname">Michael</span> <span class="othername">E</span> <span class="surname">Smoot</span></h3></div></div><div><p class="copyright">Copyright © 2003,2004,2005,2006 Michael E. Smoot</p></div></div><div></div><hr /></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt>1. <a href="#id2868084">Basic Usage</a></dt><dd><dl><dt><a href="#id2867730">Overview</a></dt><dt><a href="#id2801617">Example</a></dt><dd><dl><dt><a href="#id2801732">Library Properties</a></dt><dt><a href="#ARG_PROPERTIES">Common Argument Properties</a></dt></dl></dd><dt><a href="#COMPILING">Compiling</a></dt></dl></dd><dt>2. <a href="#FUNDAMENTAL_CLASSES">Fundamental Classes</a></dt><dd><dl><dt><a href="#COMMAND_LINE">CmdLine</a></dt><dt><a href="#id2853573">SwitchArg</a></dt><dt><a href="#id2853610">ValueArg</a></dt><dt><a href="#id2853662">MultiArg</a></dt><dt><a href="#id2853779">MultiSwitchArg</a></dt><dt><a href="#UNLABELED_MULTI_ARG">UnlabeledValueArg</a></dt><dt><a href="#id2853345">UnlabeledMultiArg</a></dt></dl></dd><dt>3. <a href="#COMPLICATIONS">Complications</a></dt><dd><dl><dt><a href="#COMBINE_SWITCHES">I want to combine multiple switches into one argument...</a></dt><dt><a href="#XOR">I want one argument or the other, but not both...</a></dt><dt><a href="#NO_FLAG">I have more arguments than single flags make sense for...</a></dt><dt><a href="#CONSTRAINT">I want to constrain the values allowed for a particular <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Templatized C++ Command Line Parser Manual</title><link rel="stylesheet" href="style.css" type="text/css" /><meta name="generator" content="DocBook XSL Stylesheets V1.71.1" /></head><body><div class="book" lang="en" xml:lang="en"><div class="titlepage"><div><div><h1 class="title"><a id="id838858"></a>Templatized C++ Command Line Parser Manual</h1></div><div><div class="author"><h3 class="author"><span class="firstname">Michael</span> <span class="othername">E</span> <span class="surname">Smoot</span></h3></div></div><div><p class="copyright">Copyright © 2003,2004,2005,2006 Michael E. Smoot</p></div></div><hr /></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="chapter"><a href="#id856549">1. Basic Usage</a></span></dt><dd><dl><dt><span class="sect1"><a href="#id856554">Overview</a></span></dt><dt><span class="sect1"><a href="#id856614">Example</a></span></dt><dd><dl><dt><span class="sect2"><a href="#id856644">Library Properties</a></span></dt><dt><span class="sect2"><a href="#ARG_PROPERTIES">Common Argument Properties</a></span></dt></dl></dd><dt><span class="sect1"><a href="#COMPILING">Compiling</a></span></dt></dl></dd><dt><span class="chapter"><a href="#FUNDAMENTAL_CLASSES">2. Fundamental Classes</a></span></dt><dd><dl><dt><span class="sect1"><a href="#COMMAND_LINE"><code class="classname">CmdLine</code></a></span></dt><dt><span class="sect1"><a href="#id857113"><code class="classname">SwitchArg</code></a></span></dt><dt><span class="sect1"><a href="#id857146"><code class="classname">ValueArg</code></a></span></dt><dt><span class="sect1"><a href="#id857193"><code class="classname">MultiArg</code></a></span></dt><dt><span class="sect1"><a href="#id857294"><code class="classname">MultiSwitchArg</code></a></span></dt><dt><span class="sect1"><a href="#UNLABELED_MULTI_ARG"><code class="classname">UnlabeledValueArg</code></a></span></dt><dt><span class="sect1"><a href="#id857468"><code class="classname">UnlabeledMultiArg</code></a></span></dt></dl></dd><dt><span class="chapter"><a href="#COMPLICATIONS">3. Complications</a></span></dt><dd><dl><dt><span class="sect1"><a href="#COMBINE_SWITCHES">I want to combine multiple switches into one argument...</a></span></dt><dt><span class="sect1"><a href="#XOR">I want one argument or the other, but not both...</a></span></dt><dt><span class="sect1"><a href="#NO_FLAG">I have more arguments than single flags make sense for...</a></span></dt><dt><span class="sect1"><a href="#CONSTRAINT">I want to constrain the values allowed for a particular
argument...</a></dt><dt><a href="#ARG_ADD_CMDLINE">I want the Args to add themselves to the CmdLine...</a></dt><dt><a href="#CHANGE_OUTPUT">I want different output than what is provided...</a></dt><dt><a href="#NO_HELP_VERSION">I don't want the --help and --version switches to be created automatically...</a></dt></dl></dd><dt>4. <a href="#NOTES">Notes</a></dt><dd><dl><dt><a href="#DESCRIPTION_EXCEPTIONS">Type Descriptions</a></dt><dt><a href="#VISITORS">Visitors</a></dt><dt><a href="#MORE_INFO">More Information</a></dt></dl></dd></dl></div><div class="chapter" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title"><a id="id2868084"></a>Chapter 1. Basic Usage</h2></div></div><div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><a href="#id2867730">Overview</a></dt><dt><a href="#id2801617">Example</a></dt><dd><dl><dt><a href="#id2801732">Library Properties</a></dt><dt><a href="#ARG_PROPERTIES">Common Argument Properties</a></dt></dl></dd><dt><a href="#COMPILING">Compiling</a></dt></dl></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="id2867730"></a>Overview</h2></div></div><div></div></div><p> argument...</a></span></dt><dt><span class="sect1"><a href="#ARG_ADD_CMDLINE">I want the Args to add themselves to the CmdLine...</a></span></dt><dt><span class="sect1"><a href="#CHANGE_OUTPUT">I want different output than what is provided...</a></span></dt><dt><span class="sect1"><a href="#NO_HELP_VERSION">I don't want the --help and --version switches to be created automatically...</a></span></dt></dl></dd><dt><span class="chapter"><a href="#NOTES">4. Notes</a></span></dt><dd><dl><dt><span class="sect1"><a href="#DESCRIPTION_EXCEPTIONS">Type Descriptions</a></span></dt><dt><span class="sect1"><a href="#VISITORS">Visitors</a></span></dt><dt><span class="sect1"><a href="#MORE_INFO">More Information</a></span></dt></dl></dd></dl></div><div class="chapter" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title"><a id="id856549"></a>Chapter 1. Basic Usage</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="sect1"><a href="#id856554">Overview</a></span></dt><dt><span class="sect1"><a href="#id856614">Example</a></span></dt><dd><dl><dt><span class="sect2"><a href="#id856644">Library Properties</a></span></dt><dt><span class="sect2"><a href="#ARG_PROPERTIES">Common Argument Properties</a></span></dt></dl></dd><dt><span class="sect1"><a href="#COMPILING">Compiling</a></span></dt></dl></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="id856554"></a>Overview</h2></div></div></div><p>
<span class="emphasis"><em>TCLAP</em></span> has a few key classes to be aware of. <span class="emphasis"><em>TCLAP</em></span> has a few key classes to be aware of.
The first is the The first is the
<tt class="classname">CmdLine</tt> (command line) class. This class parses <code class="classname">CmdLine</code> (command line) class. This class parses
the command line passed to it according to the arguments that it the command line passed to it according to the arguments that it
contains. Arguments are separate objects that are added to the contains. Arguments are separate objects that are added to the
<tt class="classname">CmdLine</tt> object one at a time. The six <code class="classname">CmdLine</code> object one at a time. The six
argument classes are: <tt class="classname">ValueArg</tt>, argument classes are: <code class="classname">ValueArg</code>,
<tt class="classname">UnlabeledValueArg</tt>, <code class="classname">UnlabeledValueArg</code>,
<tt class="classname">SwitchArg</tt>, <tt class="classname">MultiSwitchArg</tt>, <code class="classname">SwitchArg</code>, <code class="classname">MultiSwitchArg</code>,
<tt class="classname">MultiArg</tt> and <code class="classname">MultiArg</code> and
<tt class="classname">UnlabeledMultiArg</tt>. <code class="classname">UnlabeledMultiArg</code>.
These classes are templatized, which means they can be defined to parse These classes are templatized, which means they can be defined to parse
a value of any <a href="#FOOTNOTES"> type**</a>. Once you add the a value of any <a href="#FOOTNOTES"> type**</a>. Once you add the
arguments to the <tt class="classname">CmdLine</tt> object, it parses the arguments to the <code class="classname">CmdLine</code> object, it parses the
command line command line
and assigns the data it finds to the specific argument objects it and assigns the data it finds to the specific argument objects it
contains. Your program accesses the values parsed by contains. Your program accesses the values parsed by
calls to the <tt class="methodname">getValue()</tt> methods of the calls to the <code class="methodname">getValue()</code> methods of the
argument objects. argument objects.
</p></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="id2801617"></a>Example</h2></div></div><div></div></div><p> </p></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="id856614"></a>Example</h2></div></div></div><p>
Here is a simple <a href="test1.cpp" target="_top"> example</a> ... Here is a simple <a href="test1.cpp" target="_top"> example</a> ...
</p><pre class="programlisting"> </p><pre class="programlisting">
@ -38,17 +38,17 @@ int main(int argc, char** argv)
try { try {
// Define the command line object, and insert a message // Define the command line object, and insert a message
// that describes the program. The &quot;Command description message&quot; // that describes the program. The "Command description message"
// is printed last in the help text. The second argument is the // is printed last in the help text. The second argument is the
// delimiter (usually space) and the last one is the version number. // delimiter (usually space) and the last one is the version number.
// The CmdLine object parses the argv array based on the Arg objects // The CmdLine object parses the argv array based on the Arg objects
// that it contains. // that it contains.
TCLAP::CmdLine cmd(&quot;Command description message&quot;, ' ', &quot;0.9&quot;); TCLAP::CmdLine cmd("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.
// A value arg defines a flag and a type of value that it expects, // A value arg defines a flag and a type of value that it expects,
// such as &quot;-n Bishop&quot;. // such as "-n Bishop".
TCLAP::ValueArg&lt;std::string&gt; nameArg(&quot;n&quot;,&quot;name&quot;,&quot;Name to print&quot;,true,&quot;homer&quot;,&quot;string&quot;); TCLAP::ValueArg&lt;std::string&gt; nameArg("n","name","Name to print",true,"homer","string");
// Add the argument nameArg to the CmdLine object. The CmdLine object // Add the argument nameArg to the CmdLine object. The CmdLine object
// uses this Arg to parse the command line. // uses this Arg to parse the command line.
@ -61,7 +61,7 @@ int main(int argc, char** argv)
// the need to call the cmd.add() method. All args have support in // the need to call the cmd.add() method. All args have support in
// their constructors to add themselves directly to the CmdLine object. // their constructors to add themselves directly to the CmdLine object.
// It doesn't matter which idiom you choose, they accomplish the same thing. // It doesn't matter which idiom you choose, they accomplish the same thing.
TCLAP::SwitchArg reverseSwitch(&quot;r&quot;,&quot;reverse&quot;,&quot;Print name backwards&quot;, false, cmd); TCLAP::SwitchArg reverseSwitch("r","reverse","Print name backwards", cmd, false);
// Parse the argv array. // Parse the argv array.
cmd.parse( argc, argv ); cmd.parse( argc, argv );
@ -74,14 +74,14 @@ int main(int argc, char** argv)
if ( reverseName ) if ( reverseName )
{ {
std::reverse(name.begin(),name.end()); std::reverse(name.begin(),name.end());
std::cout &lt;&lt; &quot;My name (spelled backwards) is: &quot; &lt;&lt; name &lt;&lt; std::endl; std::cout &lt;&lt; "My name (spelled backwards) is: " &lt;&lt; name &lt;&lt; std::endl;
} }
else else
std::cout &lt;&lt; &quot;My name is: &quot; &lt;&lt; name &lt;&lt; std::endl; std::cout &lt;&lt; "My name is: " &lt;&lt; name &lt;&lt; std::endl;
} catch (TCLAP::ArgException &amp;e) // catch any exceptions } catch (TCLAP::ArgException &amp;e) // catch any exceptions
{ std::cerr &lt;&lt; &quot;error: &quot; &lt;&lt; e.error() &lt;&lt; &quot; for arg &quot; &lt;&lt; e.argId() &lt;&lt; std::endl; } { std::cerr &lt;&lt; "error: " &lt;&lt; e.error() &lt;&lt; " for arg " &lt;&lt; e.argId() &lt;&lt; std::endl; }
} }
</pre><p> </pre><p>
@ -137,18 +137,18 @@ Where:
Command description message Command description message
</pre><p> </pre><p>
</p><div class="sect2" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="id2801732"></a>Library Properties</h3></div></div><div></div></div><p> </p><div class="sect2" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="id856644"></a>Library Properties</h3></div></div></div><p>
This example shows a number of different properties of the This example shows a number of different properties of the
library... library...
</p><div class="itemizedlist"><ul type="disc"><li>Arguments can appear in any order (...mostly, </p><div class="itemizedlist"><ul type="disc"><li>Arguments can appear in any order (...mostly,
<a href="#COMPLICATIONS" title="Chapter 3. Complications"> more</a> on this later).</li><li>The <i class="parameter"><tt>help</tt></i>, <i class="parameter"><tt>version</tt></i> <a href="#COMPLICATIONS" title="Chapter 3. Complications"> more</a> on this later).</li><li>The <em class="parameter"><code>help</code></em>, <em class="parameter"><code>version</code></em>
and <i class="parameter"><tt>--</tt></i><tt class="classname">SwitchArg</tt>s and <em class="parameter"><code>--</code></em><code class="classname">SwitchArg</code>s
are specified automatically. Using either the <i class="parameter"><tt>-h</tt></i> or are specified automatically. Using either the <em class="parameter"><code>-h</code></em> or
<i class="parameter"><tt>--help</tt></i> flag will cause the USAGE message to be displayed, <em class="parameter"><code>--help</code></em> flag will cause the USAGE message to be displayed,
<i class="parameter"><tt>-v</tt></i> or <i class="parameter"><tt>--version</tt></i> will cause <em class="parameter"><code>-v</code></em> or <em class="parameter"><code>--version</code></em> will cause
any version information to any version information to
be displayed, and <i class="parameter"><tt>--</tt></i> or be displayed, and <em class="parameter"><code>--</code></em> or
<i class="parameter"><tt>--ignore_rest</tt></i> will cause the <em class="parameter"><code>--ignore_rest</code></em> will cause the
remaining labeled arguments to be ignored. These switches are remaining labeled arguments to be ignored. These switches are
included by default on every command line. You can <a href="#">disable this functionality</a> if desired (although we don't recommend it). included by default on every command line. You can <a href="#">disable this functionality</a> if desired (although we don't recommend it).
How we generate the behavior behind these flags is described How we generate the behavior behind these flags is described
@ -157,31 +157,31 @@ How we generate the behavior behind these flags is described
argument isn't provided), the program exits and displays a brief argument isn't provided), the program exits and displays a brief
USAGE and an error message.</li><li>The program name is assumed to always be argv[0], so it isn't USAGE and an error message.</li><li>The program name is assumed to always be argv[0], so it isn't
specified directly.</li><li>A value delimiter character can be specified. This means that if you specified directly.</li><li>A value delimiter character can be specified. This means that if you
prefer arguments of the style <i class="parameter"><tt>-s=asdf</tt></i> instead of prefer arguments of the style <em class="parameter"><code>-s=asdf</code></em> instead of
<i class="parameter"><tt>-s asdf</tt></i>, you can do so.</li><li><span class="emphasis"><em>Always wrap everything in a try block that catches <em class="parameter"><code>-s asdf</code></em>, you can do so.</li><li><span class="emphasis"><em>Always wrap everything in a try block that catches
ArgExceptions!</em></span> Any problems found in constructing the ArgExceptions!</em></span> Any problems found in constructing the
<tt class="classname">CmdLine</tt>, constructing the <tt class="classname">Arg</tt>s, <code class="classname">CmdLine</code>, constructing the <code class="classname">Arg</code>s,
or parsing the command line will throw an or parsing the command line will throw an
<tt class="classname">ArgException</tt>.</li></ul></div><p> <code class="classname">ArgException</code>.</li></ul></div><p>
</p></div><div class="sect2" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="ARG_PROPERTIES"></a>Common Argument Properties</h3></div></div><div></div></div><p> </p></div><div class="sect2" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="ARG_PROPERTIES"></a>Common Argument Properties</h3></div></div></div><p>
Arguments, whatever their type, have a few common properties. Arguments, whatever their type, have a few common properties.
These properties are set in the constructors of the arguments. These properties are set in the constructors of the arguments.
</p><div class="itemizedlist"><ul type="disc"><li>First is the flag or the character preceded by a dash(-) that </p><div class="itemizedlist"><ul type="disc"><li>First is the flag or the character preceded by a dash(-) that
signals the beginning of the argument on the command line.</li><li>Arguments also have names, which can also be used signals the beginning of the argument on the command line.</li><li>Arguments also have names, which can also be used
as an alternative flag on the command line, this time preceded by two dashes as an alternative flag on the command line, this time preceded by two dashes
(--) [like the familiar <tt class="function">getopt_long()</tt>].</li><li>Next is the description of the argument. This is a short (--) [like the familiar <code class="function">getopt_long()</code>].</li><li>Next is the description of the argument. This is a short
description of the argument displayed in the help/usage message description of the argument displayed in the help/usage message
when needed.</li><li>The following parameters in the constructors vary depending on when needed.</li><li>The following parameters in the constructors vary depending on
the type of argument. Some possible values include: the type of argument. Some possible values include:
<div class="itemizedlist"><ul type="circle"><li>A boolean value indicating whether the Arg is required or not. </li><li>A default value.</li><li>A <a href="#DESCRIPTION_EXCEPTIONS" title="Type Descriptions">description</a> of the type of value expected.</li><li>A <a href="#CONSTRAINT" title="I want to constrain the values allowed for a particular &#10;argument...">constraint</a> on the value expected.</li><li>The CmdLine instance that the Arg should be added to.</li><li>A <a href="#VISITORS" title="Visitors">Visitor</a>.</li></ul></div></li><li>See the <a href="html/index.html" target="_top">API Documentation</a> for more detail.</li></ul></div><p> <div class="itemizedlist"><ul type="circle"><li>A boolean value indicating whether the Arg is required or not. </li><li>A default value.</li><li>A <a href="#DESCRIPTION_EXCEPTIONS" title="Type Descriptions">description</a> of the type of value expected.</li><li>A <a href="#CONSTRAINT" title="I want to constrain the values allowed for a particular argument...">constraint</a> on the value expected.</li><li>The CmdLine instance that the Arg should be added to.</li><li>A <a href="#VISITORS" title="Visitors">Visitor</a>.</li></ul></div></li><li>See the <a href="html/index.html" target="_top">API Documentation</a> for more detail.</li></ul></div><p>
</p></div></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="COMPILING"></a>Compiling</h2></div></div><div></div></div><p> </p></div></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="COMPILING"></a>Compiling</h2></div></div></div><p>
<span class="emphasis"><em>TCLAP</em></span> is implemented entirely in header files <span class="emphasis"><em>TCLAP</em></span> is implemented entirely in header files
which means you only need to include CmdLine.h to use the library. which means you only need to include CmdLine.h to use the library.
</p><pre class="programlisting"> </p><pre class="programlisting">
#include &lt;tclap/CmdLine.h&gt; #include &lt;tclap/CmdLine.h&gt;
</pre><p> </pre><p>
You'll need to make sure that your compiler can see the header You'll need to make sure that your compiler can see the header
files. If you do the usual &quot;make install&quot; then your compiler should files. If you do the usual "make install" then your compiler should
see the files by default. Alternatively, you can use the -I see the files by default. Alternatively, you can use the -I
complier argument to specify the exact location of the libraries. complier argument to specify the exact location of the libraries.
</p><pre class="programlisting"> </p><pre class="programlisting">
@ -204,62 +204,62 @@ to work on Windows, Sun and Alpha platforms. We've made every
effort to keep the library compliant with the ANSI C++ standard so effort to keep the library compliant with the ANSI C++ standard so
if your compiler meets the standard, then this library should work if your compiler meets the standard, then this library should work
for you. Please let us know if this is not the case! for you. Please let us know if this is not the case!
</p><div class="sect2" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="id2804010"></a>Windows Note</h3></div></div><div></div></div><p> </p><div class="sect2" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="id856960"></a>Windows Note</h3></div></div></div><p>
As we understand things, Visual C++ does not have the file As we understand things, Visual C++ does not have the file
<tt class="filename">config.h</tt> which is used to make platform <code class="filename">config.h</code> which is used to make platform
specific definitions. In this situation, we assume that you specific definitions. In this situation, we assume that you
have access to <tt class="classname">sstream</tt>. Our understanding is that have access to <code class="classname">sstream</code>. Our understanding is that
this should not be a problem for VC++ 7.x. However, if this this should not be a problem for VC++ 7.x. However, if this
is not the case and you need to use <tt class="classname">strstream</tt>, is not the case and you need to use <code class="classname">strstream</code>,
then simply tell your compiler to define the variable then simply tell your compiler to define the variable
<tt class="constant">HAVE_STRSTREAM</tt> and undefine <code class="constant">HAVE_STRSTREAM</code> and undefine
<tt class="constant">HAVE_SSTREAM</tt> That <code class="constant">HAVE_SSTREAM</code> That
<span class="emphasis"><em>should</em></span> work. We think. Alternatively, just edit <span class="emphasis"><em>should</em></span> work. We think. Alternatively, just edit
the files <tt class="filename">ValueArg.h</tt> and <tt class="filename">MultiArg.h</tt>. the files <code class="filename">ValueArg.h</code> and <code class="filename">MultiArg.h</code>.
</p></div><p> </p></div><p>
</p><div class="sect2" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="id2804071"></a>Random Note</h3></div></div><div></div></div><p> </p><div class="sect2" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="id857012"></a>Random Note</h3></div></div></div><p>
If your compiler doesn't support the <tt class="methodname">using</tt> syntax used If your compiler doesn't support the <code class="methodname">using</code> syntax used
in <tt class="classname">UnlabeledValueArg</tt> and in <code class="classname">UnlabeledValueArg</code> and
<tt class="classname">UnlabeledMultiArg</tt> to support two stage name lookup, <code class="classname">UnlabeledMultiArg</code> to support two stage name lookup,
then you have two options. Either comment out the statements if you don't then you have two options. Either comment out the statements if you don't
need two stage name lookup, or do a bunch of search and replace and use need two stage name lookup, or do a bunch of search and replace and use
the <tt class="methodname">this</tt> pointer syntax: e.g. the <code class="methodname">this</code> pointer syntax: e.g.
<tt class="methodname">this-&gt;_ignoreable</tt> instead <code class="methodname">this-&gt;_ignoreable</code> instead
of just <tt class="methodname">_ignorable</tt> (do this for each variable of just <code class="methodname">_ignorable</code> (do this for each variable
or method referenced by <tt class="methodname">using</tt>). or method referenced by <code class="methodname">using</code>).
</p></div><p> </p></div><p>
</p></div></div><div class="chapter" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title"><a id="FUNDAMENTAL_CLASSES"></a>Chapter 2. Fundamental Classes</h2></div></div><div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><a href="#COMMAND_LINE">CmdLine</a></dt><dt><a href="#id2853573">SwitchArg</a></dt><dt><a href="#id2853610">ValueArg</a></dt><dt><a href="#id2853662">MultiArg</a></dt><dt><a href="#id2853779">MultiSwitchArg</a></dt><dt><a href="#UNLABELED_MULTI_ARG">UnlabeledValueArg</a></dt><dt><a href="#id2853345">UnlabeledMultiArg</a></dt></dl></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="COMMAND_LINE"></a><tt class="classname">CmdLine</tt></h2></div></div><div></div></div><p> </p></div></div><div class="chapter" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title"><a id="FUNDAMENTAL_CLASSES"></a>Chapter 2. Fundamental Classes</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="sect1"><a href="#COMMAND_LINE"><code class="classname">CmdLine</code></a></span></dt><dt><span class="sect1"><a href="#id857113"><code class="classname">SwitchArg</code></a></span></dt><dt><span class="sect1"><a href="#id857146"><code class="classname">ValueArg</code></a></span></dt><dt><span class="sect1"><a href="#id857193"><code class="classname">MultiArg</code></a></span></dt><dt><span class="sect1"><a href="#id857294"><code class="classname">MultiSwitchArg</code></a></span></dt><dt><span class="sect1"><a href="#UNLABELED_MULTI_ARG"><code class="classname">UnlabeledValueArg</code></a></span></dt><dt><span class="sect1"><a href="#id857468"><code class="classname">UnlabeledMultiArg</code></a></span></dt></dl></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="COMMAND_LINE"></a><code class="classname">CmdLine</code></h2></div></div></div><p>
The <tt class="classname">CmdLine</tt> class contains the arguments that define The <code class="classname">CmdLine</code> class contains the arguments that define
the command line and manages the parsing of the command line. The the command line and manages the parsing of the command line. The
<tt class="classname">CmdLine</tt> doesn't parse the command line itself it only <code class="classname">CmdLine</code> doesn't parse the command line itself it only
manages the parsing. The actual parsing of individual arguments occurs within manages the parsing. The actual parsing of individual arguments occurs within
the arguments themselves. The <tt class="classname">CmdLine</tt> keeps track of the arguments themselves. The <code class="classname">CmdLine</code> keeps track of
of the required arguments, <a href="#XOR" title="I want one argument or the other, but not both...">relationships</a> of the required arguments, <a href="#XOR" title="I want one argument or the other, but not both...">relationships</a>
between arguments, and <a href="#CHANGE_OUTPUT" title="I want different output than what is provided...">output</a> generation. between arguments, and <a href="#CHANGE_OUTPUT" title="I want different output than what is provided...">output</a> generation.
</p></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="id2853573"></a><tt class="classname">SwitchArg</tt></h2></div></div><div></div></div><p><tt class="classname">SwitchArg</tt>s are what the name implies: </p></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="id857113"></a><code class="classname">SwitchArg</code></h2></div></div></div><p><code class="classname">SwitchArg</code>s are what the name implies:
simple, on/off, boolean switches. Use <tt class="classname">SwitchArg</tt>s simple, on/off, boolean switches. Use <code class="classname">SwitchArg</code>s
anytime you want to turn anytime you want to turn
some sort of system property on or off. <tt class="classname">SwitchArg</tt>s some sort of system property on or off. <code class="classname">SwitchArg</code>s
don't parse a value. They return <tt class="constant">TRUE</tt> or don't parse a value. They return <code class="constant">TRUE</code> or
<tt class="constant">FALSE</tt>, depending on whether the switch has been found <code class="constant">FALSE</code>, depending on whether the switch has been found
on the command line and what the default value was defined as.</p></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="id2853610"></a><tt class="classname">ValueArg</tt></h2></div></div><div></div></div><p><tt class="classname">ValueArg</tt>s are arguments that read a on the command line and what the default value was defined as.</p></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="id857146"></a><code class="classname">ValueArg</code></h2></div></div></div><p><code class="classname">ValueArg</code>s are arguments that read a
value of some type value of some type
from the command line. Any time you need a file name, a number, from the command line. Any time you need a file name, a number,
etc. use a <tt class="classname">ValueArg</tt> or one of its variants. etc. use a <code class="classname">ValueArg</code> or one of its variants.
All <tt class="classname">ValueArg</tt>s are All <code class="classname">ValueArg</code>s are
<a href="#FOOTNOTES"> templatized**</a> and will attempt to parse <a href="#FOOTNOTES"> templatized**</a> and will attempt to parse
the string its flag matches on the command line as the type it is the string its flag matches on the command line as the type it is
specified as. <tt class="classname">ValueArg&lt;int&gt;</tt> specified as. <code class="classname">ValueArg&lt;int&gt;</code>
will attempt to parse an will attempt to parse an
int, <tt class="classname">ValueArg&lt;float&gt;</tt> will attempt to int, <code class="classname">ValueArg&lt;float&gt;</code> will attempt to
parse a float, etc. If <tt class="methodname">operator&gt;&gt;</tt> parse a float, etc. If <code class="methodname">operator&gt;&gt;</code>
for the specified type doesn't for the specified type doesn't
recognize the string on the command line as its defined type, then recognize the string on the command line as its defined type, then
an exception will be thrown. an exception will be thrown.
</p></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="id2853662"></a><tt class="classname">MultiArg</tt></h2></div></div><div></div></div><p> </p></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="id857193"></a><code class="classname">MultiArg</code></h2></div></div></div><p>
A <tt class="classname">MultiArg</tt> is a <tt class="classname">ValueArg</tt> that A <code class="classname">MultiArg</code> is a <code class="classname">ValueArg</code> that
can be specified more than once on a command line and instead of returning can be specified more than once on a command line and instead of returning
a single value, returns a <tt class="classname">vector</tt> of values. a single value, returns a <code class="classname">vector</code> of values.
</p><p> </p><p>
Imagine a compiler that allows you to specify multiple directories Imagine a compiler that allows you to specify multiple directories
to search for libraries... to search for libraries...
@ -267,96 +267,96 @@ to search for libraries...
% fooCompiler -L /dir/num1 -L /dir/num2 file.foo % fooCompiler -L /dir/num1 -L /dir/num2 file.foo
</pre><p> </pre><p>
Exceptions will occur if you try to do this Exceptions will occur if you try to do this
with a <tt class="classname">ValueArg</tt> or a <tt class="classname">SwitchArg</tt>. with a <code class="classname">ValueArg</code> or a <code class="classname">SwitchArg</code>.
In situations like this, you will want to use a In situations like this, you will want to use a
<tt class="classname">MultiArg</tt>. A <code class="classname">MultiArg</code>. A
<tt class="classname">MultiArg</tt> is essentially a <code class="classname">MultiArg</code> is essentially a
<tt class="classname">ValueArg</tt> that appends any <code class="classname">ValueArg</code> that appends any
value that it matches and parses onto a vector of values. When the value that it matches and parses onto a vector of values. When the
<tt class="methodname">getValue()</tt> method is called, a vector of <code class="methodname">getValue()</code> method is called, a vector of
values, instead of a single value is returned. A values, instead of a single value is returned. A
<tt class="classname">MultiArg</tt> is declared much like <code class="classname">MultiArg</code> is declared much like
a <tt class="classname">ValueArg</tt>: a <code class="classname">ValueArg</code>:
</p><pre class="programlisting"> </p><pre class="programlisting">
MultiArg&lt;int&gt; itest(&quot;i&quot;, &quot;intTest&quot;, &quot;multi int test&quot;, false,&quot;int&quot; ); MultiArg&lt;int&gt; itest("i", "intTest", "multi int test", false,"int" );
cmd.add( itest ); cmd.add( itest );
</pre><p> </pre><p>
Note that <tt class="classname">MultiArg</tt>s can be added to the Note that <code class="classname">MultiArg</code>s can be added to the
<tt class="classname">CmdLine</tt> in any order (unlike <code class="classname">CmdLine</code> in any order (unlike
<a href="#UNLABELED_MULTI_ARG" title="UnlabeledValueArg"> UnlabeledMultiArg</a>). <a href="#UNLABELED_MULTI_ARG" title="UnlabeledValueArg"> UnlabeledMultiArg</a>).
</p></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="id2853779"></a><tt class="classname">MultiSwitchArg</tt></h2></div></div><div></div></div><p> </p></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="id857294"></a><code class="classname">MultiSwitchArg</code></h2></div></div></div><p>
A <tt class="classname">MultiSwitchArg</tt> is a <tt class="classname">SwitchArg</tt> A <code class="classname">MultiSwitchArg</code> is a <code class="classname">SwitchArg</code>
that can be specified more than once on a command line. that can be specified more than once on a command line.
This can be useful This can be useful
when command lines are constructed automatically from within other applications when command lines are constructed automatically from within other applications
or when a switch occurring or when a switch occurring
more than once indicates a value (-V means a little verbose -V -V -V means a lot more than once indicates a value (-V means a little verbose -V -V -V means a lot
verbose), You can use a <tt class="classname">MultiSwitchArg</tt>. verbose), You can use a <code class="classname">MultiSwitchArg</code>.
The call The call
to <tt class="methodname">getValue()</tt> for a <tt class="classname">MultiSwitchArg</tt> returns the number (int) of times to <code class="methodname">getValue()</code> for a <code class="classname">MultiSwitchArg</code> returns the number (int) of times
the switch has been found on the command line in addition to the default value. the switch has been found on the command line in addition to the default value.
Here is an example using the default initial value of 0: Here is an example using the default initial value of 0:
</p><pre class="programlisting"> </p><pre class="programlisting">
MultiSwitchArg quiet(&quot;q&quot;,&quot;quiet&quot;,&quot;Reduce the volume of output&quot;); MultiSwitchArg quiet("q","quiet","Reduce the volume of output");
cmd.add( quiet ); cmd.add( quiet );
</pre><p> </pre><p>
Alternatively, you can specify your own initial value: Alternatively, you can specify your own initial value:
</p><pre class="programlisting"> </p><pre class="programlisting">
MultiSwitchArg quiet(&quot;q&quot;,&quot;quiet&quot;,&quot;Reduce the volume of output&quot;,5); MultiSwitchArg quiet("q","quiet","Reduce the volume of output",5);
cmd.add( quiet ); cmd.add( quiet );
</pre><p> </pre><p>
</p></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="UNLABELED_MULTI_ARG"></a><tt class="classname">UnlabeledValueArg</tt></h2></div></div><div></div></div><p> </p></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="UNLABELED_MULTI_ARG"></a><code class="classname">UnlabeledValueArg</code></h2></div></div></div><p>
An <tt class="classname">UnlabeledValueArg</tt> is a <tt class="classname">ValueArg</tt> that is not identified by a flag on the command line. Instead An <code class="classname">UnlabeledValueArg</code> is a <code class="classname">ValueArg</code> that is not identified by a flag on the command line. Instead
<tt class="classname">UnlabeledValueArg</tt>s are identified by their position in <code class="classname">UnlabeledValueArg</code>s are identified by their position in
the argv array. the argv array.
</p><p> </p><p>
To this point all of our arguments have had labels (flags) To this point all of our arguments have had labels (flags)
identifying them on the command line, but there are some identifying them on the command line, but there are some
situations where flags are burdensome and not worth the effort. One situations where flags are burdensome and not worth the effort. One
example might be if you want to implement a magical command we'll example might be if you want to implement a magical command we'll
call <b class="command">copy</b>. All <b class="command">copy</b> does is call <span><strong class="command">copy</strong></span>. All <span><strong class="command">copy</strong></span> does is
copy the file specified copy the file specified
in the first argument to the file specified in the second argument. in the first argument to the file specified in the second argument.
We can do this using <tt class="classname">UnlabeledValueArg</tt>s which are pretty We can do this using <code class="classname">UnlabeledValueArg</code>s which are pretty
much just <tt class="classname">ValueArg</tt>s without the flag specified, much just <code class="classname">ValueArg</code>s without the flag specified,
which tells which tells
the <tt class="classname">CmdLine</tt> object to treat them accordingly. the <code class="classname">CmdLine</code> object to treat them accordingly.
The code would look like this: The code would look like this:
</p><pre class="programlisting"> </p><pre class="programlisting">
UnlabeledValueArg&lt;float&gt; nolabel( &quot;name&quot;, &quot;unlabeled test&quot;, 3.14, UnlabeledValueArg&lt;float&gt; nolabel( "name", "unlabeled test", 3.14,
&quot;nameString&quot; ); "nameString" );
cmd.add( nolabel ); cmd.add( nolabel );
</pre><p> </pre><p>
Everything else is handled identically to what is seen above. The Everything else is handled identically to what is seen above. The
only difference to be aware of, and this is important: <span class="emphasis"><em>the order only difference to be aware of, and this is important: <span class="emphasis"><em>the order
that UnlabeledValueArgs are added to the <tt class="classname">CmdLine</tt> that UnlabeledValueArgs are added to the <code class="classname">CmdLine</code>
is the order that they will be parsed!!!!</em></span> is the order that they will be parsed!!!!</em></span>
This is <span class="emphasis"><em>not</em></span> the case for normal This is <span class="emphasis"><em>not</em></span> the case for normal
<tt class="classname">SwitchArg</tt>s and <tt class="classname">ValueArg</tt>s. <code class="classname">SwitchArg</code>s and <code class="classname">ValueArg</code>s.
What happens internally is the first argument that the What happens internally is the first argument that the
<tt class="classname">CmdLine</tt> doesn't recognize is assumed to be <code class="classname">CmdLine</code> doesn't recognize is assumed to be
the first <tt class="classname">UnlabeledValueArg</tt> and the first <code class="classname">UnlabeledValueArg</code> and
parses it as such. Note that you are allowed to intersperse labeled parses it as such. Note that you are allowed to intersperse labeled
args (SwitchArgs and ValueArgs) in between args (SwitchArgs and ValueArgs) in between
<tt class="classname">UnlabeledValueArgs</tt> (either on the command line <code class="classname">UnlabeledValueArgs</code> (either on the command line
or in the declaration), but the <tt class="classname">UnlabeledValueArgs</tt> or in the declaration), but the <code class="classname">UnlabeledValueArgs</code>
will still be parsed in the order they are added. Just remember that order is will still be parsed in the order they are added. Just remember that order is
important for unlabeled arguments. important for unlabeled arguments.
</p></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="id2853345"></a><tt class="classname">UnlabeledMultiArg</tt></h2></div></div><div></div></div><p> </p></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="id857468"></a><code class="classname">UnlabeledMultiArg</code></h2></div></div></div><p>
An <tt class="classname">UnlabeledMultiArg</tt> is an <tt class="classname">UnlabeledValueArg</tt> that allows more than one value to be specified. Only one An <code class="classname">UnlabeledMultiArg</code> is an <code class="classname">UnlabeledValueArg</code> that allows more than one value to be specified. Only one
<tt class="classname">UnlabeledMultiArg</tt> can be specified per command line. <code class="classname">UnlabeledMultiArg</code> can be specified per command line.
The <tt class="classname">UnlabeledMultiArg</tt> simply reads the remaining The <code class="classname">UnlabeledMultiArg</code> simply reads the remaining
values from argv up until -- or the end of the array is reached. values from argv up until -- or the end of the array is reached.
</p><p> </p><p>
Say you want a strange command Say you want a strange command
that searches each file specified for a given string (let's call it that searches each file specified for a given string (let's call it
<b class="command">grep</b>), but you don't want to have to type in all of the file <span><strong class="command">grep</strong></span>), but you don't want to have to type in all of the file
names or write a script to do it for you. Say, names or write a script to do it for you. Say,
</p><pre class="programlisting"> </p><pre class="programlisting">
@ -364,7 +364,7 @@ names or write a script to do it for you. Say,
</pre><p> </pre><p>
First remember that the <span class="emphasis"><em>*</em></span> is handled by the shell and First remember that the <span class="emphasis"><em>*</em></span> is handled by the shell and
expanded accordingly, so what the program <b class="command">grep</b> sees is expanded accordingly, so what the program <span><strong class="command">grep</strong></span> sees is
really something like: really something like:
</p><pre class="programlisting"> </p><pre class="programlisting">
@ -372,13 +372,13 @@ really something like:
</pre><p> </pre><p>
To handle situations where multiple, unlabeled arguments are needed, To handle situations where multiple, unlabeled arguments are needed,
we provide the <tt class="classname">UnlabeledMultiArg</tt>. we provide the <code class="classname">UnlabeledMultiArg</code>.
<tt class="classname">UnlabeledMultiArg</tt>s <code class="classname">UnlabeledMultiArg</code>s
are declared much like everything else, but with only a description are declared much like everything else, but with only a description
of the arguments. By default, if an <tt class="classname">UnlabeledMultiArg</tt> of the arguments. By default, if an <code class="classname">UnlabeledMultiArg</code>
is specified, then at least one is required to be present or an is specified, then at least one is required to be present or an
exception will be thrown. The most important thing to remember is, exception will be thrown. The most important thing to remember is,
that like <tt class="classname">UnlabeledValueArg</tt>s: order matters! that like <code class="classname">UnlabeledValueArg</code>s: order matters!
In fact, <span class="emphasis"><em>an UnlabeledMultiArg must be the last argument added to the In fact, <span class="emphasis"><em>an UnlabeledMultiArg must be the last argument added to the
CmdLine!</em></span>. Here is what a declaration looks like: CmdLine!</em></span>. Here is what a declaration looks like:
@ -387,7 +387,7 @@ CmdLine!</em></span>. Here is what a declaration looks like:
// //
// UnlabeledMultiArg must be the LAST argument added! // UnlabeledMultiArg must be the LAST argument added!
// //
UnlabeledMultiArg&lt;string&gt; multi(&quot;file names&quot;); UnlabeledMultiArg&lt;string&gt; multi("file names");
cmd.add( multi ); cmd.add( multi );
cmd.parse(argc, argv); cmd.parse(argc, argv);
@ -395,30 +395,30 @@ CmdLine!</em></span>. Here is what a declaration looks like:
</pre><p> </pre><p>
You must only ever specify one (1) <tt class="classname">UnlabeledMultiArg</tt>. You must only ever specify one (1) <code class="classname">UnlabeledMultiArg</code>.
One <tt class="classname">UnlabeledMultiArg</tt> will read every unlabeled One <code class="classname">UnlabeledMultiArg</code> will read every unlabeled
Arg that wasn't already processed by a Arg that wasn't already processed by a
<tt class="classname">UnlabeledValueArg</tt> into a <code class="classname">UnlabeledValueArg</code> into a
<tt class="classname">vector</tt> of type T. Any <code class="classname">vector</code> of type T. Any
<tt class="classname">UnlabeledValueArg</tt> or other <code class="classname">UnlabeledValueArg</code> or other
<tt class="classname">UnlabeledMultiArg</tt> specified after the first <code class="classname">UnlabeledMultiArg</code> specified after the first
<tt class="classname">UnlabeledMultiArg</tt> will be ignored, and if <code class="classname">UnlabeledMultiArg</code> will be ignored, and if
they are required, they are required,
exceptions will be thrown. When you call the exceptions will be thrown. When you call the
<tt class="methodname">getValue()</tt> <code class="methodname">getValue()</code>
method of the <tt class="classname">UnlabeledValueArg</tt> argument, method of the <code class="classname">UnlabeledValueArg</code> argument,
a <tt class="classname">vector</tt> a <code class="classname">vector</code>
will be returned. If you can imagine a situation where there will will be returned. If you can imagine a situation where there will
be multiple args of multiple types (stings, ints, floats, etc.) be multiple args of multiple types (stings, ints, floats, etc.)
then just declare the <tt class="classname">UnlabeledMultiArg</tt> as type then just declare the <code class="classname">UnlabeledMultiArg</code> as type
<tt class="classname">string</tt> and parse the different values yourself or use <code class="classname">string</code> and parse the different values yourself or use
several <tt class="classname">UnlabeledValueArg</tt>s. several <code class="classname">UnlabeledValueArg</code>s.
</p></div></div><div class="chapter" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title"><a id="COMPLICATIONS"></a>Chapter 3. Complications</h2></div></div><div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><a href="#COMBINE_SWITCHES">I want to combine multiple switches into one argument...</a></dt><dt><a href="#XOR">I want one argument or the other, but not both...</a></dt><dt><a href="#NO_FLAG">I have more arguments than single flags make sense for...</a></dt><dt><a href="#CONSTRAINT">I want to constrain the values allowed for a particular </p></div></div><div class="chapter" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title"><a id="COMPLICATIONS"></a>Chapter 3. Complications</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="sect1"><a href="#COMBINE_SWITCHES">I want to combine multiple switches into one argument...</a></span></dt><dt><span class="sect1"><a href="#XOR">I want one argument or the other, but not both...</a></span></dt><dt><span class="sect1"><a href="#NO_FLAG">I have more arguments than single flags make sense for...</a></span></dt><dt><span class="sect1"><a href="#CONSTRAINT">I want to constrain the values allowed for a particular
argument...</a></dt><dt><a href="#ARG_ADD_CMDLINE">I want the Args to add themselves to the CmdLine...</a></dt><dt><a href="#CHANGE_OUTPUT">I want different output than what is provided...</a></dt><dt><a href="#NO_HELP_VERSION">I don't want the --help and --version switches to be created automatically...</a></dt></dl></div><p> argument...</a></span></dt><dt><span class="sect1"><a href="#ARG_ADD_CMDLINE">I want the Args to add themselves to the CmdLine...</a></span></dt><dt><span class="sect1"><a href="#CHANGE_OUTPUT">I want different output than what is provided...</a></span></dt><dt><span class="sect1"><a href="#NO_HELP_VERSION">I don't want the --help and --version switches to be created automatically...</a></span></dt></dl></div><p>
Naturally, what we have seen to this point doesn't satisfy all of Naturally, what we have seen to this point doesn't satisfy all of
our needs. our needs.
</p><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="COMBINE_SWITCHES"></a>I want to combine multiple switches into one argument...</h2></div></div><div></div></div><p> </p><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="COMBINE_SWITCHES"></a>I want to combine multiple switches into one argument...</h2></div></div></div><p>
Multiple <tt class="classname">SwitchArg</tt>s can be combined into a Multiple <code class="classname">SwitchArg</code>s can be combined into a
single argument on the command line. If you have switches -a, -b and -c single argument on the command line. If you have switches -a, -b and -c
it is valid to do either: it is valid to do either:
@ -440,51 +440,51 @@ it is valid to do either:
This is to make this library more in line with the POSIX and GNU This is to make this library more in line with the POSIX and GNU
standards (as I understand them). standards (as I understand them).
</p></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="XOR"></a>I want one argument or the other, but not both...</h2></div></div><div></div></div><p> </p></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="XOR"></a>I want one argument or the other, but not both...</h2></div></div></div><p>
Suppose you have a command that must read input from one of two Suppose you have a command that must read input from one of two
possible locations, either a local file or a URL. The command possible locations, either a local file or a URL. The command
<span class="emphasis"><em>must</em></span> read something, so <span class="emphasis"><em>one</em></span> <span class="emphasis"><em>must</em></span> read something, so <span class="emphasis"><em>one</em></span>
argument is required, but argument is required, but
not both, yet neither argument is strictly necessary by itself. not both, yet neither argument is strictly necessary by itself.
This is called &quot;exclusive or&quot; or &quot;XOR&quot;. To accommodate this This is called "exclusive or" or "XOR". To accommodate this
situation, there is now an option to add two or more situation, there is now an option to add two or more
<tt class="classname">Arg</tt>s to <code class="classname">Arg</code>s to
a <tt class="classname">CmdLine</tt> that are exclusively or'd with one another: a <code class="classname">CmdLine</code> that are exclusively or'd with one another:
<tt class="methodname">xorAdd()</tt>. This means that exactly one of the <code class="methodname">xorAdd()</code>. This means that exactly one of the
<tt class="classname">Arg</tt>s must be set and no more. <code class="classname">Arg</code>s must be set and no more.
</p><p> </p><p>
<tt class="methodname">xorAdd()</tt> comes in two flavors, either <code class="methodname">xorAdd()</code> comes in two flavors, either
<tt class="methodname">xorAdd(Arg&amp; a, Arg&amp; b)</tt> <code class="methodname">xorAdd(Arg&amp; a, Arg&amp; b)</code>
to add just two <tt class="classname">Arg</tt>s to be xor'd and to add just two <code class="classname">Arg</code>s to be xor'd and
<tt class="methodname">xorAdd( vector&lt;Arg*&gt; xorList )</tt> <code class="methodname">xorAdd( vector&lt;Arg*&gt; xorList )</code>
to add more than two <tt class="classname">Arg</tt>s. to add more than two <code class="classname">Arg</code>s.
</p><pre class="programlisting"> </p><pre class="programlisting">
ValueArg&lt;string&gt; fileArg(&quot;f&quot;,&quot;file&quot;,&quot;File name to read&quot;,true,&quot;homer&quot;, ValueArg&lt;string&gt; fileArg("f","file","File name to read",true,"homer",
&quot;filename&quot;); "filename");
ValueArg&lt;string&gt; urlArg(&quot;u&quot;,&quot;url&quot;,&quot;URL to load&quot;,true, ValueArg&lt;string&gt; urlArg("u","url","URL to load",true,
&quot;http://example.com&quot;, &quot;URL&quot;); "http://example.com", "URL");
cmd.xorAdd( fileArg, urlArg ); cmd.xorAdd( fileArg, urlArg );
cmd.parse(argc, argv); cmd.parse(argc, argv);
</pre><p> </pre><p>
Once one <tt class="classname">Arg</tt> in the xor list is matched on the Once one <code class="classname">Arg</code> in the xor list is matched on the
<tt class="classname">CmdLine</tt> then the others in the xor list will be <code class="classname">CmdLine</code> then the others in the xor list will be
marked as set. The question then, is how to determine which of the marked as set. The question then, is how to determine which of the
<tt class="classname">Arg</tt>s has been set? This is accomplished by calling the <code class="classname">Arg</code>s has been set? This is accomplished by calling the
isSet() method for each <tt class="classname">Arg</tt>. If the isSet() method for each <code class="classname">Arg</code>. If the
<tt class="classname">Arg</tt> has been <code class="classname">Arg</code> has been
matched on the command line, the <tt class="methodname">isSet()</tt> will return matched on the command line, the <code class="methodname">isSet()</code> will return
<tt class="constant">TRUE</tt>, whereas if the <tt class="classname">Arg</tt> <code class="constant">TRUE</code>, whereas if the <code class="classname">Arg</code>
has been set as a result of matching the other <tt class="classname">Arg</tt> has been set as a result of matching the other <code class="classname">Arg</code>
that was xor'd <tt class="methodname">isSet()</tt> will that was xor'd <code class="methodname">isSet()</code> will
return <tt class="constant">FALSE</tt>. return <code class="constant">FALSE</code>.
(Of course, if the <tt class="classname">Arg</tt> was not xor'd and (Of course, if the <code class="classname">Arg</code> was not xor'd and
wasn't matched, it will also return <tt class="constant">FALSE</tt>.) wasn't matched, it will also return <code class="constant">FALSE</code>.)
</p><pre class="programlisting"> </p><pre class="programlisting">
@ -495,102 +495,102 @@ wasn't matched, it will also return <tt class="constant">FALSE</tt>.)
else else
// Should never get here because TCLAP will note that one of the // Should never get here because TCLAP will note that one of the
// required args above has not been set. // required args above has not been set.
throw(&quot;Very bad things...&quot;); throw("Very bad things...");
</pre><p> </pre><p>
</p></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="NO_FLAG"></a>I have more arguments than single flags make sense for...</h2></div></div><div></div></div><p> </p></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="NO_FLAG"></a>I have more arguments than single flags make sense for...</h2></div></div></div><p>
Some commands have so many options that single flags no longer map Some commands have so many options that single flags no longer map
sensibly to the available options. In this case, it is desirable to sensibly to the available options. In this case, it is desirable to
specify <tt class="classname">Arg</tt>s using only long options. This one is easy to specify <code class="classname">Arg</code>s using only long options. This one is easy to
accomplish, just make the flag value blank in the <tt class="classname">Arg</tt> accomplish, just make the flag value blank in the <code class="classname">Arg</code>
constructor. This will tell the <tt class="classname">Arg</tt> that only the long constructor. This will tell the <code class="classname">Arg</code> that only the long
option should be matched and will force users to specify the long option should be matched and will force users to specify the long
option on the command line. The help output is updated accordingly. option on the command line. The help output is updated accordingly.
</p><pre class="programlisting"> </p><pre class="programlisting">
ValueArg&lt;string&gt; fileArg(&quot;&quot;,&quot;file&quot;,&quot;File name&quot;,true,&quot;homer&quot;,&quot;filename&quot;); ValueArg&lt;string&gt; fileArg("","file","File name",true,"homer","filename");
SwitchArg caseSwitch(&quot;&quot;,&quot;upperCase&quot;,&quot;Print in upper case&quot;,false); SwitchArg caseSwitch("","upperCase","Print in upper case",false);
</pre><p> </pre><p>
</p></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="CONSTRAINT"></a>I want to constrain the values allowed for a particular </p></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="CONSTRAINT"></a>I want to constrain the values allowed for a particular
argument...</h2></div></div><div></div></div><p> argument...</h2></div></div></div><p>
<span class="emphasis"><em>Interface Change!!!</em></span> Sorry folks, but we've changed <span class="emphasis"><em>Interface Change!!!</em></span> Sorry folks, but we've changed
the interface since version 1.0.X for constraining <tt class="classname">Arg</tt>s. the interface since version 1.0.X for constraining <code class="classname">Arg</code>s.
Constraints are now hidden behind the <tt class="classname">Constraint</tt> Constraints are now hidden behind the <code class="classname">Constraint</code>
interface. To interface. To
constrain an <tt class="classname">Arg</tt> simply implement the interface constrain an <code class="classname">Arg</code> simply implement the interface
and specify the new class in the constructor as before. and specify the new class in the constructor as before.
</p><p> </p><p>
You can still constrain <tt class="classname">Arg</tt>s based on You can still constrain <code class="classname">Arg</code>s based on
a list of values. Instead of adding a <tt class="classname">vector</tt> of a list of values. Instead of adding a <code class="classname">vector</code> of
allowed values to the <tt class="classname">Arg</tt> directly, allowed values to the <code class="classname">Arg</code> directly,
create a <tt class="classname">ValuesConstraint</tt> object create a <code class="classname">ValuesConstraint</code> object
with a <tt class="classname">vector</tt> of values and add that to the with a <code class="classname">vector</code> of values and add that to the
<tt class="classname">Arg</tt>. The <tt class="classname">Arg</tt> constructors <code class="classname">Arg</code>. The <code class="classname">Arg</code> constructors
have been modified accordingly. have been modified accordingly.
</p><p> </p><p>
When the value for the When the value for the
<tt class="classname">Arg</tt> is parsed, <code class="classname">Arg</code> is parsed,
it is checked against the list of values specified in the it is checked against the list of values specified in the
<tt class="classname">ValuesConstraint</tt>. <code class="classname">ValuesConstraint</code>.
If the value is in the list then it is accepted. If If the value is in the list then it is accepted. If
not, then an exception is thrown. Here is a simple example: not, then an exception is thrown. Here is a simple example:
</p><pre class="programlisting"> </p><pre class="programlisting">
vector&lt;string&gt; allowed; vector&lt;string&gt; allowed;
allowed.push_back(&quot;homer&quot;); allowed.push_back("homer");
allowed.push_back(&quot;marge&quot;); allowed.push_back("marge");
allowed.push_back(&quot;bart&quot;); allowed.push_back("bart");
allowed.push_back(&quot;lisa&quot;); allowed.push_back("lisa");
allowed.push_back(&quot;maggie&quot;); allowed.push_back("maggie");
ValuesConstraint&lt;string&gt; allowedVals( allowed ); ValuesConstraint&lt;string&gt; allowedVals( allowed );
ValueArg&lt;string&gt; nameArg(&quot;n&quot;,&quot;name&quot;,&quot;Name to print&quot;,true,&quot;homer&quot;,&amp;allowedVals); ValueArg&lt;string&gt; nameArg("n","name","Name to print",true,"homer",&amp;allowedVals);
cmd.add( nameArg ); cmd.add( nameArg );
</pre><p> </pre><p>
When a <tt class="classname">ValuesConstraint</tt> is specified, When a <code class="classname">ValuesConstraint</code> is specified,
instead of a type description being specified in the instead of a type description being specified in the
<tt class="classname">Arg</tt>, a <code class="classname">Arg</code>, a
type description is created by concatenating the values in the type description is created by concatenating the values in the
allowed list using operator&lt;&lt; for the specified type. The allowed list using operator&lt;&lt; for the specified type. The
help/usage for the <tt class="classname">Arg</tt> therefore lists the help/usage for the <code class="classname">Arg</code> therefore lists the
allowable values. Because of this, you might want to keep the list allowable values. Because of this, you might want to keep the list
relatively small, however there is no limit on this. relatively small, however there is no limit on this.
</p><p> </p><p>
Obviously, a list of allowed values isn't always the best way to Obviously, a list of allowed values isn't always the best way to
constrain things. For instance, one might wish to allow only constrain things. For instance, one might wish to allow only
integers greater than 0. In this case, simply create a class that integers greater than 0. In this case, simply create a class that
implements the <tt class="classname">Constraint&lt;int&gt;</tt> interface and implements the <code class="classname">Constraint&lt;int&gt;</code> interface and
checks whether the value parsed is greater than 0 (done in the checks whether the value parsed is greater than 0 (done in the
<tt class="methodname">check()</tt> method) and create your <code class="methodname">check()</code> method) and create your
<tt class="classname">Arg</tt> with your new <tt class="classname">Constraint</tt>. <code class="classname">Arg</code> with your new <code class="classname">Constraint</code>.
</p></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="ARG_ADD_CMDLINE"></a>I want the Args to add themselves to the CmdLine...</h2></div></div><div></div></div><p> </p></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="ARG_ADD_CMDLINE"></a>I want the Args to add themselves to the CmdLine...</h2></div></div></div><p>
New constructors have been added for each <tt class="classname">Arg</tt> New constructors have been added for each <code class="classname">Arg</code>
that take a <tt class="classname">CmdLine</tt> object as an argument. that take a <code class="classname">CmdLine</code> object as an argument.
Each <tt class="classname">Arg</tt> then Each <code class="classname">Arg</code> then
<tt class="methodname">add</tt>s itself to the <tt class="classname">CmdLine</tt> <code class="methodname">add</code>s itself to the <code class="classname">CmdLine</code>
object. There is no difference in how the <tt class="classname">Arg</tt> object. There is no difference in how the <code class="classname">Arg</code>
is handled between this method and calling the is handled between this method and calling the
<tt class="methodname">add()</tt> method directly. At the moment, there is <code class="methodname">add()</code> method directly. At the moment, there is
no way to do an <tt class="methodname">xorAdd()</tt> from the constructor. Here no way to do an <code class="methodname">xorAdd()</code> from the constructor. Here
is an example: is an example:
</p><pre class="programlisting"> </p><pre class="programlisting">
// Create the command line. // Create the command line.
CmdLine cmd(&quot;this is a message&quot;, '=', &quot;0.99&quot; ); CmdLine cmd("this is a message", '=', "0.99" );
// Note that the following args take the &quot;cmd&quot; object as arguments. // Note that the following args take the "cmd" object as arguments.
SwitchArg btest(&quot;B&quot;,&quot;existTestB&quot;, &quot;exist Test B&quot;, false, cmd ); SwitchArg btest("B","existTestB", "exist Test B", cmd, false );
ValueArg&lt;string&gt; stest(&quot;s&quot;, &quot;stringTest&quot;, &quot;string test&quot;, true, &quot;homer&quot;, ValueArg&lt;string&gt; stest("s", "stringTest", "string test", true, "homer",
&quot;string&quot;, cmd ); "string", cmd );
UnlabeledValueArg&lt;string&gt; utest(&quot;unTest1&quot;,&quot;unlabeled test one&quot;, UnlabeledValueArg&lt;string&gt; utest("unTest1","unlabeled test one",
&quot;default&quot;,&quot;string&quot;, cmd ); "default","string", cmd );
// NO add() calls! // NO add() calls!
@ -598,14 +598,14 @@ is an example:
cmd.parse(argc,argv); cmd.parse(argc,argv);
</pre><p> </pre><p>
</p></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="CHANGE_OUTPUT"></a>I want different output than what is provided...</h2></div></div><div></div></div><p> </p></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="CHANGE_OUTPUT"></a>I want different output than what is provided...</h2></div></div></div><p>
It is straightforward to change the output generated by It is straightforward to change the output generated by
<span class="emphasis"><em>TCLAP</em></span>. Either subclass the <span class="emphasis"><em>TCLAP</em></span>. Either subclass the
<tt class="classname">StdOutput</tt> class and re-implement the methods you choose, <code class="classname">StdOutput</code> class and re-implement the methods you choose,
or write your own class that implements the or write your own class that implements the
<tt class="classname">CmdLineOutput</tt> interface. Once you have done this, <code class="classname">CmdLineOutput</code> interface. Once you have done this,
then use the <tt class="classname">CmdLine</tt> <tt class="methodname">setOutput</tt> then use the <code class="classname">CmdLine</code> <code class="methodname">setOutput</code>
method to tell the <tt class="classname">CmdLine</tt> to use your new output method to tell the <code class="classname">CmdLine</code> to use your new output
class. Here is a simple example: class. Here is a simple example:
</p><pre class="programlisting"> </p><pre class="programlisting">
class MyOutput : public StdOutput class MyOutput : public StdOutput
@ -613,28 +613,28 @@ class MyOutput : public StdOutput
public: public:
virtual void failure(CmdLineInterface&amp; c, ArgException&amp; e) virtual void failure(CmdLineInterface&amp; c, ArgException&amp; e)
{ {
cerr &lt;&lt; &quot;My special failure message for: &quot; &lt;&lt; endl cerr &lt;&lt; "My special failure message for: " &lt;&lt; endl
&lt;&lt; e.what() &lt;&lt; endl; &lt;&lt; e.what() &lt;&lt; endl;
} }
virtual void usage(CmdLineInterface&amp; c) virtual void usage(CmdLineInterface&amp; c)
{ {
cout &lt;&lt; &quot;my usage message:&quot; &lt;&lt; endl; cout &lt;&lt; "my usage message:" &lt;&lt; endl;
list&lt;Arg*&gt; args = c.getArgList(); list&lt;Arg*&gt; args = c.getArgList();
for (ArgListIterator it = args.begin(); it != args.end(); it++) for (ArgListIterator it = args.begin(); it != args.end(); it++)
cout &lt;&lt; (*it)-&gt;longID() cout &lt;&lt; (*it)-&gt;longID()
&lt;&lt; &quot; (&quot; &lt;&lt; (*it)-&gt;getDescription() &lt;&lt; &quot;)&quot; &lt;&lt; endl; &lt;&lt; " (" &lt;&lt; (*it)-&gt;getDescription() &lt;&lt; ")" &lt;&lt; endl;
} }
virtual void version(CmdLineInterface&amp; c) virtual void version(CmdLineInterface&amp; c)
{ {
cout &lt;&lt; &quot;my version message: 0.1&quot; &lt;&lt; endl; cout &lt;&lt; "my version message: 0.1" &lt;&lt; endl;
} }
}; };
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
CmdLine cmd(&quot;this is a message&quot;, ' ', &quot;0.99&quot; ); CmdLine cmd("this is a message", ' ', "0.99" );
// set the output // set the output
MyOutput my; MyOutput my;
@ -643,88 +643,88 @@ int main(int argc, char** argv)
// proceed normally ... // proceed normally ...
</pre><p> </pre><p>
See <tt class="filename">test4.cpp</tt> in the examples directory for the full See <code class="filename">test4.cpp</code> in the examples directory for the full
example. <span class="emphasis"><em>NOTE</em></span>: if you supply your own Output object, we example. <span class="emphasis"><em>NOTE</em></span>: if you supply your own Output object, we
will not delete it in the <tt class="classname">CmdLine</tt> destructor. This will not delete it in the <code class="classname">CmdLine</code> destructor. This
could lead to a (very small) memory leak if you don't take care of the object could lead to a (very small) memory leak if you don't take care of the object
yourself. yourself.
</p></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="NO_HELP_VERSION"></a>I don't want the --help and --version switches to be created automatically...</h2></div></div><div></div></div><p> </p></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="NO_HELP_VERSION"></a>I don't want the --help and --version switches to be created automatically...</h2></div></div></div><p>
Help and version information is useful for nearly all command line applications Help and version information is useful for nearly all command line applications
and as such we generate flags that provide those options automatically. and as such we generate flags that provide those options automatically.
However, there are situations when these flags are undesirable. For these However, there are situations when these flags are undesirable. For these
cases we've added we've added a forth parameter to the cases we've added we've added a forth parameter to the
<tt class="classname">CmdLine</tt> constructor. Making this boolean parameter <code class="classname">CmdLine</code> constructor. Making this boolean parameter
false will disable automatic help and version generation. false will disable automatic help and version generation.
</p><pre class="programlisting"> </p><pre class="programlisting">
CmdLine cmd(&quot;this is a message&quot;, ' ', &quot;0.99&quot;, false ); CmdLine cmd("this is a message", ' ', "0.99", false );
</pre><p> </pre><p>
</p></div></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="IGNORE_ARGS"></a>Ignoring arguments</h2></div></div><div></div></div><p> </p></div></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="IGNORE_ARGS"></a>Ignoring arguments</h2></div></div></div><p>
The <i class="parameter"><tt>--</tt></i> flag is automatically included in the The <em class="parameter"><code>--</code></em> flag is automatically included in the
<tt class="classname">CmdLine</tt>. <code class="classname">CmdLine</code>.
As (almost) per POSIX and GNU standards, any argument specified As (almost) per POSIX and GNU standards, any argument specified
after the <i class="parameter"><tt>--</tt></i> flag is ignored. after the <em class="parameter"><code>--</code></em> flag is ignored.
<span class="emphasis"><em>Almost</em></span> because if an <span class="emphasis"><em>Almost</em></span> because if an
<tt class="classname">UnlabeledValueArg</tt> that has not been set or an <code class="classname">UnlabeledValueArg</code> that has not been set or an
<tt class="classname">UnlabeledMultiArg</tt> has been specified, by default <code class="classname">UnlabeledMultiArg</code> has been specified, by default
we will assign any arguments beyond the <i class="parameter"><tt>--</tt></i> we will assign any arguments beyond the <em class="parameter"><code>--</code></em>
to the those arguments as to the those arguments as
per the rules above. This is primarily useful if you want to pass per the rules above. This is primarily useful if you want to pass
in arguments with a dash as the first character of the argument. It in arguments with a dash as the first character of the argument. It
should be noted that even if the <i class="parameter"><tt>--</tt></i> flag is should be noted that even if the <em class="parameter"><code>--</code></em> flag is
passed on the command line, the <tt class="classname">CmdLine</tt> will passed on the command line, the <code class="classname">CmdLine</code> will
<span class="emphasis"><em>still</em></span> test to make sure all of the required <span class="emphasis"><em>still</em></span> test to make sure all of the required
arguments are present. arguments are present.
</p><p> </p><p>
Of course, this isn't how POSIX/GNU handle things, they explicitly Of course, this isn't how POSIX/GNU handle things, they explicitly
ignore arguments after the <i class="parameter"><tt>--</tt></i>. To accommodate this, ignore arguments after the <em class="parameter"><code>--</code></em>. To accommodate this,
we can make both <tt class="classname">UnlabeledValueArg</tt>s and we can make both <code class="classname">UnlabeledValueArg</code>s and
<tt class="classname">UnlabeledMultiArg</tt>s ignoreable in their constructors. <code class="classname">UnlabeledMultiArg</code>s ignoreable in their constructors.
See the <a href="html/index.html" target="_top"> API Documentation</a> for details. See the <a href="html/index.html" target="_top"> API Documentation</a> for details.
</p></div><div class="chapter" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title"><a id="NOTES"></a>Chapter 4. Notes</h2></div></div><div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><a href="#DESCRIPTION_EXCEPTIONS">Type Descriptions</a></dt><dt><a href="#VISITORS">Visitors</a></dt><dt><a href="#MORE_INFO">More Information</a></dt></dl></div><p> </p></div><div class="chapter" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title"><a id="NOTES"></a>Chapter 4. Notes</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="sect1"><a href="#DESCRIPTION_EXCEPTIONS">Type Descriptions</a></span></dt><dt><span class="sect1"><a href="#VISITORS">Visitors</a></span></dt><dt><span class="sect1"><a href="#MORE_INFO">More Information</a></span></dt></dl></div><p>
Like all good rules, there are many exceptions.... Like all good rules, there are many exceptions....
</p><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="DESCRIPTION_EXCEPTIONS"></a>Type Descriptions</h2></div></div><div></div></div><p> </p><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="DESCRIPTION_EXCEPTIONS"></a>Type Descriptions</h2></div></div></div><p>
Ideally this library would use RTTI to return a human readable name Ideally this library would use RTTI to return a human readable name
of the type declared for a particular argument. Unfortunately, at of the type declared for a particular argument. Unfortunately, at
least for <b class="command">g++</b>, the names returned aren't least for <span><strong class="command">g++</strong></span>, the names returned aren't
particularly useful. particularly useful.
</p></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="VISITORS"></a>Visitors</h2></div></div><div></div></div><p> </p></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="VISITORS"></a>Visitors</h2></div></div></div><p>
Disclaimer: Almost no one will have any use for Disclaimer: Almost no one will have any use for
<tt class="classname">Visitor</tt>s, they were <code class="classname">Visitor</code>s, they were
added to provide special handling for default arguments. Nothing added to provide special handling for default arguments. Nothing
that <tt class="classname">Visitor</tt>s do couldn't be accomplished that <code class="classname">Visitor</code>s do couldn't be accomplished
by the user after the by the user after the
command line has been parsed. If you're still interested, keep command line has been parsed. If you're still interested, keep
reading... reading...
</p><p> </p><p>
Some of you may be wondering how we get the <i class="parameter"><tt>--help</tt></i>, Some of you may be wondering how we get the <em class="parameter"><code>--help</code></em>,
<i class="parameter"><tt>--version</tt></i> and <i class="parameter"><tt>--</tt></i> <em class="parameter"><code>--version</code></em> and <em class="parameter"><code>--</code></em>
arguments to do their thing without mucking up the arguments to do their thing without mucking up the
<tt class="classname">CmdLine</tt> code with lots of <span class="emphasis"><em>if</em></span> <code class="classname">CmdLine</code> code with lots of <span class="emphasis"><em>if</em></span>
statements and type checking. This is accomplished by using a statements and type checking. This is accomplished by using a
variation on the Visitor Pattern. Actually, it may not be a Visitor variation on the Visitor Pattern. Actually, it may not be a Visitor
Pattern at all, but that's what inspired me. Pattern at all, but that's what inspired me.
</p><p> </p><p>
If we want some argument to do some sort of special handling, If we want some argument to do some sort of special handling,
besides simply parsing a value, then we add a <tt class="classname">Visitor</tt> besides simply parsing a value, then we add a <code class="classname">Visitor</code>
pointer to the <tt class="classname">Arg</tt>. More specifically, we add a pointer to the <code class="classname">Arg</code>. More specifically, we add a
<span class="emphasis"><em>subclass</em></span> of the <tt class="classname">Visitor</tt> <span class="emphasis"><em>subclass</em></span> of the <code class="classname">Visitor</code>
class. Once the argument has been successfully parsed, the class. Once the argument has been successfully parsed, the
<tt class="classname">Visitor</tt> for that argument is <code class="classname">Visitor</code> for that argument is
called. Any data that needs to be operated on is declared in the called. Any data that needs to be operated on is declared in the
<tt class="classname">Visitor</tt> constructor and then operated on in the <code class="classname">Visitor</code> constructor and then operated on in the
<tt class="methodname">visit()</tt> method. A <tt class="classname">Visitor</tt> <code class="methodname">visit()</code> method. A <code class="classname">Visitor</code>
is added to an <tt class="classname">Arg</tt> as the last argument in its is added to an <code class="classname">Arg</code> as the last argument in its
declaration. This may sound declaration. This may sound
complicated, but it is pretty straightforward. Let's see an complicated, but it is pretty straightforward. Let's see an
example. example.
</p><p> </p><p>
Say you want to add an <i class="parameter"><tt>--authors</tt></i> flag to a program that Say you want to add an <em class="parameter"><code>--authors</code></em> flag to a program that
prints the names of the authors when present. First subclass prints the names of the authors when present. First subclass
<tt class="classname">Visitor</tt>: <code class="classname">Visitor</code>:
</p><pre class="programlisting"> </p><pre class="programlisting">
#include &quot;Visitor.h&quot; #include "Visitor.h"
#include &lt;string&gt; #include &lt;string&gt;
#include &lt;iostream&gt; #include &lt;iostream&gt;
@ -734,29 +734,29 @@ class AuthorVisitor : public Visitor
string _author; string _author;
public: public:
AuthorVisitor(const string&amp; name ) : Visitor(), _author(name) {} ; AuthorVisitor(const string&amp; name ) : Visitor(), _author(name) {} ;
void visit() { cout &lt;&lt; &quot;AUTHOR: &quot; &lt;&lt; _author &lt;&lt; endl; exit(0); }; void visit() { cout &lt;&lt; "AUTHOR: " &lt;&lt; _author &lt;&lt; endl; exit(0); };
}; };
</pre><p> </pre><p>
Now include this class definition somewhere and go about creating Now include this class definition somewhere and go about creating
your command line. When you create the author switch, add the your command line. When you create the author switch, add the
<tt class="classname">AuthorVisitor</tt> pointer as follows: <code class="classname">AuthorVisitor</code> pointer as follows:
</p><pre class="programlisting"> </p><pre class="programlisting">
SwitchArg author(&quot;a&quot;,&quot;author&quot;,&quot;Prints author name&quot;, false, SwitchArg author("a","author","Prints author name", false,
new AuthorVisitor(&quot;Homer J. Simpson&quot;) ); new AuthorVisitor("Homer J. Simpson") );
cmd.add( author ); cmd.add( author );
</pre><p> </pre><p>
Now, any time the <i class="parameter"><tt>-a</tt></i> or Now, any time the <em class="parameter"><code>-a</code></em> or
<i class="parameter"><tt>--author</tt></i> flag is specified, <em class="parameter"><code>--author</code></em> flag is specified,
the program will print the author name, Homer J. Simpson and exit the program will print the author name, Homer J. Simpson and exit
without processing any further (as specified in the without processing any further (as specified in the
<tt class="methodname">visit()</tt> method). <code class="methodname">visit()</code> method).
</p></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="MORE_INFO"></a>More Information</h2></div></div><div></div></div><p> </p></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="MORE_INFO"></a>More Information</h2></div></div></div><p>
For more information, look at the <a href="html/index.html" target="_top"> For more information, look at the <a href="html/index.html" target="_top">
API Documentation</a> and the examples included with the API Documentation</a> and the examples included with the
distribution. distribution.

View File

@ -104,7 +104,7 @@ int main(int argc, char** argv)
// the need to call the cmd.add() method. All args have support in // the need to call the cmd.add() method. All args have support in
// their constructors to add themselves directly to the CmdLine object. // their constructors to add themselves directly to the CmdLine object.
// It doesn't matter which idiom you choose, they accomplish the same thing. // It doesn't matter which idiom you choose, they accomplish the same thing.
TCLAP::SwitchArg reverseSwitch("r","reverse","Print name backwards", false, cmd); TCLAP::SwitchArg reverseSwitch("r","reverse","Print name backwards", cmd, false);
// Parse the argv array. // Parse the argv array.
cmd.parse( argc, argv ); cmd.parse( argc, argv );
@ -764,7 +764,7 @@ is an example:
CmdLine cmd("this is a message", '=', "0.99" ); CmdLine cmd("this is a message", '=', "0.99" );
// Note that the following args take the "cmd" object as arguments. // Note that the following args take the "cmd" object as arguments.
SwitchArg btest("B","existTestB", "exist Test B", false, cmd ); SwitchArg btest("B","existTestB", "exist Test B", cmd, false );
ValueArg&lt;string&gt; stest("s", "stringTest", "string test", true, "homer", ValueArg&lt;string&gt; stest("s", "stringTest", "string test", true, "homer",
"string", cmd ); "string", cmd );