<!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/xhtml;charset=UTF-8"/> <title>GNU CommonC++: cmdlineopt.cpp</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <link href="doxygen.css" rel="stylesheet" type="text/css"/> </head> <body> <!-- Generated by Doxygen 1.6.3 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Classes</span></a></li> <li><a href="files.html"><span>Files</span></a></li> <li><a href="examples.html"><span>Examples</span></a></li> </ul> </div> </div> <div class="contents"> <h1>cmdlineopt.cpp</h1><div class="fragment"><pre class="fragment"><span class="comment">// Copyright (C) 2001 Gianni Mariani</span> <span class="comment">//</span> <span class="comment">// This program is free software; you can redistribute it and/or modify</span> <span class="comment">// it under the terms of the GNU General Public License as published by</span> <span class="comment">// the Free Software Foundation; either version 2 of the License, or</span> <span class="comment">// (at your option) any later version.</span> <span class="comment">//</span> <span class="comment">// This program is distributed in the hope that it will be useful,</span> <span class="comment">// but WITHOUT ANY WARRANTY; without even the implied warranty of</span> <span class="comment">// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span> <span class="comment">// GNU General Public License for more details.</span> <span class="comment">//</span> <span class="comment">// You should have received a copy of the GNU General Public License</span> <span class="comment">// along with this program; if not, write to the Free Software</span> <span class="comment">// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.</span> <span class="comment">//</span> <span class="comment">// As a special exception to the GNU General Public License, permission is</span> <span class="comment">// granted for additional uses of the text contained in its release</span> <span class="comment">// of Common C++.</span> <span class="comment">//</span> <span class="comment">// The exception is that, if you link the Common C++ library with other</span> <span class="comment">// files to produce an executable, this does not by itself cause the</span> <span class="comment">// resulting executable to be covered by the GNU General Public License.</span> <span class="comment">// Your use of that executable is in no way restricted on account of</span> <span class="comment">// linking the Common C++ library code into it.</span> <span class="comment">//</span> <span class="comment">// This exception does not however invalidate any other reasons why</span> <span class="comment">// the executable file might be covered by the GNU General Public License.</span> <span class="comment">//</span> <span class="comment">// This exception applies only to the code released under the</span> <span class="comment">// name Common C++. If you copy code from other releases into a copy of</span> <span class="comment">// Common C++, as the General Public License permits, the exception does</span> <span class="comment">// not apply to the code that you add in this way. To avoid misleading</span> <span class="comment">// anyone as to the status of such modified files, you must delete</span> <span class="comment">// this exception notice from them.</span> <span class="comment">//</span> <span class="comment">// If you write modifications of your own for Common C++, it is your choice</span> <span class="comment">// whether to permit this exception to apply to your modifications.</span> <span class="comment">// If you do not wish that, delete this exception notice.</span> <span class="comment">//</span> <span class="comment">//</span> <span class="comment">// Example for Common C++ the command line parser interface.</span> <span class="comment">//</span> <span class="comment">//</span> <span class="comment">// This exmaple code shows how to use the command line parser provided by</span> <span class="comment">// CommonC++. The command line parser provides an interface which is</span> <span class="comment">// "object oriented" such that command line parameters are true "objects".</span> <span class="comment">//</span> <span class="comment">// Each command line option needs to be created. By defining "CommandOption"s</span> <span class="comment">// statically, the C++ constructor is called when the objects are loaded and</span> <span class="comment">// before the "main" function is called. The constructor links itself to</span> <span class="comment">// a list of other CommandOptionXXX in the list provided. If no</span> <span class="comment">// list is specified in the constructor, a default one is used. Because of</span> <span class="comment">// the undefined nature as to the order in which constructors are called,</span> <span class="comment">// no assumption as to the order in which the CommandOptionXXX constructors</span> <span class="comment">// are called should be made.</span> <span class="comment">//</span> <span class="comment">// CommandOptionXXX classes can be used to derive specialized parameter</span> <span class="comment">// classes that are specific to applications. The second example shows</span> <span class="comment">// just how this can be done.</span> <span class="comment">//</span> <span class="comment">//</span> <span class="comment">// Include the CommandOption definitions</span> <span class="comment">//</span> <span class="preprocessor">#include <<a class="code" href="common_8h.html" title="GNU Common C++ global header.">cc++/common.h</a>></span> <span class="preprocessor">#include <iostream></span> <span class="preprocessor">#ifndef WIN32</span> <span class="preprocessor"></span><span class="preprocessor">#include <cstdlib></span> <span class="preprocessor">#endif</span> <span class="preprocessor"></span> <span class="preprocessor">#ifdef CCXX_NAMESPACES</span> <span class="preprocessor"></span><span class="keyword">using namespace </span>std; <span class="keyword">using namespace </span>ost; <span class="preprocessor">#endif</span> <span class="preprocessor"></span> <span class="comment">//</span> <span class="comment">// The following definition of options all use the list header</span> <span class="comment">// defaultCommandOptionList (which is specified as the value of the</span> <span class="comment">// default parameter in the constructor. This convention would</span> <span class="comment">// allow other object files to link into the same list and add parameters</span> <span class="comment">// to the command line of this executable.</span> <a name="_a0"></a><a class="code" href="classost_1_1_command_option_arg.html" title="Class for options with an argument e.g.">CommandOptionArg</a> test_option1( <span class="stringliteral">"test_option1"</span>, <span class="stringliteral">"p"</span>, <span class="stringliteral">"This option takes an argument"</span>, <span class="keyword">true</span> ); <a name="_a1"></a><a class="code" href="classost_1_1_command_option_no_arg.html" title="CommandOption type for flags.">CommandOptionNoArg</a> test_noarg( <span class="stringliteral">"test_noarg"</span>, <span class="stringliteral">"b"</span>, <span class="stringliteral">"This option does not take an argument"</span> ); <a class="code" href="classost_1_1_command_option_no_arg.html" title="CommandOption type for flags.">CommandOptionNoArg</a> helparg( <span class="stringliteral">"help"</span>, <span class="stringliteral">"?"</span>, <span class="stringliteral">"Print help usage"</span> ); <a name="_a2"></a><a class="code" href="classost_1_1_command_option_collect.html" title="It only makes sense to have a single one of these set and it is also exclusive with...">CommandOptionCollect</a> restoargs( 0, 0, <span class="stringliteral">"Collect all the parameters"</span>, <span class="keyword">true</span> ); <span class="comment">//</span> <span class="comment">// Normally this would me the regular main(). In this example</span> <span class="comment">// this processes the first command option list.</span> <span class="comment">//</span> <span class="keywordtype">int</span> Example_main( <span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> ** argv ) { <span class="comment">// Create a CommandOptionParse object. This takes the</span> <span class="comment">// defaultCommandOptionList and parses the command line arguments.</span> <span class="comment">//</span> <a name="_a3"></a><a class="code" href="classost_1_1_command_option_parse.html" title="This is the CommandOptionParse interface class.">CommandOptionParse</a> * args = <a name="a4"></a><a class="code" href="namespaceost.html#a282d97c4c4323467d6e8b82cb93ec89d" title="makeCommandOptionParse will create an implementation of a CommandOptionParse object...">makeCommandOptionParse</a>( argc, argv, <span class="stringliteral">"CommonC++ command like option interface. This is example\n"</span> <span class="stringliteral">" code only."</span> ); <span class="comment">// If the user requested help then suppress all the usage error</span> <span class="comment">// messages.</span> <span class="keywordflow">if</span> ( helparg.numSet ) { cerr << args-><a name="a5"></a><a class="code" href="classost_1_1_command_option_parse.html#a7dc375ddeff083528d143a1ffdb67e16" title="Return a string that contains the usage description of this list of paramaters.">printUsage</a>(); ::exit(0); } <span class="comment">// Print usage your way.</span> <span class="keywordflow">if</span> ( args-><a name="a6"></a><a class="code" href="classost_1_1_command_option_parse.html#af8f0828a668ba000ea6ecc4de1d2c5b3" title="Get the value of the error flag set if the parser encountered errors.">argsHaveError</a>() ) { cerr << args-><a name="a7"></a><a class="code" href="classost_1_1_command_option_parse.html#af2176dfc5ec12e3b4fb9f39334f5173a" title="Return a string of text describing the list of errors encountered.">printErrors</a>(); cerr << args-><a class="code" href="classost_1_1_command_option_parse.html#a7dc375ddeff083528d143a1ffdb67e16" title="Return a string that contains the usage description of this list of paramaters.">printUsage</a>(); ::exit(1); } <span class="comment">// Go off and run any option specific task</span> args-><a name="a8"></a><a class="code" href="classost_1_1_command_option_parse.html#a060afd8cbb6e8395191cef1cb68520ea" title="The method should be invoked by the main code once it has determined that the application...">performTask</a>(); <span class="comment">// print all the -p options</span> <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i = 0; i < test_option1.numValue; i ++ ) { cerr << <span class="stringliteral">"test_option1 = "</span> << test_option1.values[ i ] << endl; } <span class="comment">// print all the other options.</span> <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i = 0; i < restoargs.numValue; i ++ ) { cerr << <span class="stringliteral">"restoargs "</span> << i << <span class="stringliteral">" : "</span> << restoargs.values[ i ] << endl; } <span class="keyword">delete</span> args; <span class="keywordflow">return</span> 0; } <span class="comment">//</span> <span class="comment">// This shows how to build a second option list. The example is similar to</span> <span class="comment">// the first as well as it shows how to derive a new command object.</span> <span class="comment">//</span> <a name="_a9"></a><a class="code" href="classost_1_1_command_option.html" title="CommandOption is the base class for all command line options.">CommandOption</a> * TestList = 0; <span class="keyword">extern</span> <a name="_a10"></a><a class="code" href="classost_1_1_command_option_rest.html" title="It only makes sense to have a single one of these set and it is exclusive with CommandOptionCollect...">CommandOptionRest</a> test_restoargs; <span class="preprocessor">#include <unistd.h></span> <span class="preprocessor">#include <sys/types.h></span> <span class="preprocessor">#include <sys/stat.h></span> <span class="preprocessor">#include <fcntl.h></span> <span class="preprocessor">#include <errno.h></span> <span class="preprocessor">#include <<a class="code" href="string_8h.html" title="Common C++ generic string class.">string.h</a>></span> <span class="preprocessor">#include <stdlib.h></span> <span class="preprocessor">#include <sys/wait.h></span> <span class="preprocessor">#include <strstream></span> <span class="comment">//</span> <span class="comment">// This is a parameter class derived from CommandOptionArg that takes</span> <span class="comment">// a file name parameter and detects wether the file is accessible</span> <span class="comment">// flagging an error if the file is inaccessible to read.</span> <span class="comment">//</span> <span class="keyword">class </span>file_option : <span class="keyword">public</span> <a class="code" href="classost_1_1_command_option_arg.html" title="Class for options with an argument e.g.">CommandOptionArg</a> { <span class="keyword">public</span>: <span class="comment">// the constructor calls the regular CommandOptionArg constructor</span> <span class="comment">// and all should be well.</span> file_option( <span class="keyword">const</span> <span class="keywordtype">char</span> * in_option_name, <span class="keyword">const</span> <span class="keywordtype">char</span> * in_option_letter, <span class="keyword">const</span> <span class="keywordtype">char</span> * in_description, <span class="keywordtype">bool</span> in_required = <span class="keyword">false</span>, <a class="code" href="classost_1_1_command_option.html" title="CommandOption is the base class for all command line options.">CommandOption</a> ** pp_next = & <a name="a11"></a><a class="code" href="namespaceost.html#a3b701311bbf7cf16f954820e316f5766" title="This defines a linked list head pointer for all the command line options that use...">defaultCommandOptionList</a> ) : <a class="code" href="classost_1_1_command_option_arg.html" title="Class for options with an argument e.g.">CommandOptionArg</a>( in_option_name, in_option_letter, in_description, in_required, pp_next ) { } <span class="comment">//</span> <span class="comment">// When parsing is done check if the file is accessible and register</span> <span class="comment">// an error with the CommandOptionParse object to let it know so.</span> <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a name="a12"></a><a class="code" href="classost_1_1_command_option.html#a1cc99afcc775ca21d16c38a0661839a2" title="Once parsing of command line options is complete, this method is called.">parseDone</a>( <a class="code" href="classost_1_1_command_option_parse.html" title="This is the CommandOptionParse interface class.">CommandOptionParse</a> * cop ) { <span class="keywordflow">if</span> ( numValue ) { <span class="keywordflow">if</span> ( ::access( values[ numValue - 1 ], R_OK ) ) { <span class="keywordtype">int</span> errno_s = errno; strstream msg; msg << <span class="stringliteral">"Error: "</span> << optionName << <span class="stringliteral">" '"</span> << values[ numValue - 1 ]; msg << <span class="stringliteral">"' : "</span> << ::strerror( errno_s ); cop-><a name="a13"></a><a class="code" href="classost_1_1_command_option_parse.html#a1ca17c61a4e4c8f40102cfafed61702a" title="Register an error with this parser.">registerError</a>( msg.str() ); } } } <span class="comment">//</span> <span class="comment">// Open said file. Do some operations on things - like open the file.</span> <span class="keywordtype">int</span> OpenFile() { <span class="comment">// Should put in way more error handling here ...</span> return ::open( values[ numValue - 1 ], O_RDONLY ); } <span class="comment">//</span> <span class="comment">// The most elaborate way to spit the contents of a file</span> <span class="comment">// to standard output.</span> pid_t pid; <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a name="a14"></a><a class="code" href="classost_1_1_command_option.html#af52fe62b7a0d3c40b726fa2447cb3b78" title="Once CommandOption objects have completed parsing and there are no errors they may...">performTask</a>( <a class="code" href="classost_1_1_command_option_parse.html" title="This is the CommandOptionParse interface class.">CommandOptionParse</a> * cop ) { pid = ::fork(); <span class="keywordflow">if</span> ( pid ) { <span class="keywordflow">return</span>; } <span class="keywordtype">int</span> fd = OpenFile(); <span class="keywordflow">if</span> ( fd < 0 ) { <span class="keywordtype">int</span> errno_s = errno; cerr << <span class="stringliteral">"Error: '"</span> << values[ numValue - 1 ] << <span class="stringliteral">"' : "</span> << ::strerror( errno_s ) ; ::exit( 1 ); } dup2(fd, 0); ::execvp( test_restoargs.<a name="a15"></a><a class="code" href="classost_1_1_command_option_with_arg.html#a5ccc2d9deb4a248d8414193385a1fc09" title="Array of list of values collected for this option.">values</a>[0], (<span class="keywordtype">char</span>**) test_restoargs.<a class="code" href="classost_1_1_command_option_with_arg.html#a5ccc2d9deb4a248d8414193385a1fc09" title="Array of list of values collected for this option.">values</a> ); ::exit(1); } ~file_option() { <span class="keywordflow">if</span> ( pid <= 0 ) <span class="keywordflow">return</span>; <span class="keywordtype">int</span> status;<a name="a16"></a><a class="code" href="namespaceost.html#a24c7774639b3768bdd45b65a296f6369"></a> <a class="code" href="namespaceost.html#a24c7774639b3768bdd45b65a296f6369"> ::wait</a>(&status); } }; <span class="comment">//</span> <span class="comment">// This is the linked list head for the options in the second example.</span> <span class="comment">// Note that the first example used the default value defined in the</span> <span class="comment">// method. Here it is explicitly specified as TestList in all the following</span> <span class="comment">// CommandOption constructors.</span> file_option test_file( <span class="stringliteral">"test_file"</span>, <span class="stringliteral">"f"</span>, <span class="stringliteral">"Filename to read from"</span>, <span class="keyword">true</span>, &TestList ); <a class="code" href="classost_1_1_command_option_no_arg.html" title="CommandOption type for flags.">CommandOptionNoArg</a> test_xnoarg( <span class="stringliteral">"test_xnoarg"</span>, <span class="stringliteral">"b"</span>, <span class="stringliteral">"This option does not take an argument"</span>, <span class="keyword">false</span>, &TestList ); <a class="code" href="classost_1_1_command_option_no_arg.html" title="CommandOption type for flags.">CommandOptionNoArg</a> test_helparg( <span class="stringliteral">"help"</span>, <span class="stringliteral">"?"</span>, <span class="stringliteral">"Print help usage"</span>, <span class="keyword">false</span>, &TestList ); <a class="code" href="classost_1_1_command_option_rest.html" title="It only makes sense to have a single one of these set and it is exclusive with CommandOptionCollect...">CommandOptionRest</a> test_restoargs( 0, 0, <span class="stringliteral">"Command to be executed"</span>, <span class="keyword">true</span>, &TestList ); <span class="comment">//</span> <span class="comment">// in most apps this would be the regular "main" function.</span> <span class="keywordtype">int</span> Test_main( <span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> ** argv ) { <a class="code" href="classost_1_1_command_option_parse.html" title="This is the CommandOptionParse interface class.">CommandOptionParse</a> * args = <a class="code" href="namespaceost.html#a282d97c4c4323467d6e8b82cb93ec89d" title="makeCommandOptionParse will create an implementation of a CommandOptionParse object...">makeCommandOptionParse</a>( argc, argv, <span class="stringliteral">"Command line parser X test.\n"</span> <span class="stringliteral">" This example is executed when the command ends in 'x'\n"</span> <span class="stringliteral">" It shows how the -f parameter can be specialized.\n"</span>, TestList ); <span class="comment">// If the user requested help then suppress all the usage error</span> <span class="comment">// messages.</span> <span class="keywordflow">if</span> ( test_helparg.numSet ) { cerr << args-><a class="code" href="classost_1_1_command_option_parse.html#a7dc375ddeff083528d143a1ffdb67e16" title="Return a string that contains the usage description of this list of paramaters.">printUsage</a>(); ::exit(0); } <span class="comment">// Print usage your way.</span> <span class="keywordflow">if</span> ( args-><a class="code" href="classost_1_1_command_option_parse.html#af8f0828a668ba000ea6ecc4de1d2c5b3" title="Get the value of the error flag set if the parser encountered errors.">argsHaveError</a>() ) { cerr << args-><a class="code" href="classost_1_1_command_option_parse.html#af2176dfc5ec12e3b4fb9f39334f5173a" title="Return a string of text describing the list of errors encountered.">printErrors</a>(); cerr << <span class="stringliteral">"Get help by --help\n"</span>; ::exit(1); } <span class="comment">// Go off and run any option specific task</span> args-><a class="code" href="classost_1_1_command_option_parse.html#a060afd8cbb6e8395191cef1cb68520ea" title="The method should be invoked by the main code once it has determined that the application...">performTask</a>(); <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i = 0; i < test_file.numValue; i ++ ) { cerr << <span class="stringliteral">"test_file = "</span> << test_file.values[ i ] << endl; } <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i = 0; i < test_restoargs.<a name="a17"></a><a class="code" href="classost_1_1_command_option_with_arg.html#a1fdb4121e6a385dd52e6599338019ce7" title="Number of values in the values array.">numValue</a>; i ++ ) { cerr << <span class="stringliteral">"test_restoargs "</span> << i << <span class="stringliteral">" : "</span> << test_restoargs.<a class="code" href="classost_1_1_command_option_with_arg.html#a5ccc2d9deb4a248d8414193385a1fc09" title="Array of list of values collected for this option.">values</a>[ i ] << endl; } <span class="keyword">delete</span> args; <span class="keywordflow">return</span> 0; } <span class="comment">//</span> <span class="comment">// This switches behaviour of this executable depending of wether it is</span> <span class="comment">// invoked with a command ending in "x". This is mimicking for example</span> <span class="comment">// the behaviour of bunzip2 and bzip2. These executables are THE SAME</span> <span class="comment">// file i.e.</span> <span class="comment">// 0 lrwxrwxrwx 1 root root 5 Oct 11 14:04 /usr/bin/bunzip2 -> bzip2*</span> <span class="comment">// and the behaviour is determined by the executable name.</span> <span class="comment">//</span> <span class="comment">// This example is way more complex than the way most people will end up</span> <span class="comment">// using feature.</span> <span class="keywordtype">int</span> main( <span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> ** argv ) { <span class="keywordtype">int</span> i = ::strlen( argv[ 0 ] ); <span class="comment">// determine which real "main" function do I call</span> <span class="keywordflow">if</span> ( argv[ 0 ][ i - 1 ] == <span class="charliteral">'x'</span> ) { <span class="keywordflow">return</span> Test_main( argc, argv ); } <span class="keywordflow">else</span> { <span class="keywordflow">return</span> Example_main( argc, argv ); } } </pre></div> </div> <hr class="footer"/><address style="text-align: right;"><small>Generated on Fri Mar 12 10:07:38 2010 for GNU CommonC++ by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.3 </small></address> </body> </html>