<!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>ucommon: shell.cpp</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <link href="navtree.css" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="navtree.js"></script> <script type="text/javascript" src="resize.js"></script> <script type="text/javascript"> $(document).ready(initResizable); </script> <link href="doxygen.css" rel="stylesheet" type="text/css"/> </head> <body> <!-- Generated by Doxygen 1.7.3 --> <div id="top"> <div id="titlearea"> <table cellspacing="0" cellpadding="0"> <tbody> <tr style="height: 56px;"> <td style="padding-left: 0.5em;"> <div id="projectname">ucommon</div> </td> </tr> </tbody> </table> </div> <div id="navrow1" class="tabs"> <ul class="tablist"> <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>Data Structures</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 id="side-nav" class="ui-resizable side-nav-resizable"> <div id="nav-tree"> <div id="nav-tree-contents"> </div> </div> <div id="splitbar" style="-moz-user-select:none;" class="ui-resizable-handle"> </div> </div> <script type="text/javascript"> initNavTree('a00007.html',''); </script> <div id="doc-content"> <div class="header"> <div class="headertitle"> <h1>shell.cpp</h1> </div> </div> <div class="contents"> <p>Example of shell parsing.</p> <div class="fragment"><pre class="fragment"><span class="comment">// Copyright (C) 2009-2010 David Sugar, Tycho Softworks.</span> <span class="comment">//</span> <span class="comment">// This file is part of GNU uCommon C++.</span> <span class="comment">//</span> <span class="comment">// GNU uCommon C++ is free software: you can redistribute it and/or modify</span> <span class="comment">// it under the terms of the GNU Lesser General Public License as published</span> <span class="comment">// by the Free Software Foundation, either version 3 of the License, or</span> <span class="comment">// (at your option) any later version.</span> <span class="comment">//</span> <span class="comment">// GNU uCommon C++ 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 Lesser General Public License for more details.</span> <span class="comment">//</span> <span class="comment">// You should have received a copy of the GNU Lesser General Public License</span> <span class="comment">// along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.</span> <span class="preprocessor">#ifndef DEBUG</span> <span class="preprocessor"></span><span class="preprocessor">#define DEBUG</span> <span class="preprocessor"></span><span class="preprocessor">#endif</span> <span class="preprocessor"></span> <span class="preprocessor">#include <<a class="code" href="a00202.html" title="Top level include file for the GNU uCommon C++ core library.">ucommon/ucommon.h</a>></span> <span class="preprocessor">#include <stdio.h></span> <span class="keyword">using namespace </span>UCOMMON_NAMESPACE; <span class="keyword">extern</span> <span class="stringliteral">"C"</span> <span class="keywordtype">int</span> main() { <span class="keywordtype">int</span> test_argc; <span class="keywordtype">char</span> *test_argv[6]; <a name="a0"></a><a class="code" href="a00130.html#a98a08e2f7c3869a437d4a5d79ff7820d" title="Bind application to text domain for internationalization.">shell::bind</a>(<span class="stringliteral">"test"</span>); test_argc = 5; test_argv[0] = (<span class="keywordtype">char</span> *)<span class="stringliteral">"test"</span>; test_argv[1] = (<span class="keywordtype">char</span> *)<span class="stringliteral">"--lines=5"</span>; test_argv[2] = (<span class="keywordtype">char</span> *)<span class="stringliteral">"-r"</span>; test_argv[3] = (<span class="keywordtype">char</span> *)<span class="stringliteral">"a"</span>; test_argv[4] = (<span class="keywordtype">char</span> *)<span class="stringliteral">"b"</span>; test_argv[5] = NULL; <a name="_a1"></a><a class="code" href="a00134.html" title="Flag option for shell parsing.">shell::flagopt</a> rflag(<span class="charliteral">'r'</span>, <span class="stringliteral">"--reverse"</span>, <span class="stringliteral">"reverse order of arguments"</span>); <a class="code" href="a00134.html" title="Flag option for shell parsing.">shell::flagopt</a> tflag(<span class="charliteral">'t'</span>, <span class="stringliteral">"--testing"</span>, <span class="stringliteral">"never hit this flag"</span>); <a name="_a2"></a><a class="code" href="a00137.html" title="Numeric option for shell parsing.">shell::numericopt</a> lines(<span class="charliteral">'l'</span>, <span class="stringliteral">"--lines"</span>, <span class="stringliteral">"number of lines in output"</span>); <a name="_a3"></a><a class="code" href="a00130.html" title="A utility class for generic shell operations.">shell</a> args(test_argc, test_argv); assert(!tflag); assert(<a name="a4"></a><a class="code" href="a00185.html#a8e91e3bc27122a2ea5315c17a40dc374" title="Convenience function to validate object assuming it is castable to bool.">is</a>(rflag)); assert(*lines == 5); assert(args() == 2); <span class="comment">// two file arguments left</span> assert(<a name="a5"></a><a class="code" href="a00197.html#a980d221b0273a9bf5d14a0f403495b40" title="Compare two socket addresses to see if equal.">eq</a>(args[0], <span class="stringliteral">"a"</span>)); <span class="comment">// first file argument is "a"</span> <a name="_a6"></a><a class="code" href="a00150.html" title="A copy-on-write string class that operates by reference count.">string_t</a> prefix, subdir, basedir; prefix = <a name="a7"></a><a class="code" href="a00130.html#a8700714dd59769acda4d74ba689567b2" title="Get a system path.">shell::path</a>(shell::SYSTEM_PREFIX); subdir = <a class="code" href="a00130.html#a8700714dd59769acda4d74ba689567b2" title="Get a system path.">shell::path</a>(shell::SYSTEM_PREFIX, <span class="stringliteral">"test"</span>); basedir = <a class="code" href="a00130.html#a8700714dd59769acda4d74ba689567b2" title="Get a system path.">shell::path</a>(shell::SYSTEM_PREFIX, <span class="stringliteral">"/test"</span>); prefix = prefix + <span class="stringliteral">"/test"</span>; assert(<a class="code" href="a00197.html#a980d221b0273a9bf5d14a0f403495b40" title="Compare two socket addresses to see if equal.">eq</a>(basedir, <span class="stringliteral">"/test"</span>)); assert(<a class="code" href="a00197.html#a980d221b0273a9bf5d14a0f403495b40" title="Compare two socket addresses to see if equal.">eq</a>(subdir, prefix)); } </pre></div> </div> </div> </div> <div id="nav-path" class="navpath"> <ul> <li class="footer">Generated on Wed Apr 13 2011 22:39:47 for ucommon by  <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.3 </li> </ul> </div> </body> </html>