<html lang="en"> <head> <title>GNU Gengetopt 2.22.4</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="GNU Gengetopt 2.22.4"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="top" href="#Top"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <!-- This manual is for GNU Gengetopt (version 2.22.4, 23 December 2009), a tool to write command line option parsers for C programs. Copyright (C) 2001 - 2009 Free Software Foundation, Inc. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with the Front-Cover Texts being "A GNU Manual," and with the Back-Cover Texts as in (a) below. A copy of the license is included in the section entitled "GNU Free Documentation License." (a) The FSF's Back-Cover Text is: "You have freedom to copy and modify this GNU Manual, like GNU software. Copies published by the Free Software Foundation raise funds for GNU development." --> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <h1 class="settitle">GNU Gengetopt 2.22.4</h1> <div class="contents"> <h2>Table of Contents</h2> <ul> <li><a name="toc_Top" href="#Top">Gengetopt</a> <li><a name="toc_Audience" href="#Audience">Audience</a> <li><a name="toc_Copying" href="#Copying">Gengetopt Copying Conditions</a> <li><a name="toc_Installation" href="#Installation">1 Installation</a> <ul> <li><a href="#Installation">1.1 Download</a> <li><a href="#Installation">1.2 Anonymous Git Checkout</a> <li><a href="#Installation">1.3 What you need to build gengetopt</a> <li><a href="#Installation">1.4 Patching from a previous version</a> </li></ul> <li><a name="toc_Basic-Usage" href="#Basic-Usage">2 Basic Usage</a> <li><a name="toc_Invoking-gengetopt" href="#Invoking-gengetopt">3 Invoking gengetopt</a> <li><a name="toc_Terminology" href="#Terminology">4 Terminology</a> <li><a name="toc_Options-with-enumerated-values" href="#Options-with-enumerated-values">5 Options with enumerated values</a> <li><a name="toc_Group-options" href="#Group-options">6 Group options</a> <li><a name="toc_Mode-options" href="#Mode-options">7 Mode options</a> <li><a name="toc_Parser-function-additional-parameters" href="#Parser-function-additional-parameters">8 Parser function additional parameters</a> <li><a name="toc_Configuration-files" href="#Configuration-files">9 Configuration files</a> <ul> <li><a href="#Configuration-files">9.1 Further details on the configuration file parser</a> </li></ul> <li><a name="toc_Multiple-Options" href="#Multiple-Options">10 Multiple Options</a> <li><a name="toc_String-Parsers-and-Multiple-Parsers" href="#String-Parsers-and-Multiple-Parsers">11 String Parsers and Multiple Parsers</a> <li><a name="toc_No-getopt_005flong" href="#No-getopt_005flong">12 What if getopt_long is not available?</a> <ul> <li><a href="#Include-the-getopt_005flong-code-into-the-parser">12.1 Include the <code>getopt_long</code> code into the generated parser</a> <li><a href="#Use-automake_002fautoconf">12.2 Use automake/autoconf to check for the existence of <code>getopt_long</code></a> <ul> <li><a href="#Use-Gnulib">12.2.1 Use Gnulib</a> <li><a href="#Use-getopt_005flong-sources">12.2.2 Use getopt_long sources</a> </li></ul> </li></ul> <li><a name="toc_Bugs" href="#Bugs">13 Known Bugs and Limitations</a> <ul> <li><a href="#Getopt-and-subsequent-calls">13.1 Getopt and subsequent calls</a> </li></ul> <li><a name="toc_Mailing-Lists" href="#Mailing-Lists">14 Mailing Lists</a> <li><a name="toc_Index" href="#Index">Index</a> </li></ul> </div> <div class="node"> <a name="Top"></a> <p><hr> Next: <a rel="next" accesskey="n" href="#Audience">Audience</a>, Previous: <a rel="previous" accesskey="p" href="#dir">(dir)</a>, Up: <a rel="up" accesskey="u" href="#dir">(dir)</a> </div> <h2 class="unnumbered">Gengetopt</h2> <p>Gengetopt is a tool to write command line option parsing code for C programs. <p>This is Edition 2.22.4 of the Gengetopt manual. <p>This file documents GNU Gengetopt version 2.22.4. <p>This manual is for GNU Gengetopt (version 2.22.4, 23 December 2009), a tool to write command line option parsers for C programs. <p>Copyright © 2001 - 2009 Free Software Foundation, Inc. <blockquote> Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with the Front-Cover Texts being “A GNU Manual,” and with the Back-Cover Texts as in (a) below. A copy of the license is included in the section entitled “GNU Free Documentation License.” <p>(a) The FSF's Back-Cover Text is: “You have freedom to copy and modify this GNU Manual, like GNU software. Copies published by the Free Software Foundation raise funds for GNU development.” </blockquote> <!-- All the nodes can be updated using the EMACS command --> <!-- texinfo-every-node-update, which is normally bound to C-c C-u C-e. --> <!-- @node Top, Introduction, (dir), (dir) --> <!-- All the menus can be updated with the EMACS command --> <!-- texinfo-all-menus-update, which is normally bound to C-c C-u C-a. --> <ul class="menu"> <li><a accesskey="1" href="#Audience">Audience</a>: What is gengetopt and who it is for. <li><a accesskey="2" href="#Copying">Copying</a>: Your rights. <li><a accesskey="3" href="#Installation">Installation</a>: Download and Installation <li><a accesskey="4" href="#Basic-Usage">Basic Usage</a>: How to run Gengetopt; syntax of .ggo files <li><a accesskey="5" href="#Invoking-gengetopt">Invoking gengetopt</a>: gengetopt command line options <li><a accesskey="6" href="#Terminology">Terminology</a>: Definitions of options, values, and so forth. <li><a accesskey="7" href="#Options-with-enumerated-values">Options with enumerated values</a>: Options accepting only a list of values <li><a accesskey="8" href="#Group-options">Group options</a>: Groups of mutual exclusive options <li><a accesskey="9" href="#Mode-options">Mode options</a>: Mutual exclusive group of options <li><a href="#Parser-function-additional-parameters">Parser function additional parameters</a> <li><a href="#Configuration-files">Configuration files</a>: Parsing a configuration file <li><a href="#Multiple-Options">Multiple Options</a>: Multiple options <li><a href="#String-Parsers-and-Multiple-Parsers">String Parsers and Multiple Parsers</a>: Using multiple parsers in the same program <li><a href="#No-getopt_005flong">No getopt_long</a>: What if getopt_long is not available? <li><a href="#Bugs">Bugs</a>: What is lame and/or buggy in Gengetopt 2.22.4 <li><a href="#Mailing-Lists">Mailing Lists</a>: For discussions and announces <li><a href="#Index">Index</a>: Index. </ul> <div class="node"> <a name="Audience"></a> <p><hr> Next: <a rel="next" accesskey="n" href="#Copying">Copying</a>, Previous: <a rel="previous" accesskey="p" href="#Top">Top</a>, Up: <a rel="up" accesskey="u" href="#Top">Top</a> </div> <h2 class="unnumbered">Audience</h2> <p><a name="index-Audience-1"></a><a name="index-Who-should-use-Gengetopt-2"></a> Gengetopt is a tool to generate C code to parse the command line arguments <code>argc</code> and <code>argv</code> that are part of every C or C++ program. The generated code uses the C library function <code>getopt_long</code> to perform the actual command line parsing. <p>This manual is written for C and C++ programmers, specifically the <em>lazy</em> ones. If you've written any non-trivial C program, you've had to deal with argument parsing. It isn't particularly difficult, nor is it particularly exciting. It <em>is</em> one of the classic programming nuisances, which is why most books on programming leave it as an exercise for the reader. Gengetopt can save you from this work, leaving you free to focus on the interesting parts of your program. <p>Thus your program will be able to handle command line options such as: <pre class="example"> myprog --input foo.c -o foo.o --no-tabs -i 100 *.class </pre> <p>And both long options (those that start with <code>--</code>) and short options (start with <code>-</code> and consist of only one character) can be handled (see <a href="#Terminology">Terminology</a> for further details). For standards about short and long options you may want to take a look at the GNU Coding Standards (<a href="http://www.gnu.org/prep/standards_toc.html">http://www.gnu.org/prep/standards_toc.html</a>). <p>Gengetopt can also generate a function to save the command line options into a file (see <a href="#Basic-Usage">Basic Usage</a>), and a function to read the command line options from a file (see <a href="#Configuration-files">Configuration files</a>). Of course, these two kinds of files are compliant. <p>Generated code works also if you use GNU Autoconf and GNU Automake and it is documented with Doxygen comments. In particular, <a name="index-PACKAGE-3"></a><code>PACKAGE</code>, <a name="index-PACKAGE_005fNAME-4"></a><code>PACKAGE_NAME</code> and <a name="index-VERSION-5"></a><code>VERSION</code> are used in the generated code to print information. <div class="node"> <a name="Copying"></a> <p><hr> Next: <a rel="next" accesskey="n" href="#Installation">Installation</a>, Previous: <a rel="previous" accesskey="p" href="#Audience">Audience</a>, Up: <a rel="up" accesskey="u" href="#Top">Top</a> </div> <h2 class="unnumbered">Gengetopt Copying Conditions</h2> <p><a name="index-Copying-conditions-6"></a><a name="index-Conditions-for-copying-Gengetopt-7"></a> Gengetopt is free software; you are free to use, share and modify it under the terms of the GNU General Public License that accompanies this manual. <p>The code that Gengetopt generates is also free software; however it is licensed with a simple all-permissive license instead of the GPL or LGPL. You are free to do anything you like with the generated code, including incorporating it into or linking it with proprietary software. <p>Gengetopt was originally written by Roberto Arturo Tena Sanchez. It is currently maintained by Lorenzo Bettini <a href="http://www.lorenzobettini.it">http://www.lorenzobettini.it</a>. <p>A primordial version of <a href="#Terminology">Terminology</a> was written by Adam Greenblatt. <div class="node"> <a name="Installation"></a> <p><hr> Next: <a rel="next" accesskey="n" href="#Basic-Usage">Basic Usage</a>, Previous: <a rel="previous" accesskey="p" href="#Copying">Copying</a>, Up: <a rel="up" accesskey="u" href="#Top">Top</a> </div> <h2 class="chapter">1 Installation</h2> <p><a name="index-installation-8"></a><a name="index-download-9"></a> See the file <samp><span class="file">INSTALL</span></samp> for detailed building and installation instructions; anyway if you're used to compiling Linux software that comes with sources you may simply follow the usual procedure, i.e. untar the file you downloaded in a directory and then: <pre class="example"> cd <source code main directory> ./configure make make install </pre> <p>Note: unless you specify a different install directory by <code>--prefix</code> option of configure (e.g. <code>./configure --prefix=<your home></code>), you must be root to run <code>make install</code>. <p>Files will be installed in the following directories: <dl> <dt><code>executables</code><dd> <code>/prefix/bin</code> <br><dt><code>docs</code><dd> <code>/prefix/share/doc/gengetopt</code> <br><dt><code>examples</code><dd> <code>/prefix/share/doc/gengetopt/examples</code> <br><dt><code>additional files</code><dd> <code>/prefix/share/gengetopt</code> </dl> <p>Default value for prefix is <code>/usr/local</code> but you may change it with <code>--prefix</code> option to configure. <h3 class="section">1.1 Download</h3> <p><a name="index-download-10"></a> You can download it from GNU's ftp site: <a href="ftp://ftp.gnu.org/gnu/gengetopt">ftp://ftp.gnu.org/gnu/gengetopt</a> or from one of its mirrors (see <a href="http://www.gnu.org/prep/ftp.html">http://www.gnu.org/prep/ftp.html</a>). <p>I do not distribute Windows binaries anymore; since, they can be easily built by using Cygnus C/C++ compiler, available at <a href="http://www.cygwin.com">http://www.cygwin.com</a>. However, if you don't feel like downloading such compiler, you can request such binaries directly to me, by e-mail (find my e-mail at my home page) and I can send them to you. <p>Archives are digitally signed by me (Lorenzo Bettini) with GNU gpg (<a href="http://www.gnupg.org">http://www.gnupg.org</a>). My GPG public key can be found at my home page (<a href="http://www.lorenzobettini.it">http://www.lorenzobettini.it</a>). <p>You can also get the patches, if they are available for a particular release (see below for patching from a previous version). <h3 class="section">1.2 Anonymous Git Checkout</h3> <p><a name="index-Git-11"></a>This project's git repository can be checked out through the following clone instruction<a rel="footnote" href="#fn-1" name="fnd-1"><sup>1</sup></a>: <pre class="example"> git clone git://git.savannah.gnu.org/gengetopt.git </pre> <p>Further instructions can be found at the address: <p><a href="http://savannah.gnu.org/projects/gengetopt">http://savannah.gnu.org/projects/gengetopt</a>. <p>And the git repository can also browsed on-line at <p><a href="http://git.savannah.gnu.org/cgit/gengetopt.git">http://git.savannah.gnu.org/cgit/gengetopt.git</a>. <p>Please note that this way you will get the latest development sources of Gengetopt, which may also be unstable. This solution is the best if you intend to correct/extend this program: you should send me patches against the latest git repository sources. <p>If, on the contrary, you want to get the sources of a given release, through git, say, e.g., version X.Y.Z, you must specify the tag <code>rel_X_Y_Z</code>. <p>When you compile the sources that you get from the git repository, before running the <code>configure</code> and <code>make</code> commands, for the first time, you must run the command: <pre class="example"> autoreconf -i </pre> <p class="noindent">This will run the autotools commands in the correct order, and also copy possibly missing files. You should have installed recent versions of <a name="index-automake-12"></a><a name="index-autoconf-13"></a><a name="index-libtool-14"></a><code>automake</code>, <code>autoconf</code> and <code>libtool</code> in order for this to succeed. <h3 class="section">1.3 What you need to build gengetopt</h3> <p><a name="index-requirements-15"></a> Gengetopt has been developed under GNU/Linux, using gcc (C++), and bison (yacc) and flex (lex), and ported under Win32 with Cygnus C/C++compiler, available at <a href="http://www.cygwin.com">http://www.cygwin.com</a>. <p>For developing gengetopt, I use the excellent <a name="index-automake-16"></a><a name="index-autoconf-17"></a><a name="index-libtool-18"></a>GNU Autoconf<a rel="footnote" href="#fn-2" name="fnd-2"><sup>2</sup></a>, GNU Automake<a rel="footnote" href="#fn-3" name="fnd-3"><sup>3</sup></a> and GNU Libtool<a rel="footnote" href="#fn-4" name="fnd-4"><sup>4</sup></a>. <a name="index-gnulib-19"></a>Since version 2.19 I also started to use Gnulib - The GNU Portability Library<a rel="footnote" href="#fn-5" name="fnd-5"><sup>5</sup></a>, “a central location for common GNU code, intended to be shared among GNU packages” (for instance, I rely on Gnulib for checking for the presence and correctness of <code>getopt_long</code> function, <a href="#Use-Gnulib">Use Gnulib</a>). <p>Moreover <em>GNU Gengen</em> (<a href="http://www.gnu.org/software/gengen">http://www.gnu.org/software/gengen</a>) is used for automatically generating the code that generates the command line parser. <p>Actually, you don't need all these tools above to build gengetopt because I provide generated sources, unless you want to develop gengetopt. <p>The code generated by gengetopt relies on the <code>getopt_long</code> function that is usually in the standard C library; however, there may be some implementations of the C library that don't include it; we refer to <a href="#No-getopt_005flong">No getopt_long</a>, for instructions on how to check whether getopt_long is part of the library and how to deal with their lacking (using autoconf and automake). <h3 class="section">1.4 Patching from a previous version</h3> <p><a name="index-patching-20"></a> If you downloaded a patch, say <samp><span class="file">gengetopt-1.3-1.3.1-patch.gz</span></samp> (i.e., the patch to go from version 1.3 to version 1.3.1), cd to the directory with sources from the previous version (gengetopt-1.3) and type: <pre class="example"> gunzip -cd ../gengetopt-1.3-1.3.1.patch.gz | patch -p1 </pre> <p>and restart the compilation process (if you had already run configure a simple make should do). <div class="node"> <a name="Basic-Usage"></a> <p><hr> Next: <a rel="next" accesskey="n" href="#Invoking-gengetopt">Invoking gengetopt</a>, Previous: <a rel="previous" accesskey="p" href="#Installation">Installation</a>, Up: <a rel="up" accesskey="u" href="#Top">Top</a> </div> <h2 class="chapter">2 Basic Usage</h2> <p><a name="index-how-to-use-Gengetopt-21"></a><a name="index-getting-started-with-Gengetopt-22"></a><a name="index-tutorial-23"></a> The command line options, which have to be handled by gengetopt generated function, are specified in a file (typically with <samp><span class="file">.ggo</span></samp> extension). This file consists of sentences with the formats shown below (these sentences are allowed to span more than one line). Statements in {} are optional (the <code>option</code> sentences need not to be given in separate lines): <pre class="example"> package "<packname>" version "<version>" purpose "<purpose>" usage "<usage>" description "<description>" args "<command line options>" option <long> <short> "<desc>" {details="<detailed description>"} {argtype} {typestr="<type descr>"} {values="<value1>","<value2>",...} {default="<default value>"} {dependon="<other option>"} {required} {argoptional} {multiple} {hidden} option <long> <short> "<desc>" flag <on/off> section "section name" {sectiondesc="optional section description"} text "a textual sentence" </pre> <p class="noindent">Where: <dl> <dt><code>package</code><dd><a name="index-package-24"></a><a name="index-PACKAGE-25"></a><a name="index-PACKAGE_005fNAME-26"></a>This has the precedence over <code>PACKAGE</code> and <code>PACKAGE_NAME</code> generated by autoconf. This is required, unless you use autoconf. <p>If <code>package</code> is specified, then it will be used to print the program name in the output of <code>--help</code> and <code>--version</code>, and also when printing errors (from within the generated parser). If it is not specified, then <code>PACKAGE</code> will be used when printing errors, and <code>PACKAGE_NAME</code> in the output of <code>--help</code> and <code>--version</code>. Note that if <code>PACKAGE_NAME</code> is empty, then <code>PACKAGE</code> will be used also in this case. <br><dt><code>version</code><dd><a name="index-version-27"></a><a name="index-VERSION-28"></a> This has the precedence over <code>VERSION</code> generated by autoconf. This is required, unless you use autoconf. <br><dt><code>purpose</code><dd><a name="index-purpose-29"></a> What the program does (even on more than one line), it will be printed with the help, before the usage string. Optional. <br><dt><code>usage</code><dd><a name="index-usage-30"></a> The “Usage” string that will be printed with the help<a rel="footnote" href="#fn-6" name="fnd-6"><sup>6</sup></a>. If not specified, it will be generated automatically. Optional. <br><dt><code>description</code><dd><a name="index-description-31"></a> The “Description” string that will be printed with the help<a rel="footnote" href="#fn-7" name="fnd-7"><sup>7</sup></a>, after the usage string. Optional. <br><dt><code>args</code><dd><a name="index-args-32"></a> With <code>args</code><a rel="footnote" href="#fn-8" name="fnd-8"><sup>8</sup></a> you can specify options that will be added to the command line options of gengetopt itself. For instance, if you always run gengetopt on your input file with the options <code>--no-handle-error --string-parser -u</code>, you can add these options in the input file like this: <pre class="example"> args "--no-handle-error --string-parser -u" </pre> <p class="noindent">and remove those recurrent options from the command line. Optional. <br><dt><code>long</code><dd><a name="index-long-33"></a> The long option, a double quoted string with upper and lower case characters, digits, a dash (<code>-</code>) and a dot (<code>.</code>). No spaces allowed. The name of the variables generated to store arguments (see later in this section) are long options converted to be legal C variable names. This means that <code>.</code> and <code>-</code> are both replaced by <code>_</code>. <p><a name="index-short-34"></a><br><dt><code>short</code><dd> The short option, a single upper or lower case char, or a digit. If a <code>-</code> is specified, then no short option is considered for the long option (thus long options with no associated short options are allowed). Since version 2.22 you can also specify <code>?</code> as the short option. <p><a name="index-desc-35"></a><br><dt><code>desc</code><dd> <a name="index-wrapping-36"></a>This description will be printed during the output of <code>--help</code>. Wrapping will be automatically performed. <br><dt><code>details</code><dd><a name="index-details-37"></a><a name="index-g_t_0040code_007b_002d_002ddetailed_002dhelp_007d-38"></a> This is an extended description string for the option that will be printed (after the option) during the output of <code>--detailed-help</code><a rel="footnote" href="#fn-9" name="fnd-9"><sup>9</sup></a>, which will be automatically generated. Thus, these further details will NOT be printed with <code>--help</code>. Wrapping will be automatically performed. Optional. <p><a name="index-argtype-39"></a><br><dt><code>argtype</code><dd> <code>string</code>, <code>int</code>, <code>short</code>, <code>long</code>, <code>float</code>, <code>double</code>, <code>longdouble</code> or <code>longlong</code>. If the option is an enumerated one (see <a href="#Options-with-enumerated-values">Options with enumerated values</a>) the type can also be <code>enum</code>. If no type is specified the option does not accept an argument. <p><a name="index-typestr-40"></a><br><dt><code>typestr</code><dd> a description for the type of the values for the option. This will be used during the output of <code>--help</code> (e.g., <code>"filename"</code> instead of simply <code>STRING</code>, or <code>"portnumber"</code> instead of simply <code>INT</code>). <p><a name="index-values-41"></a><br><dt><code>values</code><dd> a list of strings containing all the possible values that can be passed to the option. If not specified, the type is considered string<a rel="footnote" href="#fn-10" name="fnd-10"><sup>10</sup></a>. For this kind of options, the type can also be <code>enum</code>. More on this feature can be found in <a href="#Options-with-enumerated-values">Options with enumerated values</a>. <p><a name="index-default-42"></a><br><dt><code>default</code><dd> an optional default value for the option. The value must always be specified as a double quoted string. It is possible to combine this with the <code>multiple</code> flag (<a href="#Multiple-Options">Multiple Options</a>) but only by giving a single default value. It is not possible to specify a list of default values. <p><a name="index-dependon-43"></a><br><dt><code>dependon</code><dd> this option depends on another option (whose long name description is specified). If this option is given at command line but not the option it depends on, an error will be generated. <p><a name="index-required-44"></a><a name="index-optional-45"></a><a name="index-g_t_0040code_007b_002d_002ddefault_002doptional_007d-46"></a><br><dt><code>required</code><dd> <code>required</code> or <code>optional</code>. This specifies whether such option must be given at each program invocation. These keywords were introduced in release 2.17. Before, you had to use the keywords <code>yes</code> or <code>no</code>. You can still use these keywords but their use is not advised since they are not much explicative. <p>If not specified, an option is considered mandatory; if you do not want this behavior, you can require that by default options are considered optional, by using the command line option <code>--default-optional</code><a rel="footnote" href="#fn-11" name="fnd-11"><sup>11</sup></a>. <p><a name="index-argoptional-47"></a><br><dt><code>argoptional</code><dd> If this flag is specified then this option has an argument that is optional. In this case, when you specify the argument at command line, please use <code>=</code> in case you use a long option, and avoid spaces if you use short option. For instance, if the option with optional argument is <code>-B|--bar</code>, use the following command line syntax: <code>-B15</code> or <code>--bar=15</code>, and NOT the following one <code>-B 15</code> nor <code>--bar 15</code>. <p>By using this specification together with <code>default</code> you can obtain an option that even if not explicitly specified will have the default value, and if it is specified without an argument it will have, again, the default value. <p><a name="index-multiple-48"></a><br><dt><code>multiple</code><dd> If this flag is specified then this option can be specified more than once at command line; all the values for this option are stored in an array. You can also specify the number of occurrences that a multiple option must be specified. Notice that this is independent from the <code>required</code> flag. See <a href="#Multiple-Options">Multiple Options</a>. <p><a name="index-hidden-49"></a><a name="index-details-50"></a><a name="index-g_t_0040code_007b_002d_002dfull_002dhelp_007d-51"></a><a name="index-g_t_0040code_007b_002d_002ddetailed_002dhelp_007d-52"></a><br><dt><code>hidden</code><dd> <a name="hidden"></a><a name="fullhelp"></a>If an option is “hidden” it will not appear in the output of <code>--help</code> but it can still be specified at command line<a rel="footnote" href="#fn-12" name="fnd-12"><sup>12</sup></a>. In case hidden options are used, the command line option <code>--full-help</code> will also be generated. This will print also the hidden options<a rel="footnote" href="#fn-13" name="fnd-13"><sup>13</sup></a>. If there's an option with <code>details</code>, then also <code>--detailed-help</code><a rel="footnote" href="#fn-14" name="fnd-14"><sup>14</sup></a> will be automatically generated; if one then specifies <code>--detailed-help</code> also hidden options will be printed (besides the details for options). <p><a name="index-on_002foff-53"></a><br><dt><code>on/off</code><dd> <code>on</code> or <code>off</code>. This is the state of the flag when the program starts. If user specifies the option, the flag toggles. </dl> <p>For strings (delimited by <code>"</code>) the following convention is adopted<a rel="footnote" href="#fn-15" name="fnd-15"><sup>15</sup></a>: a string spanning more than one line will be interpreted with the corresponding line breaks; if the line break is not desired one can use the backslash <code>\</code> to break the line without inserting a line break. A line break in a string can also be inserted with the string <code>\n</code>. Here are some examples: <pre class="example"> "This string will be interpreted into two lines exactly as it is" "This string is specified with two lines \ but interpreted as specified in only one line \ i.e., without explicit line break" "This string\nwill have a line break" </pre> <p class="noindent">Moreover, if the character <code>"</code> must be specified in the string, it will have to be escaped with the backslash<a rel="footnote" href="#fn-16" name="fnd-16"><sup>16</sup></a>: <pre class="example"> "This string contains \"a quoted string\" inside" </pre> <p>The part that must be provided in the correct order is <pre class="example"> option <long> <short> "<desc>" </pre> <p class="noindent">while the other specifications can be given in any order<a rel="footnote" href="#fn-17" name="fnd-17"><sup>17</sup></a>. Thus, for instance <pre class="example"> option <long> <short> "<desc>" {argtype} {typestr="<type descr>"} </pre> <p class="noindent">is the same as <pre class="example"> option <long> <short> "<desc>" {typestr="<type descr>"} {argtype} </pre> <p>Comments begin with <code>#</code> in any place (but in strings) of the line and ends in the end of line. <p><a name="index-g_t_0040code_007b_002dh_002c_002d_002dhelp_007d-54"></a><a name="index-g_t_0040code_007b_002dV_002c_002d_002dversion_007d-55"></a><a name="addedoptions"></a>Notice that the options <code>-h,--help</code> and <code>-V,--version</code> are added automatically; however, if you specify an option yourself that has <code>h</code> as short form, then only <code>--help</code> is added<a rel="footnote" href="#fn-18" name="fnd-18"><sup>18</sup></a>. The same holds for <code>-V,--version</code>. <p><a name="index-g_t_0040code_007b_002d_002dfull_002dhelp_007d-56"></a>In case hidden options are used, See <a href="#hidden">Hidden options</a>, the command line option <code>--full-help</code> will also be generated. This will print also the hidden options<a rel="footnote" href="#fn-19" name="fnd-19"><sup>19</sup></a>. <p><a name="index-g_t_0040code_007b_002d_002ddetailed_002dhelp_007d-57"></a>If there's at least one option with <code>details</code>, the command line option <code>--detailed-help</code> will also be generated. This will print also the details for options and hidden options<a rel="footnote" href="#fn-20" name="fnd-20"><sup>20</sup></a>. <p><a name="index-section-58"></a>Options can be part of sections, that provide a more meaningful descriptions of the options. A <em>section</em> can be defined with the following syntax (the <code>sectiondesc</code> is optional) and all the options following a section declaration are considered part of that sections: <pre class="example"> section "section name" {sectiondesc="optional section description"} </pre> <p class="noindent">Notice that the separation in sections is stronger than separation in groups of mutual exclusive options (see <a href="#Group-options">Group options</a>). Furthermore, sections should not be inserted among group options (but only externally). A section makes sense only if it is followed by some options. If you don't specify any option after a section, that section will not be printed at all. If you need to simply insert some text in the output of <code>--help</code>, then you must use <code>text</code>, explained in the next paragraph. <p><a name="index-text-59"></a>You can insert, among options, a textual string that will be printed in the output of <code>--help</code><a rel="footnote" href="#fn-21" name="fnd-21"><sup>21</sup></a>: <pre class="example"> text "\nA text description with possible line\nbreaks" </pre> <p class="noindent">Of course, you can use this mechanism even to manually insert blank lines among options with an empty text string: <pre class="example"> text "" </pre> <p>You can also specify the list of <code>values</code> that can be passed to an option (if the type is not specified, the option has type <code>string</code>). More on this feature can be found in <a href="#Options-with-enumerated-values">Options with enumerated values</a>. If a value that is not in the list is passed, an error is raised. You can think of such options as <em>enumerated</em> options. It is not necessary to pass the complete value at the command line option: a non ambiguous prefix will do. For instance, if the accepted values are <code>"foo","bar","foobar"</code>, then you can pass at the command line the value <code>"b"</code> and the value <code>"bar"</code> will be selected, or the value <code>"foob"</code> and the value <code>"foobar"</code> will be selected; instead, passing the value <code>"fo"</code> will raise an ambiguity error. <p>Here's an example of such a file (the file is called <samp><span class="file">sample1.ggo</span></samp>) <!-- Generator: GNU source-highlight, by Lorenzo Bettini, http://www.gnu.org/software/src-highlite --> <pre class="example"> # Name of your program package "sample1" # don't use package if you're using automake # Version of your program version "2.0" # don't use version if you're using automake # Options option "str-opt" s "A string option, for a filename" string typestr="filename" optional text "\nA brief text description" text " before the other options.\n" option "my-opt" m "Another integer option, \ this time the description of the option should be \"quite\" long to \ require wrapping... possibly more than one wrapping :-) \ especially if I\nrequire a line break" int optional option "int-opt" i "A int option" int yes section "more involved options" sectiondesc="the following options\nare more complex" text "" option "flag-opt" - "A flag option" flag off option "funct-opt" F "A function option" optional details="\nA function option is basically an option with no argument.\ It can be used, e.g., to specify a specific behavior for a program. Well, this further explanation is quite useless, but it's only to \ show an example of an option with details, \ which will be printed only when --detailed-help is given \ at the command line." section "last option section" option "long-opt" - "A long option" long optional option "def-opt" - "A string option with default" string default="Hello" optional option "enum-opt" - "A string option with list of values" values="foo","bar","hello","bye" default="hello" optional option "secret" S "hidden option will not appear in --help" int optional hidden option "dependant" D "option that depends on str-opt" int optional dependon="str-opt" text "\nAn ending text." </pre> <p>The simplest way to use gengetopt is to pass this file as the standard input, i.e.: <pre class="example"> gengetopt < sample1.ggo </pre> <p>By default gengetopt generates <samp><span class="file">cmdline.h</span></samp> and <samp><span class="file">cmdline.c</span></samp>. Otherwise we can specify these names with a command line option: <pre class="example"> gengetopt < sample1.ggo --file-name=cmdline1 --unamed-opts </pre> <p><a name="index-parameter-60"></a><a name="index-option-without-name-61"></a>The option <code>--unamed-opts</code> allows the generated command line parser to accept also names, without an option (for instance you can pass a file name without an option in front of it, and also use wildcards, such as <code>*.c</code>, <code>foo*.?</code> and so on). These are also called <em>parameters</em> (see <a href="#Terminology">Terminology</a>). You can specify an optional description for these additional names (default is <code>FILES</code>). <p>In <samp><span class="file">cmdline1.h</span></samp> you'll find the generated C struct: <!-- Generator: GNU source-highlight, by Lorenzo Bettini, http://www.gnu.org/software/src-highlite --> <pre class="example"> <i>/** </i><b>@file</b><i> cmdline1.h</i> <i> * </i><b>@brief</b><i> The header file for the command line option parser</i> <i> * generated by GNU Gengetopt version 2.22.3</i> <i> * </i><tt>http://www.gnu.org/software/gengetopt.</tt> <i> * DO NOT modify this file, since it can be overwritten</i> <i> * </i><b>@author</b><i> GNU Gengetopt by Lorenzo Bettini */</i> <b>#ifndef</b> CMDLINE1_H <b>#define</b> CMDLINE1_H <i>/* If we use autoconf. */</i> <b>#ifdef</b> HAVE_CONFIG_H <b>#include</b> <tt>"config.h"</tt> <b>#endif</b> <b>#include</b> <tt><stdio.h></tt> <i>/* for FILE */</i> <b>#ifdef</b> __cplusplus <b>extern</b> <tt>"C"</tt> { <b>#endif</b> <i>/* __cplusplus */</i> <b>#ifndef</b> CMDLINE_PARSER_PACKAGE <i>/** </i><b>@brief</b><i> the program name (used for printing errors) */</i> <b>#define</b> CMDLINE_PARSER_PACKAGE <tt>"sample1"</tt> <b>#endif</b> <b>#ifndef</b> CMDLINE_PARSER_PACKAGE_NAME <i>/** </i><b>@brief</b><i> the complete program name (used for help and version) */</i> <b>#define</b> CMDLINE_PARSER_PACKAGE_NAME <tt>"sample1"</tt> <b>#endif</b> <b>#ifndef</b> CMDLINE_PARSER_VERSION <i>/** </i><b>@brief</b><i> the program version */</i> <b>#define</b> CMDLINE_PARSER_VERSION <tt>"2.0"</tt> <b>#endif</b> <i>/** </i><b>@brief</b><i> Where the command line options are stored */</i> <b>struct</b> gengetopt_args_info { <b>const</b> <b>char</b> *help_help; <i>/**< </i><b>@brief</b><i> Print help and exit help description. */</i> <b>const</b> <b>char</b> *detailed_help_help; <i>/**< </i><b>@brief</b><i> Print help, including all details and hidden options, and exit help description. */</i> <b>const</b> <b>char</b> *full_help_help; <i>/**< </i><b>@brief</b><i> Print help, including hidden options, and exit help description. */</i> <b>const</b> <b>char</b> *version_help; <i>/**< </i><b>@brief</b><i> Print version and exit help description. */</i> <b>char</b> * str_opt_arg; <i>/**< </i><b>@brief</b><i> A string option, for a filename. */</i> <b>char</b> * str_opt_orig; <i>/**< </i><b>@brief</b><i> A string option, for a filename original value given at command line. */</i> <b>const</b> <b>char</b> *str_opt_help; <i>/**< </i><b>@brief</b><i> A string option, for a filename help description. */</i> <b>int</b> my_opt_arg; <i>/**< </i><b>@brief</b><i> Another integer option, this time the description of the option should be \"quite\" long to require wrapping... possibly more than one wrapping :-) especially if I\nrequire a line break. */</i> <b>char</b> * my_opt_orig; <i>/**< </i><b>@brief</b><i> Another integer option, this time the description of the option should be \"quite\" long to require wrapping... possibly more than one wrapping :-) especially if I\nrequire a line break original value given at command line. */</i> <b>const</b> <b>char</b> *my_opt_help; <i>/**< </i><b>@brief</b><i> Another integer option, this time the description of the option should be \"quite\" long to require wrapping... possibly more than one wrapping :-) especially if I\nrequire a line break help description. */</i> <b>int</b> int_opt_arg; <i>/**< </i><b>@brief</b><i> A int option. */</i> <b>char</b> * int_opt_orig; <i>/**< </i><b>@brief</b><i> A int option original value given at command line. */</i> <b>const</b> <b>char</b> *int_opt_help; <i>/**< </i><b>@brief</b><i> A int option help description. */</i> <b>int</b> flag_opt_flag; <i>/**< </i><b>@brief</b><i> A flag option (default=off). */</i> <b>const</b> <b>char</b> *flag_opt_help; <i>/**< </i><b>@brief</b><i> A flag option help description. */</i> <b>const</b> <b>char</b> *funct_opt_help; <i>/**< </i><b>@brief</b><i> A function option help description. */</i> <b>long</b> long_opt_arg; <i>/**< </i><b>@brief</b><i> A long option. */</i> <b>char</b> * long_opt_orig; <i>/**< </i><b>@brief</b><i> A long option original value given at command line. */</i> <b>const</b> <b>char</b> *long_opt_help; <i>/**< </i><b>@brief</b><i> A long option help description. */</i> <b>char</b> * def_opt_arg; <i>/**< </i><b>@brief</b><i> A string option with default (default='Hello'). */</i> <b>char</b> * def_opt_orig; <i>/**< </i><b>@brief</b><i> A string option with default original value given at command line. */</i> <b>const</b> <b>char</b> *def_opt_help; <i>/**< </i><b>@brief</b><i> A string option with default help description. */</i> <b>char</b> * enum_opt_arg; <i>/**< </i><b>@brief</b><i> A string option with list of values (default='hello'). */</i> <b>char</b> * enum_opt_orig; <i>/**< </i><b>@brief</b><i> A string option with list of values original value given at command line. */</i> <b>const</b> <b>char</b> *enum_opt_help; <i>/**< </i><b>@brief</b><i> A string option with list of values help description. */</i> <b>int</b> secret_arg; <i>/**< </i><b>@brief</b><i> hidden option will not appear in --help. */</i> <b>char</b> * secret_orig; <i>/**< </i><b>@brief</b><i> hidden option will not appear in --help original value given at command line. */</i> <b>const</b> <b>char</b> *secret_help; <i>/**< </i><b>@brief</b><i> hidden option will not appear in --help help description. */</i> <b>int</b> dependant_arg; <i>/**< </i><b>@brief</b><i> option that depends on str-opt. */</i> <b>char</b> * dependant_orig; <i>/**< </i><b>@brief</b><i> option that depends on str-opt original value given at command line. */</i> <b>const</b> <b>char</b> *dependant_help; <i>/**< </i><b>@brief</b><i> option that depends on str-opt help description. */</i> <b>unsigned</b> <b>int</b> help_given ; <i>/**< </i><b>@brief</b><i> Whether help was given. */</i> <b>unsigned</b> <b>int</b> detailed_help_given ; <i>/**< </i><b>@brief</b><i> Whether detailed-help was given. */</i> <b>unsigned</b> <b>int</b> full_help_given ; <i>/**< </i><b>@brief</b><i> Whether full-help was given. */</i> <b>unsigned</b> <b>int</b> version_given ; <i>/**< </i><b>@brief</b><i> Whether version was given. */</i> <b>unsigned</b> <b>int</b> str_opt_given ; <i>/**< </i><b>@brief</b><i> Whether str-opt was given. */</i> <b>unsigned</b> <b>int</b> my_opt_given ; <i>/**< </i><b>@brief</b><i> Whether my-opt was given. */</i> <b>unsigned</b> <b>int</b> int_opt_given ; <i>/**< </i><b>@brief</b><i> Whether int-opt was given. */</i> <b>unsigned</b> <b>int</b> flag_opt_given ; <i>/**< </i><b>@brief</b><i> Whether flag-opt was given. */</i> <b>unsigned</b> <b>int</b> funct_opt_given ; <i>/**< </i><b>@brief</b><i> Whether funct-opt was given. */</i> <b>unsigned</b> <b>int</b> long_opt_given ; <i>/**< </i><b>@brief</b><i> Whether long-opt was given. */</i> <b>unsigned</b> <b>int</b> def_opt_given ; <i>/**< </i><b>@brief</b><i> Whether def-opt was given. */</i> <b>unsigned</b> <b>int</b> enum_opt_given ; <i>/**< </i><b>@brief</b><i> Whether enum-opt was given. */</i> <b>unsigned</b> <b>int</b> secret_given ; <i>/**< </i><b>@brief</b><i> Whether secret was given. */</i> <b>unsigned</b> <b>int</b> dependant_given ; <i>/**< </i><b>@brief</b><i> Whether dependant was given. */</i> <b>char</b> **inputs ; <i>/**< </i><b>@brief</b><i> unamed options (options without names) */</i> <b>unsigned</b> inputs_num ; <i>/**< </i><b>@brief</b><i> unamed options number */</i> } ; <i>/** </i><b>@brief</b><i> The additional parameters to pass to parser functions */</i> <b>struct</b> cmdline_parser_params { <b>int</b> override; <i>/**< </i><b>@brief</b><i> whether to override possibly already present options (default 0) */</i> <b>int</b> initialize; <i>/**< </i><b>@brief</b><i> whether to initialize the option structure gengetopt_args_info (default 1) */</i> <b>int</b> check_required; <i>/**< </i><b>@brief</b><i> whether to check that all required options were provided (default 1) */</i> <b>int</b> check_ambiguity; <i>/**< </i><b>@brief</b><i> whether to check for options already specified in the option structure gengetopt_args_info (default 0) */</i> <b>int</b> print_errors; <i>/**< </i><b>@brief</b><i> whether getopt_long should print an error message for a bad option (default 1) */</i> } ; <i>/** </i><b>@brief</b><i> the purpose string of the program */</i> <b>extern</b> <b>const</b> <b>char</b> *gengetopt_args_info_purpose; <i>/** </i><b>@brief</b><i> the usage string of the program */</i> <b>extern</b> <b>const</b> <b>char</b> *gengetopt_args_info_usage; <i>/** </i><b>@brief</b><i> all the lines making the help output */</i> <b>extern</b> <b>const</b> <b>char</b> *gengetopt_args_info_help[]; <i>/** </i><b>@brief</b><i> all the lines making the full help output (including hidden options) */</i> <b>extern</b> <b>const</b> <b>char</b> *gengetopt_args_info_full_help[]; <i>/** </i><b>@brief</b><i> all the lines making the detailed help output (including hidden options and details) */</i> <b>extern</b> <b>const</b> <b>char</b> *gengetopt_args_info_detailed_help[]; <i>/**</i> <i> * The command line parser</i> <i> * </i><b>@param</b><i> argc the number of command line options</i> <i> * </i><b>@param</b><i> argv the command line options</i> <i> * </i><b>@param</b><i> args_info the structure where option information will be stored</i> <i> * </i><b>@return</b><i> 0 if everything went fine, NON 0 if an error took place</i> <i> */</i> <b>int</b> cmdline_parser (<b>int</b> argc, <b>char</b> **argv, <b>struct</b> gengetopt_args_info *args_info); <i>/**</i> <i> * The command line parser (version with additional parameters - deprecated)</i> <i> * </i><b>@param</b><i> argc the number of command line options</i> <i> * </i><b>@param</b><i> argv the command line options</i> <i> * </i><b>@param</b><i> args_info the structure where option information will be stored</i> <i> * </i><b>@param</b><i> override whether to override possibly already present options</i> <i> * </i><b>@param</b><i> initialize whether to initialize the option structure my_args_info</i> <i> * </i><b>@param</b><i> check_required whether to check that all required options were provided</i> <i> * </i><b>@return</b><i> 0 if everything went fine, NON 0 if an error took place</i> <i> * </i><b>@deprecated</b><i> use cmdline_parser_ext() instead</i> <i> */</i> <b>int</b> cmdline_parser2 (<b>int</b> argc, <b>char</b> **argv, <b>struct</b> gengetopt_args_info *args_info, <b>int</b> override, <b>int</b> initialize, <b>int</b> check_required); <i>/**</i> <i> * The command line parser (version with additional parameters)</i> <i> * </i><b>@param</b><i> argc the number of command line options</i> <i> * </i><b>@param</b><i> argv the command line options</i> <i> * </i><b>@param</b><i> args_info the structure where option information will be stored</i> <i> * </i><b>@param</b><i> params additional parameters for the parser</i> <i> * </i><b>@return</b><i> 0 if everything went fine, NON 0 if an error took place</i> <i> */</i> <b>int</b> cmdline_parser_ext (<b>int</b> argc, <b>char</b> **argv, <b>struct</b> gengetopt_args_info *args_info, <b>struct</b> cmdline_parser_params *params); <i>/**</i> <i> * Save the contents of the option struct into an already open FILE stream.</i> <i> * </i><b>@param</b><i> outfile the stream where to dump options</i> <i> * </i><b>@param</b><i> args_info the option struct to dump</i> <i> * </i><b>@return</b><i> 0 if everything went fine, NON 0 if an error took place</i> <i> */</i> <b>int</b> cmdline_parser_dump(FILE *outfile, <b>struct</b> gengetopt_args_info *args_info); <i>/**</i> <i> * Save the contents of the option struct into a (text) file.</i> <i> * This file can be read by the config file parser (if generated by gengetopt)</i> <i> * </i><b>@param</b><i> filename the file where to save</i> <i> * </i><b>@param</b><i> args_info the option struct to save</i> <i> * </i><b>@return</b><i> 0 if everything went fine, NON 0 if an error took place</i> <i> */</i> <b>int</b> cmdline_parser_file_save(<b>const</b> <b>char</b> *filename, <b>struct</b> gengetopt_args_info *args_info); <i>/**</i> <i> * Print the help</i> <i> */</i> <b>void</b> cmdline_parser_print_help(<b>void</b>); <i>/**</i> <i> * Print the full help (including hidden options)</i> <i> */</i> <b>void</b> cmdline_parser_print_full_help(<b>void</b>); <i>/**</i> <i> * Print the detailed help (including hidden options and details)</i> <i> */</i> <b>void</b> cmdline_parser_print_detailed_help(<b>void</b>); <i>/**</i> <i> * Print the version</i> <i> */</i> <b>void</b> cmdline_parser_print_version(<b>void</b>); <i>/**</i> <i> * Initializes all the fields a cmdline_parser_params structure </i> <i> * to their default values</i> <i> * </i><b>@param</b><i> params the structure to initialize</i> <i> */</i> <b>void</b> cmdline_parser_params_init(<b>struct</b> cmdline_parser_params *params); <i>/**</i> <i> * Allocates dynamically a cmdline_parser_params structure and initializes</i> <i> * all its fields to their default values</i> <i> * </i><b>@return</b><i> the created and initialized cmdline_parser_params structure</i> <i> */</i> <b>struct</b> cmdline_parser_params *cmdline_parser_params_create(<b>void</b>); <i>/**</i> <i> * Initializes the passed gengetopt_args_info structure's fields</i> <i> * (also set default values for options that have a default)</i> <i> * </i><b>@param</b><i> args_info the structure to initialize</i> <i> */</i> <b>void</b> cmdline_parser_init (<b>struct</b> gengetopt_args_info *args_info); <i>/**</i> <i> * Deallocates the string fields of the gengetopt_args_info structure</i> <i> * (but does not deallocate the structure itself)</i> <i> * </i><b>@param</b><i> args_info the structure to deallocate</i> <i> */</i> <b>void</b> cmdline_parser_free (<b>struct</b> gengetopt_args_info *args_info); <i>/**</i> <i> * Checks that all the required options were specified</i> <i> * </i><b>@param</b><i> args_info the structure to check</i> <i> * </i><b>@param</b><i> prog_name the name of the program that will be used to print</i> <i> * possible errors</i> <i> * </i><b>@return</b> <i> */</i> <b>int</b> cmdline_parser_required (<b>struct</b> gengetopt_args_info *args_info, <b>const</b> <b>char</b> *prog_name); <b>extern</b> <b>const</b> <b>char</b> *cmdline_parser_enum_opt_values[]; <i>/**< </i><b>@brief</b><i> Possible values for enum-opt. */</i> <b>#ifdef</b> __cplusplus } <b>#endif</b> <i>/* __cplusplus */</i> <b>#endif</b> <i>/* CMDLINE1_H */</i> </pre> <p><a name="index-argv-62"></a>First of all, notice that the <code>argv</code> parameter (typically corresponding to the homonimous argument of your program's <code>main</code> function) is declared as <code>char **</code> and not as <code>char *const *</code><a rel="footnote" href="#fn-22" name="fnd-22"><sup>22</sup></a>. Actually, the version of <code>getopt_long</code> in libc uses prototypes with <code>char *const *argv</code> that are incorrect because getopt_long and getopt_long_only can permute argv; this is required for backward compatibility (e.g., for LSB 2.0.1)<a rel="footnote" href="#fn-23" name="fnd-23"><sup>23</sup></a>. So, it is better to declare <code>argv</code> as <code>char **</code> in the generated parser functions. <p>The <code><option>_given</code> field is set to 1 when an argument for <code><option></code> has been specified (otherwise it is 0)<a rel="footnote" href="#fn-24" name="fnd-24"><sup>24</sup></a>. This fields also counts the times a multiple option is specified (see <a href="#Multiple-Options">Multiple Options</a>). If the option accepts an argument and it is not of <code>flag</code> type The <code><option>_arg</code> field is set to the value passed at the command line. The <code><option>_arg</code> field has the corresponding C type specified in the file passed to gengetopt. <p>Notice that if an option has a default value, then the corresponding <code><option>_arg</code> will be initialized with that value but the corresponding <code><option>_given</code> will NOT be initialized to 1. Thus, <code><option>_given</code> will effectively inform you if the user has specified that command line option. <p>The additional field <code><option>_orig</code> is always a string containing the original value passed at the command line. This may be different, for instance, in case of numerical arguments: gengetopt converts the passed value (a string) into the corresponding numerical type; due to conversions, float representations, etc., this may not correspond exactly to the original value passed at command line. It can also be different when enumerated options are used (see above): in particular the <code><option>_arg</code> field will contain a value taken from the specified list, while <code><option>_orig</code> contains the (non-ambiguous) prefix specified at the command line. <p>The user can always access this original value by using <code><option>_orig</code> instead of <code><option>_arg</code>, as he sees fit<a rel="footnote" href="#fn-25" name="fnd-25"><sup>25</sup></a>. For instance, gengetopt itself uses the original value when it saves the command line options into a file (see the <code>_file_save</code> function in the following). However, apart from very specific command line processing, the user might hardly need the <code><option>_orig</code> field, and can be always safely use <code><option>_arg</code>. <p>The <code><option>_help</code> contains the string (concerning this very option) that is printed when <code>--help</code> command line is given. <p>If it is of <code>flag</code> type, only the field <code><option>_flag</code> is generated. <p>The strings <code>cmdline_parser_purpose</code> and <code>cmdline_parser_usage</code> contain the <code>purpose</code> as specified in the input file and the generated “usage” string as printed when <code>--help</code> command line is given. Finally, the string array <code>cmdline_parser_help</code> contains the strings (one for each option) printed when <code>--help</code> command line is given (this array is terminated by a null string element). If hidden options are used also the <code>cmdline_parser_full_help</code> array is available (containing also help strings concerning hidden options). If at least one option has <code>details</code>, then the <code>cmdline_parser_detailed_help</code> array is available (containing also help strings concerning hidden options and details for options). All these strings can be used by the programmer to build a customized help output<a rel="footnote" href="#fn-26" name="fnd-26"><sup>26</sup></a>. <p>Even if <code><option>_given</code> is 0, the corresponding <code><option>_arg</code> is set to default value (if one has been specified for <code><option></code>). However, in this case, the <code><option>_orig</code> is set to <code>NULL</code>. <p>Notice that by default the generated function is called <code>cmdline_parser</code> (see the command line options below, to override this name), and it takes the arguments that main receives and a pointer to such a struct, that it will be filled. Another version, <code>cmdline_parser2</code>, can be specified more arguments. Since you typically need this second version only in conjunction with other “kinds” of parsers such as configuration files and multiple parsers, you can find more details about it in <a href="#Configuration-files">Configuration files</a>. <p><b>IMPORTANT</b>: The array passed to the parser function (that in turn is passed to <code>getopt_long</code> is expected to have in the first element (of index 0) the name of the program that was invoked. This will be used, for instance, for printing possible errors. <p><code>cmdline_parser_free</code> can be called to deallocate memory allocated by the parser for string and multiple options. <p><code>cmdline_parser_init</code> can be called to initialize the struct (it is not mandatory, since it is done automatically by the command line parser). <p><code>cmdline_parser_file_save</code><a rel="footnote" href="#fn-27" name="fnd-27"><sup>27</sup></a> can be used to save the command line options into a file. The contents of this file are consistent with the configuration files (<a href="#Configuration-files">Configuration files</a>). Notice that if an option has a default value, this option will be saved into the file only if it was passed explicitly at command line (or read from a configuration file), i.e., default values will not be saved into the file. Alternatively, you can use <code>cmdline_parser_dump</code><a rel="footnote" href="#fn-28" name="fnd-28"><sup>28</sup></a> that takes as the first parameter an already open stream (<code>FILE *</code>) instead of a file name. <p>And here's how these functions can be used inside the main program: <!-- Generator: GNU source-highlight, by Lorenzo Bettini, http://www.gnu.org/software/src-highlite --> <pre class="example"> <i>/* main1.cc */</i> <i>/* we try to use gengetopt generated file in a C++ program */</i> <i>/* we don't use autoconf and automake vars */</i> <b>#include</b> <tt><iostream></tt> <b>#include</b> <tt>"stdlib.h"</tt> <b>#include</b> <tt>"cmdline1.h"</tt> <b>using</b> std::cout; <b>using</b> std::endl; <b>int</b> main (<b>int</b> argc, <b>char</b> **argv) { gengetopt_args_info args_info; cout << <tt>"This one is from a C++ program"</tt> << endl ; cout << <tt>"Try to launch me with some options"</tt> << endl ; cout << <tt>"(type sample1 --help for the complete list)"</tt> << endl ; cout << <tt>"For example: ./sample1 *.* --funct-opt"</tt> << endl ; <i>/* let's call our cmdline parser */</i> <b>if</b> (cmdline_parser (argc, argv, &args_info) != 0) exit(1) ; cout << <tt>"Here are the options you passed..."</tt> << endl; <b>for</b> ( <b>unsigned</b> i = 0 ; i < args_info.inputs_num ; ++i ) cout << <tt>"file: "</tt> << args_info.inputs[i] << endl ; <b>if</b> ( args_info.funct_opt_given ) cout << <tt>"You chose --funct-opt or -F."</tt> << endl ; <b>if</b> ( args_info.str_opt_given ) cout << <tt>"You inserted "</tt> << args_info.str_opt_arg << <tt>" for "</tt> << <tt>"--str-opt option."</tt> << endl ; <b>if</b> ( args_info.int_opt_given ) cout << <tt>"This is the integer you input: "</tt> << args_info.int_opt_arg << <tt>"."</tt> << endl; <b>if</b> (args_info.flag_opt_given) cout << <tt>"The flag option was given!"</tt> << endl; cout << <tt>"The flag is "</tt> << ( args_info.flag_opt_flag ? <tt>"on"</tt> : <tt>"off"</tt> ) << <tt>"."</tt> << endl ; <b>if</b> (args_info.enum_opt_given) { cout << <tt>"enum-opt value: "</tt> << args_info.enum_opt_arg << endl; cout << <tt>"enum-opt (original specified) value: "</tt> << args_info.enum_opt_orig << endl; } <b>if</b> (args_info.secret_given) cout << <tt>"Secret option was specified: "</tt> << args_info.secret_arg << endl; cout << args_info.def_opt_arg << <tt>"! "</tt>; cout << <tt>"Have a nice day! :-)"</tt> << endl ; cmdline_parser_free (&args_info); <i>/* release allocated memory */</i> <b>return</b> 0; } </pre> <p>Now you can compile <samp><span class="file">main1.cc</span></samp> and the <samp><span class="file">cmdline1.c</span></samp> generated by gengetopt and link all together to obtain <code>sample1</code> executable: <pre class="example"> gcc -c cmdline1.c g++ -c main1.cc g++ -o sample1 cmdline1.o main1.o </pre> <p>(Here we assume that <code>getopt_long</code> is included in the standard C library; see <a href="#Installation">Installation</a> and <a href="#No-getopt_005flong">No getopt_long</a>). <p>Now let's try some tests with this program: <pre class="example"> $ ./sample1 -s "hello" --int-opt 1234 This one is from a C++ program Try to launch me with some options (type sample1 --help for the complete list) For example: ./sample1 *.* --funct-opt Here are the options you passed... You inserted hello for --str-opt option. This is the integer you input: 1234. The flag is off. Have a nice day! :-) </pre> <p>You can also pass many file names to the command line (this also shows how flags work): <pre class="example"> $ ./sample1 *.h -i -100 -x This one is from a C++ program Try to launch me with some options (type sample1 --help for the complete list) For example: ./sample1 *.* --funct-opt Here are the options you passed... file: cmdline1.h file: cmdline2.h file: cmdline.h file: getopt.h This is the integer you input: -100. The flag is on. Have a nice day! :-) </pre> <p>And if we try to omit the <code>--int-opt</code> (or <code>-i</code>), which is required, we get an error: <pre class="example"> $ ./sample1 This one is from a C++ program Try to launch me with some options (type sample1 --help for the complete list) For example: ./sample1 *.* --funct-opt sample1: `--int-opt' (`-i') option required! </pre> <p>Now, let's test the enumerated options, notice the use of a prefix for specifying an acceptable value, and the difference between the actual passed value and the one recorded in <code><option>_arg</code>: <pre class="example"> $ ./sample1 -i 10 --enum-opt h ... enum-opt value: hello enum-opt (original specified) value: h ... </pre> <p>While the next one raises an ambiguity error (between <code>"bar"</code> and <code>"bye"</code>): <pre class="example"> $ ./sample1 -i 10 --enum-opt b ... ./sample1: ambiguous argument, "b", for option `--enum-opt' </pre> <p><a name="showrequired"></a><a name="index-g_t_0040code_007b_002dh_002c_002d_002dhelp_007d-63"></a>Here is the output of <code>--help</code> of the parser generated from <samp><span class="file">sample1.ggo</span></samp> by specifying the following options to gengetopt: <code>--long-help -u --show-required</code> (see <a href="#Invoking-gengetopt">Invoking gengetopt</a> for further explanation for these command line options). <p><a name="helpoutput"></a> <pre class="example"> This one is from a C++ program Try to launch me with some options (type sample1 --help for the complete list) For example: ./sample1 *.* --funct-opt sample1 2.0 Usage: sample1 -iINT|--int-opt=INT [-h|--help] [--detailed-help] [--full-help] [-V|--version] [-sfilename|--str-opt=filename] [-mINT|--my-opt=INT] [--flag-opt] [-F|--funct-opt] [--long-opt=LONG] [--def-opt=STRING] [--enum-opt=STRING] [-DINT|--dependant=INT] [FILES]... -h, --help Print help and exit --detailed-help Print help, including all details and hidden options, and exit --full-help Print help, including hidden options, and exit -V, --version Print version and exit -s, --str-opt=filename A string option, for a filename A brief text description before the other options. -m, --my-opt=INT Another integer option, this time the description of the option should be "quite" long to require wrapping... possibly more than one wrapping :-) especially if I require a line break -i, --int-opt=INT A int option (mandatory) more involved options: the following options are more complex --flag-opt A flag option (default=off) -F, --funct-opt A function option last option section: --long-opt=LONG A long option --def-opt=STRING A string option with default (default=`Hello') --enum-opt=STRING A string option with list of values (possible values="foo", "bar", "hello", "bye" default=`hello') -D, --dependant=INT option that depends on str-opt An ending text. </pre> <p><a name="index-wrapping-64"></a>Notice how <code>filename</code> is printed instead of <code>STRING</code> for the option <code>--str-opt</code> (since <code>typestr</code> was used in the <samp><span class="file">sample1.ggo</span></samp> file) and how the description of <code>--my-opt</code> is wrapped to 80 columns, and how the <code>\n</code> is actually interpreted as a newline request. Also the usage string is wrapped. Moreover, since <code>-S,--secret</code> is <a name="index-hidden-65"></a>an hidden option (See <a href="#hidden">Hidden options</a>.) it is not printed; <a name="index-g_t_0040code_007b_002d_002dfull_002dhelp_007d-66"></a>if you wanted that to be printed, you should use <code>--full-help</code>. <a name="index-details-67"></a>The option <code>--func-opt</code> has also the <code>details</code>, but they are not printed with <code>--help</code>. <p>Finally, notice how the <code>text</code> strings are printed in the help output (and the empty line after the “more involved options” section achieved with an empty <code>text</code> string). <p><a name="index-g_t_0040code_007b_002dh_002c_002d_002ddetailed_002dhelp_007d-68"></a>Instead, here is the output of <code>--detailed-help</code> of the parser generated from <samp><span class="file">sample1.ggo</span></samp>. You may want to compare this output with the one produced by <code>--help</code> (See <a href="#helpoutput">Output of <code>--help</code></a>.); in particular, you may notice that the hidden option <code>--secret</code> is actually printed and the details of <code>--func-opt</code> are printed too: <pre class="example"> This one is from a C++ program Try to launch me with some options (type sample1 --help for the complete list) For example: ./sample1 *.* --funct-opt sample1 2.0 Usage: sample1 -iINT|--int-opt=INT [-h|--help] [--detailed-help] [--full-help] [-V|--version] [-sfilename|--str-opt=filename] [-mINT|--my-opt=INT] [--flag-opt] [-F|--funct-opt] [--long-opt=LONG] [--def-opt=STRING] [--enum-opt=STRING] [-DINT|--dependant=INT] [FILES]... -h, --help Print help and exit --detailed-help Print help, including all details and hidden options, and exit --full-help Print help, including hidden options, and exit -V, --version Print version and exit -s, --str-opt=filename A string option, for a filename A brief text description before the other options. -m, --my-opt=INT Another integer option, this time the description of the option should be "quite" long to require wrapping... possibly more than one wrapping :-) especially if I require a line break -i, --int-opt=INT A int option (mandatory) more involved options: the following options are more complex --flag-opt A flag option (default=off) -F, --funct-opt A function option A function option is basically an option with no argument. It can be used, e.g., to specify a specific behavior for a program. Well, this further explanation is quite useless, but it's only to show an example of an option with details, which will be printed only when --detailed-help is given at the command line. last option section: --long-opt=LONG A long option --def-opt=STRING A string option with default (default=`Hello') --enum-opt=STRING A string option with list of values (possible values="foo", "bar", "hello", "bye" default=`hello') -S, --secret=INT hidden option will not appear in --help -D, --dependant=INT option that depends on str-opt An ending text. </pre> <p>If you're curious you may want to take a look at the generated C file <samp><span class="file">cmdline1.c</span></samp>. <p>You may find other examples in <samp><span class="file">/prefix/share/doc/gengetopt/examples</span></samp> or in the <samp><span class="file">tests</span></samp> of the source tarbal. <div class="node"> <a name="Invoking-gengetopt"></a> <p><hr> Next: <a rel="next" accesskey="n" href="#Terminology">Terminology</a>, Previous: <a rel="previous" accesskey="p" href="#Basic-Usage">Basic Usage</a>, Up: <a rel="up" accesskey="u" href="#Top">Top</a> </div> <h2 class="chapter">3 Invoking gengetopt</h2> <p><a name="index-invoking-69"></a><a name="index-gengetopt-options-70"></a> This is the output of <code>gengetopt --help</code>: <pre class="smallexample"> gengetopt This program generates a C function that uses getopt_long function to parse the command line options, validate them and fill a struct. Usage: gengetopt [OPTIONS]... -h, --help Print help and exit --detailed-help Print help, including all details and hidden options, and exit -V, --version Print version and exit Main options: -i, --input=filename input file (default std input) -f, --func-name=name name of generated function (default=`cmdline_parser') -a, --arg-struct-name=name name of generated args info struct (default=`gengetopt_args_info') -F, --file-name=name name of generated file (default=`cmdline') --output-dir=path output directory --header-output-dir=path header output directory --src-output-dir=path source output directory -c, --c-extension=ext extension of c file (default=`c') -H, --header-extension=ext extension of header file (default=`h') -l, --long-help long usage line in help --default-optional by default, an option is considered optional if not specified otherwise -u, --unamed-opts[=STRING] accept options without names (e.g., file names) (default=`FILES') The parser generated is thought to be used to parse the command line arguments. However, you can also generate parsers for configuration files, or strings that contain the arguments to parse, by using the following two options. -C, --conf-parser generate a config file parser -S, --string-parser generate a string parser (the string contains the command line) Additional options: -G, --include-getopt adds the code for getopt_long in the generated C file -n, --no-handle-help do not handle --help|-h automatically --no-help do not add --help|-h automatically -N, --no-handle-version do not handle --version|-V automatically --no-version do not add --version|-V automatically -e, --no-handle-error do not exit on errors --show-required[=STRING] in the output of help will specify which options are mandatory, by using the optional passed string (default=`(mandatory)') -g, --gen-version put gengetopt version in the generated file (default=on) --set-package=STRING set the package name (override package defined in the .ggo file) --set-version=STRING set the version number (override version defined in the .ggo file) --show-help show the output of --help instead of generating code --show-full-help show the output of --full-help (i.e., including hidden options) instead of generating code --show-detailed-help show the output of --detailed-help (i.e., including details and hidden options) instead of generating code --show-version show the output of --version instead of generating code Please refer to the info manual for further explanations. </pre> <p>The options should be clear; in particular: <dl> <dt><code>--func-name</code><dd><a name="index-g_t_0040code_007b_002d_002dfunc_002dname_007d-71"></a>if no <code>--func-name</code> is given, <code>cmdline_parser</code> is taken by default; <br><dt><code>--output-dir</code><dd><a name="index-g_t_0040code_007b_002d_002doutput_002ddir_007d-72"></a>if no <code>--output-dir</code><a rel="footnote" href="#fn-29" name="fnd-29"><sup>29</sup></a> is given, the files are generated in the current directory; <br><dt><code>--src-output-dir</code><br><dt><code>--header-output-dir</code><dd><a name="index-g_t_0040code_007b_002d_002dsrc_002doutput_002ddir_007d-73"></a><a name="index-g_t_0040code_007b_002d_002dheader_002doutput_002ddir_007d-74"></a>With these options<a rel="footnote" href="#fn-30" name="fnd-30"><sup>30</sup></a> the generated C file and the corresponding generated header files can be generated in different directories; <br><dt><code>--arg-struct-name</code><dd><a name="index-g_t_0040code_007b_002d_002darg_002dstruct_002dname_007d-75"></a>allows to specify the name of the generated struct for command line arguments (default is <code>gengetopt_args_info</code>) <br><dt><code>--long-help</code><dd><a name="index-g_t_0040code_007b_002d_002dlong_002dhelp_007d-76"></a>the “Usage” line reports all the options; this may be unpleasant if options are many; <br><dt><code>--default-optional</code><dd><a name="index-g_t_0040code_007b_002d_002ddefault_002doptional_007d-77"></a>If this command line option is given, by default, options are considered optional (if not explicitly specified otherwise). Otherwise, options are considered mandatory (if not explicitly specified otherwise). <br><dt><code>--unamed-opts</code><dd><a name="index-g_t_0040code_007b_002d_002dunamed_002dopts_007d-78"></a>the program will accept also options without a name, which, in most case, means that we can pass many file names to the program (see the example in <a href="#Basic-Usage">Basic Usage</a>, where we call <code>sample1 *.h</code>). You can specify an optional description for these additional names (default is <code>FILES</code>). <br><dt><code>--no-handle-help</code><dt><code>--no-handle-version</code><dd><a name="index-g_t_0040code_007b_002d_002dno_002dhandle_002dhelp_007d-79"></a><a name="index-g_t_0040code_007b_002d_002dno_002dhandle_002dversion_007d-80"></a>if <code>--no-handle-help</code> (<code>--no-handle-version</code>) is given the command line option <code>--help|-h</code> (<code>--version|-V</code>) is not handled automatically, so the programmer will be able to print some other information; then the function for printing the standard help (version) response can be used; this function is called <code><parser-name>_print_help</code> (<code><parser-name>_print_version</code>), where <code><parser-name></code> is the name specified with <code>--func-name</code> or the default, <code>cmdline_parser</code>. In case hidden options are used, See <a href="#hidden">Hidden options</a>, also the function <code><parser-name>_print_full_help</code> will be generated; if <code>details</code> are used for at least one option, then also the function <code><parser-name>_print_detailed_help</code> will be generated. Notice that, although the programmer can handle these options manually, the parser will return after finding one of these options: the other command line options, if any, will be ignored. In case you want to have full control on <code>--help|-h</code>, <code>--version|-V</code>, you should use the following options: <br><dt><code>--no-help</code><dt><code>--no-version</code><dd><a name="index-g_t_0040code_007b_002d_002dno_002dhelp_007d-81"></a><a name="index-g_t_0040code_007b_002d_002dno_002dversion_007d-82"></a>With these options<a rel="footnote" href="#fn-31" name="fnd-31"><sup>31</sup></a> you can disable the automatic addition of options <code>--help|-h</code> and <code>--version|-V</code>, respectively. The programmer will then be able to add these options in the input file and handle them as he sees fit. Notice that <code>--no-help</code> will also disable the automatic options <code>--detailed-help</code> and <code>--full-help</code>. The programmer can still define options with short character <code>h</code> and <code>V</code> as he wants, but he cannot define options <code>help</code> and <code>version</code>, unless he specifies <code>--no-help</code> and <code>--no-version</code>, respectively (otherwise an error will be printed). An example using these options and manually handles <code>--help</code> and <code>--version</code> can be found in <samp><span class="file">test_manual_help_cmd.ggo</span></samp> and <samp><span class="file">test_manual_help.c</span></samp> in the <samp><span class="file">examples</span></samp> directory. <br><dt><code>--no-handle-error</code><dd><a name="index-g_t_0040code_007b_002d_002dno_002dhandle_002derror_007d-83"></a>if <code>--no-handle-error</code> is given, an error in the parsing does not provoke the exit of the program; instead, since the parser function, in case of an error, returns a value different 0, the program can print a help message, as gengetopt itself does in case of an error (try it!). <br><dt><code>--show-required</code><dd><a name="index-g_t_0040code_007b_002d_002dshow_002drequired_007d-84"></a>if <code>--show-required</code> is given, possibly with a string, in the output of <code>--help</code> will be made explicit which options are actually required, See <a href="#showrequired">Basic Usage</a>. <br><dt><code>--gen-version</code><dd><a name="index-g_t_0040code_007b_002d_002dgen_002dversion_007d-85"></a>is a flag (default on) that when disabled does not put in the output file the gengetopt version (it is useful for testing purposes). <br><dt><code>--conf-parser</code><dd><a name="index-g_t_0040code_007b_002d_002dconf_002dparser_007d-86"></a>Detailed in <a href="#Configuration-files">Configuration files</a>. <br><dt><code>--string-parser</code><dd><a name="index-g_t_0040code_007b_002d_002dstring_002dparser_007d-87"></a>Detailed in <a href="#String-Parsers-and-Multiple-Parsers">String Parsers and Multiple Parsers</a>. <br><dt><code>--include-getopt</code><dd><a name="index-g_t_0040code_007b_002d_002dinclude_002dgetopt_007d-88"></a>Adds the code for <code>getopt_long</code> into the generated parser C file. This will make your generated parser much bigger, but it will be compiled in any system, even if <code>getopt_long</code> is not part of the C library where your program is being compiled. See also <a href="#No-getopt_005flong">No getopt_long</a>. <br><dt><code>--show-help</code><dt><code>--show-full-help</code><dt><code>--show-version</code><dd><a name="index-g_t_0040code_007b_002d_002dshow_002dhelp_007d-89"></a><a name="index-g_t_0040code_007b_002d_002dshow_002dfull_002dhelp_007d-90"></a><a name="index-g_t_0040code_007b_002d_002dshow_002dversion_007d-91"></a>only make gengetopt show the output of <code>--help</code>, <code>--full-help</code> and <code>--version</code> command lines without generating any code, See <a href="#addedoptions">Automatically added options</a>. For instance, I use the <code>--show-help</code> option to generate a texinfo file with the output of help (this also shows an example of use of <code>--set-package</code> and <code>--set-version</code>): <pre class="example"> ../src/gengetopt --show-help -i ../src/cmdline.ggo \ --set-package="gengetopt" \ --set-version="" > help_output.texinfo </pre> </dl> <p>You may have already guessed it: gengetopt uses gengetopt itself for command line options, and its specification file is <code>cmdline.ggo</code> in the source directory. In particular the command line for gengetopt itself is generated with the following command: <pre class="example"> gengetopt --input=cmdline.ggo --no-handle-version \ --no-handle-help --no-handle-error </pre> <p>Indeed when <code>--help|-h</code> is passed on the command line, gengetopt will call <code>cmdline_parser_print_help()</code> and then the lines for reporting bugs. When <code>--version|-V</code> is passed, it will call cmdline_parser_print_version() and then prints a copyright. If an error occurs it prints a message on the screen: <pre class="example"> $ ./gengetopt --zzzz ./gengetopt: unrecognized option `--zzzz' Run gengetopt --help to see the list of options. </pre> <div class="node"> <a name="Terminology"></a> <p><hr> Next: <a rel="next" accesskey="n" href="#Options-with-enumerated-values">Options with enumerated values</a>, Previous: <a rel="previous" accesskey="p" href="#Invoking-gengetopt">Invoking gengetopt</a>, Up: <a rel="up" accesskey="u" href="#Top">Top</a> </div> <h2 class="chapter">4 Terminology</h2> <p><a name="index-Terminology-92"></a><a name="index-argument_002c-defined-93"></a><a name="index-option_002c-defined-94"></a><a name="index-value_002c-defined-95"></a><a name="index-parameter_002c-defined-96"></a><a name="index-short-option_002c-defined-97"></a><a name="index-long-option_002c-defined-98"></a><a name="index-multiple-option_002c-defined-99"></a><a name="index-required-option_002c-defined-100"></a> An <dfn>argument</dfn> is an element of the <code>argv</code> array passed into your C or C++ program by your operating system. <p>An <dfn>option</dfn> is an argument that begins with <samp><span class="option">-</span></samp>, or <samp><span class="option">--</span></samp>. <p>A <dfn>value</dfn> is an argument, or part of an argument, that is associated with a particular option (an option may also not accept any value). For example, in <pre class="example"> > ls --width=80 </pre> <p class="noindent"><samp><span class="command">ls</span></samp> is called with one argument, <samp><span class="option">--width=80</span></samp>, which is an option that has a value, <code>80</code>, while in <pre class="example"> > ls --width 80 </pre> <p class="noindent"><samp><span class="command">ls</span></samp> is called with two arguments, <samp><span class="option">--width</span></samp>, which is an option, and <code>80</code> which might or might not be a value. In this case, whether the <code>80</code> is treated as a value associated with the preceding <samp><span class="option">--width</span></samp> option, or as the name of a file to list depends on how <samp><span class="command">ls</span></samp> parses the <samp><span class="option">--width</span></samp> option. <p>The order in which options are specified is <em>usually</em> unimportant: <pre class="example"> > ls -a -l > ls -l -a </pre> <p class="noindent">both do exactly the same thing. <p>An <dfn>parameter</dfn> is an argument that is not an option. For example, in <pre class="example"> > cp --archive source dest </pre> <p class="noindent"><samp><span class="command">cp</span></samp> is called with three arguments, the option <samp><span class="option">--archive</span></samp>, the parameter <code>source</code>, and the parameter <code>dest</code>. Unlike options, the order in which parameters are specified usually <em>is</em> important: <pre class="example"> > cp --archive --verbose source dest > cp --verbose --archive source dest > cp --archive source --verbose dest > cp --archive --verbose dest source </pre> <p class="noindent">The first three <samp><span class="command">cp</span></samp> commands do the same thing, but the fourth one is completely different. <p>If you're new to Gengetopt, you may wish to skip the rest of this section. It goes into more detail about different sorts of options, and how they are parsed. <p>Note that some parameters may begin with <samp><span class="option">-</span></samp> or <samp><span class="option">--</span></samp>. Equivalently, not <em>all</em> arguments that begin with <samp><span class="option">-</span></samp> or <samp><span class="option">--</span></samp> are options. Consider <pre class="example"> > ls -- -file > tar -c -f - . > ../foo.tar </pre> <p class="noindent">The <samp><span class="command">ls</span></samp> command has two arguments; the first argument, <samp><span class="option">--</span></samp> is ignored by <samp><span class="command">ls</span></samp>, but causes the <samp><span class="option">-file</span></samp> argument to be interpreted as a parameter. The <samp><span class="command">tar</span></samp> command has four arguments. The <samp><span class="option">-c</span></samp> argument tells tar to create an archive; the <samp><span class="option">-f</span></samp> argument, which takes a value, <code>-</code>, tells tar that the archive should be written onto the standard output, and the fourth argument, <code>.</code>, tells tar what directories to include in the archive. (The remaining two items, <code>></code> and <code>../foo.tar</code>, tell the shell to redirect the <samp><span class="command">tar</span></samp> command's output to the file ../foo.tar. The <samp><span class="command">tar</span></samp> command doesn't even see them.) <p>The GNU convention is that <samp><span class="option">-</span></samp> by itself is always interpreted as a value or parameter, while the first <samp><span class="option">--</span></samp> by itself is always ignored, but causes all subsequent arguments to be interpreted as parameters. Gengetopt always behaves this way. <p>A <dfn>short option</dfn> is an option that begins with <samp><span class="option">-</span></samp>. Not including the leading dash, short options must be one character long: <pre class="example"> > ls -a -l -t --width=80 </pre> <p class="noindent">The <samp><span class="option">-a</span></samp>, <samp><span class="option">-l</span></samp>, and <samp><span class="option">-t</span></samp> options are all short options. Multiple short options may be combined into a single argument: <pre class="example"> > ls -alt --width=80 </pre> <p class="noindent">is equivalent to the above example. <p>A <dfn>long option</dfn> is an option that begins with <samp><span class="option">-</span></samp> or <samp><span class="option">--</span></samp>. Ignoring the leading punctuation, long options may be one or more characters long: <pre class="example"> > ls --all -fs </pre> <p class="noindent">The <samp><span class="command">ls</span></samp> command has two arguments; the long option <samp><span class="option">--all</span></samp>, and the pair of short options <samp><span class="option">-fs</span></samp>. <p>Long options need not have synonymous short options; after all, complex programs like <samp><span class="command">cc</span></samp> have more long options than there are valid short option characters; it wouldn't be possible to assign a short option to each of them. Short options are encouraged, but not required, to have a synonymous long option. <p>Long options may be abbreviated, as long as the abbreviation is not ambiguous. Gengetopt automatically treats unambiguous abbreviations as synonyms. <p>Short options may have values just like long options, but if several short options are grouped together into one argument, only the last one may have a value. Values in the same argument as a long option are delimited by an equals sign, values in the same argument as a short option are not: <pre class="example"> > ls --width 60 # ok, value is "60" > ls --width=60 # ok, value is "60" > ls -w60 # ok, value is "60" > ls -w 60 # ok, value is "60" > ls -w=60 # unexpected, value is "=60" > ls -T7 -w60 # ok, value for -T is 7, value for -w is 60 > ls -T7w60 # unexpected, value for -T is "7w60", no -w at all </pre> <p>A <dfn>required option</dfn> must be present, otherwise an error will be raised. <p>A <dfn>multiple option</dfn> is an option that may appear more than once on the command line. Gengetopt would create a tidy array for multiple options (see <a href="#Multiple-Options">Multiple Options</a>, for further details about dealing with multiple options). <div class="node"> <a name="Options-with-enumerated-values"></a> <p><hr> Next: <a rel="next" accesskey="n" href="#Group-options">Group options</a>, Previous: <a rel="previous" accesskey="p" href="#Terminology">Terminology</a>, Up: <a rel="up" accesskey="u" href="#Top">Top</a> </div> <h2 class="chapter">5 Options with enumerated values</h2> <p><a name="index-enumerated-options-101"></a><a name="index-values-102"></a>You can also specify the list of <code>values</code> that can be passed to an option (if the type is not specified, the option has type <code>string</code>). If a value that is not in the list is passed, an error is raised. You can think of such options as <em>enumerated</em> options. It is not necessary to pass the complete value at the command line option: a non ambiguous prefix will do. For instance, if the accepted values are <code>"foo","bar","foobar"</code>, then you can pass at the command line the value <code>"b"</code> and the value <code>"bar"</code> will be selected, or the value <code>"foob"</code> and the value <code>"foobar"</code> will be selected; instead, passing the value <code>"fo"</code> will raise an ambiguity error. <p>Since version 2.22 options with values can be given a specific type (the default is string). If you give a numeric type to such options, gengetopt will check that the enumerated values are actually valid values for that numeric type. <p>As for other options, the <code><option>_arg</code> field will have the specified type, while the <code><option>_orig</code> field will always be a string (<code>char *</code>) storing the (non-ambiguous) prefix specified at the command line. <p>For such an option, no matter what its type is, an array of strings, <code><parser-name>_<option>_values</code>, will be generated that contains all the strings representing the possible accepted values. <p><a name="index-enum-103"></a>An option with enumerated values can also be given the type <code>enum</code>; in that case, a C <code>enum</code> type is also generated with name <code>enum_<option></code>; the values of such C enum will be generated according this pattern: <code><option>_arg_<value></code>, where <code>value</code> is the value specified in the input file, and the starting value is always 0. For instance, if we specify in the input file the following option <pre class="example"> option "myopt" ... ... values="FOO","180","BAR" enum ... </pre> <p class="noindent">then the following C enum will be generated: <pre class="example"> enum enum_myopt { myopt_arg_FOO = 0, myopt_arg_180, myopt_arg_BAR }; </pre> <p>If you use the symbols <code>+</code> and <code>-</code>, these will be translated into <code>PLUS_</code> and <code>MINUS_</code>, respectively, in the the C enum. Thus, if we specify in the input file the following option <pre class="example"> option "myopt" ... ... values="+foo","-all","-foo" enum ... </pre> <p class="noindent">then the following C enum will be generated: <pre class="example"> enum enum_myopt { myopt_arg_PLUS_foo = 0, myopt_arg_MINUS_all, myopt_arg_MINUS_foo }; </pre> <p>An example using options with values (and enum options) is <samp><span class="file">tests/test_values_cmd.ggo</span></samp> and <samp><span class="file">tests/test_values.c</span></samp>. <div class="node"> <a name="Group-options"></a> <p><hr> Next: <a rel="next" accesskey="n" href="#Mode-options">Mode options</a>, Previous: <a rel="previous" accesskey="p" href="#Options-with-enumerated-values">Options with enumerated values</a>, Up: <a rel="up" accesskey="u" href="#Top">Top</a> </div> <h2 class="chapter">6 Group options</h2> <p><a name="index-group-options-104"></a> It is also possible to group options; options belonging to a <em>group</em> are considered <em>in mutual exclusion</em>. In order to use this feature, first the <code>group</code> has to be defined, and then a <code>groupoption</code> can be defined. A groupoption has basically the same syntax of a standard option, apart that the required flag must not be specified (it would not make sense, since the options of the same group are mutual exclusive) and the group to which the option belongs has to be specified. <pre class="example"> defgroup "<group name>" {groupdesc="<group description>"} {required} groupoption <long> <short> "<desc>" <argtype> group="<group name>" \ {argoptional} {multiple} </pre> <p>If a group is defined as <code>required</code>, then one (but only one) option belonging to the group has to be specified. <p>Here's an example (taken from the test <samp><span class="file">test_group_cmd.ggo</span></samp>): <pre class="example"> defgroup "my grp2" defgroup "grp1" groupdesc="an option of this group is required" required groupoption "opta" a "string a" group="grp1" multiple groupoption "optA" A "string A" string group="grp1" argoptional groupoption "optAmul" M "string M" string group="grp1" argoptional multiple groupoption "optb" b "string b" group="grp1" groupoption "optc" - "string c" group="my grp2" groupoption "optd" d "string d" group="my grp2" </pre> <p>The group <code>grp1</code> is required, so either <code>--opta</code> or <code>--optb</code> has to be specified (but only one of them). Here's the output of some executions: <pre class="example"> $ ./test_groups test_groups: 0 options of group grp1 were given. One is required $ ./test_groups -a OK $ ./test_groups -a -a OK (the same option given twice) $ ./test_groups -a -b test_groups: 2 options of group grp1 were given. One is required $ ./test_groups -a -c OK $ ./test_groups -a --optc -d test_groups: 2 options of group my grp2 were given. At most one is required </pre> <div class="node"> <a name="Mode-options"></a> <p><hr> Next: <a rel="next" accesskey="n" href="#Parser-function-additional-parameters">Parser function additional parameters</a>, Previous: <a rel="previous" accesskey="p" href="#Group-options">Group options</a>, Up: <a rel="up" accesskey="u" href="#Top">Top</a> </div> <h2 class="chapter">7 Mode options</h2> <p><a name="index-mode-options-105"></a> It is also possible to specify “mode options”; options belonging to a <em>mode</em> are considered <em>in mutual exclusion</em> with options of a different mode. Thus, you can specify more options belonging to the same mode, but you cannot specify, on the same command line, two options belonging to two different modes (thus, modes are different from groups, <a href="#Group-options">Group options</a>). <p>These sets of options are called modes, since they represent the different modes (modalities), in which a program can be run. <p>In order to use this feature, first the <code>mode</code> has to be defined, and then a <code>modeoption</code> can be defined. A modeoption has basically the same syntax of a standard option, and it can be given the required flag must not be specified (with a slightly different semantics, see below) and the group to which the option belongs has to be specified. <pre class="example"> defmode "<mode name>" {modedesc="<mode description>"} modeoption <long> <short> "<desc>" <argtype> mode="<mode name>" \ {argoptional} {multiple} {required} </pre> <p>If a mode option is specified as <code>required</code>, then it will be required only if other options of the same mode are specified; this makes it possible to specify options of different modes as required. <p>Options not belonging to any mode are not in conflict with mode options. <p>For instance, let us consider the file <samp><span class="file">test_modes_cmd.ggo</span></samp>: <!-- Generator: GNU source-highlight, by Lorenzo Bettini, http://www.gnu.org/software/src-highlite --> <pre class="example"> package "test_modes" version "1.0" section "some non mode options" option "no-mode" N "a generic option not beloging to any mode" optional option "no-mode2" - "another generic option not beloging to any mode" string optional section "some modes just for testing" defmode "mode 2" defmode "my mode" defmode "mode1" modedesc="any option of this mode is in contrast with any \ option of the other mode\nNotice that this description is quite long so \ it may spawn many lines... \ fortunately gengetopt will wrap it for you :-)" modeoption "opta" a "string a" multiple mode="mode1" optional modeoption "optA" A "string A" string argoptional mode="mode1" required modeoption "optAmul" M "string M" argoptional string mode="mode1" multiple optional modeoption "optb" b "string b" mode="mode1" optional modeoption "optc" - "string c" mode="mode 2" optional modeoption "optd" d "string d" mode="mode 2" required modeoption "mopt" m "option of my mode" int optional mode="my mode" optional </pre> <p class="noindent">Now, we use the program <samp><span class="file">test_modes</span></samp> (that uses the generated parser for the input file above) to demonstrate how the parser generated by gengetopt perform checks on mode options. <pre class="example"> test_modes -N </pre> <p class="noindent">This execution generates no errors (although there are required options which are not specified, these required options are part of modes and they are required only if that mode is used). <pre class="example"> test_modes -a ./test_modes: '--optA' ('-A') option required </pre> <p class="noindent">Since an option of a mode is specified, then required options of that mode must be provided, but, in this execution, we forgot to specify a required option of the mode that is being used. <pre class="example"> test_modes -a -A -N </pre> <p class="noindent">This execution is correct: we specified two options of the same mode, in particular we also specified the required option of that mode. Notice that we use also an option not belonging to any mode, which does not interfere with mode options. <pre class="example"> test_modes -a -A -N --optc test_modes: option --optc conflicts with option --opta test_modes: option --optc conflicts with option --optA test_modes: '--optd' ('-d') option required </pre> <p class="noindent">Here we see a conflict, (actually two), since the last option we specified belongs to a mode that is different from the one of the first two options. <p>If you require gengetopt to generate <code>--full-help</code> (See <a href="#fullhelp"><code>--full-help</code></a>.), the usage string will be generated so that it will show the modes of the program; for instance, this is the output of <code>--help</code> of the generated parser for the input file above: <pre class="example"> test_modes 1.0 Usage: test_modes [-h|--help] [-V|--version] [-N|--no-mode] [--no-mode2=STRING] or : test_modes -d|--optd [--optc] or : test_modes -ASTRING|--optA=STRING [-a|--opta] [-MSTRING|--optAmul=STRING] [-b|--optb] or : test_modes [-mINT|--mopt=INT] -h, --help Print help and exit -V, --version Print version and exit some non mode options: -N, --no-mode a generic option not beloging to any mode --no-mode2=STRING another generic option not beloging to any mode some modes just for testing: Mode: mode1 any option of this mode is in contrast with any option of the other mode Notice that this description is quite long so it may spawn many lines... fortunately gengetopt will wrap it for you :-) -a, --opta string a -A, --optA[=STRING] string A -M, --optAmul[=STRING] string M -b, --optb string b Mode: mode 2 --optc string c -d, --optd string d Mode: my mode -m, --mopt=INT option of my mode </pre> <div class="node"> <a name="Parser-function-additional-parameters"></a> <p><hr> Next: <a rel="next" accesskey="n" href="#Configuration-files">Configuration files</a>, Previous: <a rel="previous" accesskey="p" href="#Mode-options">Mode options</a>, Up: <a rel="up" accesskey="u" href="#Top">Top</a> </div> <h2 class="chapter">8 Parser function additional parameters</h2> <p><a name="index-parser-parameters-106"></a><a name="index-parameters-107"></a> Besides the parser functions, in the generated header file, gengetopt also generates<a rel="footnote" href="#fn-32" name="fnd-32"><sup>32</sup></a> an additional structure <code><cmd_parser_name>_params</code> that can be used to customize the invocation of the generated parsers (it is especially useful when using configuration file parsers, <a href="#Configuration-files">Configuration files</a>, string parsers, <a href="#String-Parsers-and-Multiple-Parsers">String Parsers and Multiple Parsers</a>, and, in general, multiple parsers). <p>These are the fields of this structure (as usual, boolean options are represented as <code>int</code> and they are <code>true</code> if they are set to <code>1</code> and <code>false</code> if they are set to <code>0</code>): <dl> <dt><code>int initialize (default = 1)</code><dd><a name="index-initialize-108"></a>tells whether the args_info struct has to be initialized. <br><dt><code>int override (default = 0)</code><dd><a name="index-override-109"></a>tells whether the values for the options that are parsed should override those that are already present in the passed args_info struct (e.g., this is the case when you call a parser multiple times using the same args_info struct, see, e.g., <a href="#Configuration-files">Configuration files</a> and<a href="#String-Parsers-and-Multiple-Parsers">String Parsers and Multiple Parsers</a>). <br><dt><code>int check_required (default = 1)</code><dd><a name="index-check_005frequired-110"></a>tells whether the check for required options must be performed or not. <br><dt><code>int check_ambiguity (default = 0)</code><dd><a name="index-check_005fambiguity-111"></a>tells whether the check whether options that are parsed are not already present in the passed args_info struct; this is performed only for NON multiple options (see also <a href="#Multiple-Options">Multiple Options</a>). <br><dt><code>int print_errors (default = 1)</code><dd><a name="index-print_005ferrors-112"></a>tells<a rel="footnote" href="#fn-33" name="fnd-33"><sup>33</sup></a> whether <code>getopt_long</code> must print error messages to the standard error stream if it encounters an unknown option character or an option with a missing required argument. This is the default behavior. If you set this variable to zero, <code>getopt_long</code> does not print any messages, but the generated parser will still return with error. </dl> <p>Gengetopt also generates an initialization function for such structures<a rel="footnote" href="#fn-34" name="fnd-34"><sup>34</sup></a>, called <code><cmd_parser_name>_params_init</code>, which takes as argument a pointer to such structure and initialize all its fields to their default values; it also generates a function called <code><cmd_parser_name>_params_create</code> that returns a dynamically allocated structure with all fields initialized to their default values. <p>We strongly advise to use such functions for creating and initializing such a structure, since this will make your code scalable to future releases of gengetopt where such structure might contain additional fields. Otherwise, you might risk to use a structure where some fields are not initialized, with unpredictable results. Furthermore, since the <code><cmd_parser_name>_params_create</code> function returns a pointer to a dynamically allocated structure (with <code>malloc</code>), it is up to you to deallocate that structure when you no longer need it (with <code>free</code>). <p>Some examples of usage of this parameters struct are shown in <a href="#Configuration-files">Configuration files</a>. <div class="node"> <a name="Configuration-files"></a> <p><hr> Next: <a rel="next" accesskey="n" href="#Multiple-Options">Multiple Options</a>, Previous: <a rel="previous" accesskey="p" href="#Parser-function-additional-parameters">Parser function additional parameters</a>, Up: <a rel="up" accesskey="u" href="#Top">Top</a> </div> <h2 class="chapter">9 Configuration files</h2> <p><a name="index-configuration-files-113"></a> <a name="index-g_t_0040code_007b_002dC_002c_002d_002dconf_002dparser_007d-114"></a>It is often useful to specify command line options directly in a configuration file, so that the value of some options are read from this file if they are not given as command line options. When the command line option <code>-C|--conf-parser</code> is given to gengetopt, apart from the standard command line option parser, also this additional parser is generated (its name is <code><cmd_parser_name>_config_file</code><a rel="footnote" href="#fn-35" name="fnd-35"><sup>35</sup></a>): <p><a name="index-g_t_003ccmd_005fparser_005fname_003e_005fconfig_005ffile-115"></a> <pre class="example"> int <cmd_parser_name>_config_file(char * const filename, struct gengetopt_args_info *args_info, struct <cmd_parser_name>_params *params); </pre> <p>The parameter structure <code><cmd_parser_name>_params</code> is described in <a href="#Parser-function-additional-parameters">Parser function additional parameters</a>. For instance, <code>params->override</code> tells whether the values read in the configuration file have to override those specified at the command line. <p>IMPORTANT: you have to explicitly set <code>params->initialize</code> to 1 if you call the config file parser before the standard command line option parser, otherwise unpredictable results may show. <p>If you call the config file parser before the standard command line option parser and then you want to call the standard command line parser you MUST use this second version of the parser function, with <code>params->initialize</code> set to 0, so that collected values from the config file are not lost<a rel="footnote" href="#fn-36" name="fnd-36"><sup>36</sup></a>: <p><a name="index-g_t_003ccmd_005fparser_005fname_003e_005fext-116"></a> <pre class="example"> int <cmd_parser_name>_ext (int argc, char **argv, struct gengetopt_args_info *args_info, struct <cmd_parser_name>_params *params); </pre> <p>Notice, that with this version you can also specify whether the options passed at the command line must override the ones read from the config file. <!-- If you set @code{override} to 0, and a command line option is --> <!-- given that has already been read from the config file, an error will be --> <!-- raised. --> Moreover, you have to specify whether the check for missing required options must be performed or not. This concerns also options of a required group (<a href="#Group-options">Group options</a>). <p>If you decide not to request the check for required option, you can test it manually, after the command line parsing returns by using the following generated function: <p><a name="index-g_t_003ccmd_005fparser_005fname_003e_005frequired-117"></a> <pre class="example"> int <cmd_parser_name>_required (struct gengetopt_args_info *args_info, const char *program_name); </pre> <p>where <code>program_name</code> is the name of your executable (usually you should pass <code>argv[0]</code> as argument). If the function returns a value different from 0, then some required options are missing. An error has already been printed by this function. This concerns also options of a required group (<a href="#Group-options">Group options</a>). <p>The config file has the following simple syntax: lines starting with <code>#</code> are considered comments and: <pre class="example"> <option_name> = {<option_val>} </pre> <p class="noindent">or simply (if the option does not take an argument): <pre class="example"> <option_name> </pre> <p class="noindent">which means that <code>option_name</code> is given, and if it accepts an argument, then its value is <code>option_val</code>. The <code>=</code> is not mandatory. <p><a name="index-include-118"></a>Since version 2.19, it is possible to include other files (i.e., other configuration files) in a configuration file, by using the <code>include</code> syntax: <pre class="example"> include "filename" </pre> <p>For instance here's a program that uses this feature (this is the test <samp><span class="file">test_conf_parser</span></samp>): <!-- Generator: GNU source-highlight, by Lorenzo Bettini, http://www.gnu.org/software/src-highlite --> <pre class="example"> <i>/* test_conf_parser.c test */</i> <i>/* test all kinds of options and the conf file parser */</i> <b>#include</b> <tt><stdlib.h></tt> <b>#include</b> <tt><stdio.h></tt> <b>#include</b> <tt>"test_conf_parser_cmd.h"</tt> <b>static</b> <b>struct</b> my_args_info args_info; <b>int</b> main (<b>int</b> argc, <b>char</b> **argv) { <b>unsigned</b> <b>int</b> i; <b>int</b> result = 0; <b>struct</b> test_conf_parser_cmd_parser_params *params; <i>/* initialize the parameters structure */</i> params = test_conf_parser_cmd_parser_params_create(); <i>/* call the command line parser */</i> <b>if</b> (test_conf_parser_cmd_parser (argc, argv, &args_info) != 0) { result = 1; <b>goto</b> stop; } <i>/* </i> <i> override command line options,</i> <i> but do not initialize args_info, check for required options.</i> <i> NOTICE: we must NOT skip the 0 assignment to initialize,</i> <i> since its default value is 1 and override defaults to 0</i> <i> while check_required is already set to its default value, 1</i> <i> */</i> params->initialize = 0; params->override = 1; <i>/* call the config file parser */</i> <b>if</b> (test_conf_parser_cmd_parser_config_file (args_info.conf_file_arg, &args_info, params) != 0) { result = 1; <b>goto</b> stop; } printf (<tt>"value of required: %s</tt>\n<tt>"</tt>, args_info.required_arg); printf (<tt>"value of string: %s</tt>\n<tt>"</tt>, args_info.string_arg); printf (<tt>"value of no-short_given: %d</tt>\n<tt>"</tt>, args_info.no_short_given); printf (<tt>"value of int: %d</tt>\n<tt>"</tt>, args_info.int_arg); printf (<tt>"value of float: %f</tt>\n<tt>"</tt>, args_info.float_arg); printf (<tt>"value of multi-string_given: %d</tt>\n<tt>"</tt>, args_info.multi_string_given); <b>for</b> (i = 0; i < args_info.multi_string_given; i++) printf (<tt>" value of multi-string: %s</tt>\n<tt>"</tt>, args_info.multi_string_arg [i]); printf (<tt>"value of multi-string-def_given: %d</tt>\n<tt>"</tt>, args_info.multi_string_def_given); <b>for</b> (i = 0; i < args_info.multi_string_def_given; ++i) printf (<tt>" value of multi-string-def: %s</tt>\n<tt>"</tt>, args_info.multi_string_def_arg [i]); <b>if</b> (!args_info.multi_string_def_given && args_info.multi_string_def_arg [0]) printf (<tt>"default value of multi-string-def: %s</tt>\n<tt>"</tt>, args_info.multi_string_def_arg [0]); printf (<tt>"value of opta: %s</tt>\n<tt>"</tt>, args_info.opta_arg); printf (<tt>"noarg given %d times</tt>\n<tt>"</tt>, args_info.noarg_given); printf (<tt>"noarg_noshort given %d times</tt>\n<tt>"</tt>, args_info.noarg_noshort_given); printf (<tt>"opt-arg given: %d</tt>\n<tt>"</tt>, args_info.opt_arg_given); printf (<tt>"opt-arg value: %s</tt>\n<tt>"</tt>, (args_info.opt_arg_arg ? args_info.opt_arg_arg : <tt>"not given"</tt>)); <b>if</b> (args_info.file_save_given) { <b>if</b> (test_conf_parser_cmd_parser_file_save (args_info.file_save_arg, &args_info) == EXIT_FAILURE) result = 1; <b>else</b> printf (<tt>"saved configuration file %s</tt>\n<tt>"</tt>, args_info.file_save_arg); } <b> stop:</b> <i>/* deallocate structures */</i> test_conf_parser_cmd_parser_free (&args_info); free (params); <b>return</b> result; } </pre> <p>So if we use the following config file <pre class="example"> # required option required "this is a test" float 3.14 no-short string another </pre> <p class="noindent">and we run <code>test_conf_parser</code> like that, we will have <pre class="example"> ./test_conf_parser -r bar -i 100 --float 2.14 --conf-file test_conf.conf value of required: this is a test value of string: another value of no-short: 1 value of int: 100 value of float: 3.140000 </pre> <p>If, instead we call the <code>test_conf_parser_cmd_parser_configfile</code> with 0 for override argument, we get the following result <pre class="example"> value of required: bar value of string: another value of no-short: 1 value of int: 100 value of float: 2.140000 </pre> <p>This second example use the second version of the command line parser: first call the configuration file parser and then the command line parser (the command line options will override the configuration file options): <!-- Generator: GNU source-highlight, by Lorenzo Bettini, http://www.gnu.org/software/src-highlite --> <pre class="example"> <i>/* test_conf_parser_ov2.c test */</i> <i>/* test all kinds of options and the conf file parser */</i> <i>/* differently from test_conf_parser_ov.c, first scan the conf file and</i> <i> then the command line */</i> <b>#include</b> <tt><stdlib.h></tt> <b>#include</b> <tt><stdio.h></tt> <b>#include</b> <tt>"test_conf_parser_cmd.h"</tt> <b>static</b> <b>struct</b> my_args_info args_info; <b>int</b> main (<b>int</b> argc, <b>char</b> **argv) { <b>struct</b> test_conf_parser_cmd_parser_params *params; <i>/* initialize the parameters structure */</i> params = test_conf_parser_cmd_parser_params_create(); <i>/* </i> <i> initialize args_info, but don't check for required options</i> <i> NOTICE: the other fields are initialized to their default values</i> <i> */</i> params->check_required = 0; <i>/* call the config file parser */</i> <b>if</b> (test_conf_parser_cmd_parser_config_file (<tt>"../../tests/test_conf2.conf"</tt>, &args_info, params) != 0) exit(1); <i>/* </i> <i> override config file options,</i> <i> do not initialize args_info, check for required options.</i> <i> */</i> params->initialize = 0; params->override = 1; params->check_required = 1; <i>/* call the command line parser */</i> <b>if</b> (test_conf_parser_cmd_parser_ext (argc, argv, &args_info, params) != 0) exit(1) ; printf (<tt>"value of required: %s</tt>\n<tt>"</tt>, args_info.required_arg); printf (<tt>"value of string: %s</tt>\n<tt>"</tt>, args_info.string_arg); printf (<tt>"value of no-short_given: %d</tt>\n<tt>"</tt>, args_info.no_short_given); printf (<tt>"value of int: %d</tt>\n<tt>"</tt>, args_info.int_arg); printf (<tt>"value of float: %f</tt>\n<tt>"</tt>, args_info.float_arg); <i>/* release memory */</i> test_conf_parser_cmd_parser_free (&args_info); free (params); <b>return</b> 0; } </pre> <p class="noindent">This is an invocation and its results: <pre class="example"> ./test_conf_parser_ov2 -r "bar" --float 2.14 -i 100 value of required: bar value of string: another value of no-short: 1 value of int: 100 value of float: 2.140000 </pre> <p>If on the above code you substitute <code>params->override = 1</code> with <code>params->check_ambiguity = 1</code> (see the test file <samp><span class="file">test_conf_parser_ov4.c</span></samp>), then the following invocation will generate an error: <pre class="example"> ./test_conf_parser_ov4 -r "bar" -i 100 ./test_conf_parser_ov4: `--required' (`-r') option given more than once </pre> <p class="noindent">since the <code>-r</code> option is specified both in the configuration file and at the command line. <h3 class="section">9.1 Further details on the configuration file parser</h3> <p>The generated config file parser function uses the constant <code>CONFIG_FILE_LINE_SIZE</code> to read each line of the configuration file. By default this constant is set to <code>2048</code> that should be enough for most applications. If your application uses configuration files with lines that are longer, you can compile the generated C file by specifying an explicit value for this constant with the <code>-D</code> command line option of <code>gcc</code>. <div class="node"> <a name="Multiple-Options"></a> <p><hr> Next: <a rel="next" accesskey="n" href="#String-Parsers-and-Multiple-Parsers">String Parsers and Multiple Parsers</a>, Previous: <a rel="previous" accesskey="p" href="#Configuration-files">Configuration files</a>, Up: <a rel="up" accesskey="u" href="#Top">Top</a> </div> <h2 class="chapter">10 Multiple Options</h2> <p><a name="index-multiple-options-119"></a> If an option is specified as <code>multiple</code>, then it can be specified multiple times at command line. In this case, say the option is called <code>foo</code>, the generated <code>foo_given</code> field in the args structure contains the number of times it was specified and the generated field <code>foo_arg</code> is an array containing all the values that were specified for this option. <p>Notice that if a default value is specified for a multiple option, that value is assigned to the option only if no other value is specified on the command line, i.e., a default value IS NOT always part of the values of a multiple option. <p>As in the case for standard options, if a multiple option has a default value, and this is set because no value was specified on the command line, then the corresponding <code><option>_given</code> will still be initialized to 0. Thus, <code><option>_given</code> will effectively inform you if the user has specified that command line option. <p>If it is known that a multiple option has a default value, then it can be safely assumed that the first element of generated array <code><option>_arg</code> is always set. <p>For instance, if the gengetopt file is as follows <pre class="example"> # test options that can be given more than once option "string" s "string option" string optional multiple option "int" i "int option" int optional multiple </pre> <p class="noindent">Then the command line options can be collected like that <pre class="example"> </pre> <p class="noindent">Then if this program is called with the following command line options <!-- Generator: GNU source-highlight, by Lorenzo Bettini, http://www.gnu.org/software/src-highlite --> <pre class="example"> <i>/* test options that can be given more than once */</i> <b>#include</b> <tt><stdlib.h></tt> <b>#include</b> <tt><stdio.h></tt> <b>#include</b> <tt>"test_multiple_cmd.h"</tt> <b>static</b> <b>struct</b> gengetopt_args_info args_info; <b>int</b> main (<b>int</b> argc, <b>char</b> **argv) { <b>int</b> i = 0; <b>if</b> (test_multiple_cmd_parser (argc, argv, &args_info) != 0) exit(1) ; <b>for</b> (i = 0; i < args_info.string_given; ++i) printf (<tt>"passed string: %s</tt>\n<tt>"</tt>, args_info.string_arg[i]); <b>for</b> (i = 0; i < args_info.int_given; ++i) printf (<tt>"passed int: %d</tt>\n<tt>"</tt>, args_info.int_arg[i]); <b>return</b> 0; } </pre> <p class="noindent">The output of the program will be <pre class="example"> passed string: world passed string: hello passed string: bar passed string: foo passed int: 200 passed int: 100 </pre> <p>You can also pass arguments to a multiple option separated by commas (if you need to actually specify the comma operator as part of the argument you can escape it with <code>\</code>), as in the following: <pre class="example"> ./test_multiple -s"foo","bar","hello" -i100,200 -s "world" </pre> <p>You can specify the number of occurrences of multiple options by using the following syntax (that must be given after the <code>multiple</code> keyword): <dl> <dt><code>(number)</code><dd>requires that the multiple option, if given, must be given exactly <code>number</code> times <br><dt><code>(number1-number2)</code><dd>requires that the multiple option, if given, must be given not less than <code>number1</code> times and no more than <code>number2</code> times <br><dt><code>(number-)</code><dd>requires that the multiple option, if given, must be given at least <code>number</code> times <br><dt><code>(-number)</code><dd>requires that the multiple option, if given, must be given at most <code>number</code> times </dl> <p>Here are some examples: <pre class="example"> option "string" s "string option" string optional multiple(4) </pre> <pre class="example"> option "string" s "string option" string optional multiple(1-4) </pre> <pre class="example"> option "string" s "string option" string optional multiple(-5) </pre> <p>Notice that this is independent from the <code>required</code> flag. <div class="node"> <a name="String-Parsers-and-Multiple-Parsers"></a> <p><hr> Next: <a rel="next" accesskey="n" href="#No-getopt_005flong">No getopt_long</a>, Previous: <a rel="previous" accesskey="p" href="#Multiple-Options">Multiple Options</a>, Up: <a rel="up" accesskey="u" href="#Top">Top</a> </div> <h2 class="chapter">11 String Parsers and Multiple Parsers</h2> <p><a name="index-multiple-parsers-120"></a><a name="index-string-parsers-121"></a> The parsers generated by gengetopt (indeed the C and header files) are self-contained and different parsers can be linked in the same program, without interferences. This is useful, e.g., in cases where a specific command line option argument has a complex syntax that accepts options itself according to terminology already defined, i.e., the one handled by <code>getopt_long</code>, see <a href="#Terminology">Terminology</a>. Another case when multiple parsers can be useful is when your command behaves differently according to a specific command line option. <p>Obviously there exists only one instance of command line arguments passed to the <code>main</code> function (namely the variables <code>argc</code> and <code>argv</code>) so passing the same arguments to different command line parsers is likely to generate errors: the different command line parsers are likely to have different syntaxes for accepted options. <p><a name="index-g_t_0040code_007b_002dS_002c_002d_002dstring_002dparser_007d-122"></a>For this reason gengetopt can generate parser functions that take a string containing the further options to parse, instead of taking an array. This additional parser will have the parser name and the suffix <code>_string</code>. If you want these additional parsers to be generated you have to pass the command line option <code>-S|--string-parser</code> to gengetopt (see <a href="#Invoking-gengetopt">Invoking gengetopt</a>). The two functions will be: <pre class="example"> int <parser_name>_string (const char *cmdline, struct test_first_cmdline_cmd_struct *args_info, const char *prog_name); int <parser_name>_string_ext (const char *cmdline, struct test_first_cmdline_cmd_struct *args_info, const char *prog_name, struct <cmd_parser_name>_params *params); </pre> <p>The second version<a rel="footnote" href="#fn-37" name="fnd-37"><sup>37</sup></a> allows you to specify more details about the parsing, using the <code><cmd_parser_name>_params</code> structure, shown in <a href="#Parser-function-additional-parameters">Parser function additional parameters</a> (this is the same as for configuration files, thus we refer to that section for the details of the two functions and default values, see <a href="#Configuration-files">Configuration files</a>). <p>Of course, these functions can be used in general to simulate the invocation of a program with specific command line options (stored in the first string argument), or in general to parse options that are all stored in a string (instead of a vector). <p>The first argument of these parsers is a string containing the options to parse (remember that this must respect the option format handled by <code>getopt_long</code>, see <a href="#Terminology">Terminology</a>). The second one is the pointer to the struct that will be filled with passed options and arguments, as usual. The third option is the program name: this will be used when errors have to be printed. This last argument can be null: in this case, the first element of the first string argument is considered the program name. <p>Let's show these functionalities with an example. Consider a program that accepts two command line options (required in this case): <pre class="example"> # test for multiple parsers, this is the main file # test_main_cmdline_cmd.ggo option "first-cmd" F "the first command line to parse" required \ typestr="first command" string multiple option "second-cmd" S "the second command line to parse" required \ typestr="second command" string multiple </pre> <p class="noindent">These two options accept strings as argument that in turn are considered command line arguments, according to specific syntaxes. The first one is: <pre class="example"> # test for multiple parsers, this is the first command line file # test_first_cmdline_cmd.ggostr option "option-a" a "option a of the first command line to parse" optional int option "multi" M \ "multiple option of the first command line to parse" \ optional string multiple </pre> <p class="noindent">and the second one is: <pre class="example"> # test for multiple parsers, this is the second command line file # test_second_cmdline_cmd.ggostr option "option-a" a "option a of the second command line to parse" \ optional string option "option-b" b "option a of the second command line to parse" \ optional string option "my-multi" M \ "multiple option of the second command line to parse" \ optional string multiple </pre> <p>These last two files are processed with gengetopt using the <code>--string-parser</code>. Let's put everything together in this main file: <!-- Generator: GNU source-highlight, by Lorenzo Bettini, http://www.gnu.org/software/src-highlite --> <pre class="example"> <b>#include</b> <tt><stdio.h></tt> <b>#include</b> <tt><stdlib.h></tt> <b>#include</b> <tt>"test_main_cmdline_cmd.h"</tt> <b>#include</b> <tt>"test_first_cmdline_cmd.h"</tt> <b>#include</b> <tt>"test_second_cmdline_cmd.h"</tt> <b>int</b> main(<b>int</b> argc, <b>char</b> **argv) { <b>struct</b> gengetopt_args_info main_args_info; <b>struct</b> test_first_cmdline_cmd_struct first_args_info; <b>struct</b> test_second_cmdline_cmd_struct second_args_info; <b>int</b> exit_code = 0; <b>unsigned</b> <b>int</b> i, j; <b>if</b> (test_main_cmdline_cmd_parser (argc, argv, &main_args_info) != 0) { exit_code = 1; <b>return</b> exit_code; } <b>for</b> (j = 0; j < main_args_info.second_cmd_given; ++j) { printf(<tt>"second cmdline: %s</tt>\n<tt>"</tt>, main_args_info.second_cmd_arg[j]); <b>if</b> (test_second_cmdline_cmd_parser_string (main_args_info.second_cmd_arg[j], &second_args_info, argv[0]) == 0) { <b>if</b> (second_args_info.option_a_given) printf(<tt>" --option-a: %s</tt>\n<tt>"</tt>, second_args_info.option_a_arg); <b>if</b> (second_args_info.option_b_given) printf(<tt>" --option-b: %s</tt>\n<tt>"</tt>, second_args_info.option_b_arg); <b>for</b> (i = 0; i < second_args_info.my_multi_given; ++i) printf(<tt>" --my-multi: %s</tt>\n<tt>"</tt>, second_args_info.my_multi_arg[i]); test_second_cmdline_cmd_parser_free (&second_args_info); } } <b>for</b> (j = 0; j < main_args_info.first_cmd_given; ++j) { printf(<tt>"first cmdline: %s</tt>\n<tt>"</tt>, main_args_info.first_cmd_arg[j]); <b>if</b> (test_first_cmdline_cmd_parser_string (main_args_info.first_cmd_arg[j], &first_args_info, argv[0]) == 0) { <b>if</b> (first_args_info.option_a_given) printf(<tt>" --option-a: %d</tt>\n<tt>"</tt>, first_args_info.option_a_arg); <b>for</b> (i = 0; i < first_args_info.multi_given; ++i) printf(<tt>" --multi: %s</tt>\n<tt>"</tt>, first_args_info.multi_arg[i]); test_first_cmdline_cmd_parser_free (&first_args_info); } } test_main_cmdline_cmd_parser_free (&main_args_info); <b>return</b> exit_code; } </pre> <p>Notice that in the <code>for</code> loops we always free the elements of the argument structures in order to avoid memory leaks. <p>Now if you can run this program as follows (notice that we use the comma separated arguments for multiple option arguments but we escape it with <code>\</code> because otherwise, e.g., 200 and 300 would be intended as further arguments of <code>--first-cmd</code> instead of <code>--multi</code>, see <a href="#Multiple-Options">Multiple Options</a>): <pre class="example"> ./test_multiple_parsers \ --first-cmd="-M400 -a10 --multi 100\,200\,300" \ --second-cmd="-a20 -b10 --my-multi=a\,b\,c\,d\,e\,f" \ -F"-M500 -M600" -S"--my-multi g" second cmdline: -a20 -b10 --my-multi=a,b,c,d,e,f --option-a: 20 --option-b: 10 --my-multi: a --my-multi: b --my-multi: c --my-multi: d --my-multi: e --my-multi: f second cmdline: --my-multi g --my-multi: g first cmdline: -M400 -a10 --multi 100,200,300 --option-a: 10 --multi: 400 --multi: 100 --multi: 200 --multi: 300 first cmdline: -M500 -M600 --multi: 500 --multi: 600 </pre> <div class="node"> <a name="No-getopt_long"></a> <a name="No-getopt_005flong"></a> <p><hr> Next: <a rel="next" accesskey="n" href="#Bugs">Bugs</a>, Previous: <a rel="previous" accesskey="p" href="#String-Parsers-and-Multiple-Parsers">String Parsers and Multiple Parsers</a>, Up: <a rel="up" accesskey="u" href="#Top">Top</a> </div> <h2 class="chapter">12 What if getopt_long is not available?</h2> <p><a name="index-getopt_005flong-123"></a> If you use gengetopt to generate C functions for parsing command line arguments you have to know that these generated functions use <code>getopt_long</code> to actually read the command line and parsing it. This function is typically part of the standard C library, but some implementations may not include it. If you want your program to be portable on several systems, and be compilable with many C compilers, you can rely on one of the following solutions. <ul class="menu"> <li><a accesskey="1" href="#Include-the-getopt_005flong-code-into-the-parser">Include the getopt_long code into the parser</a> <li><a accesskey="2" href="#Use-automake_002fautoconf">Use automake/autoconf</a> </ul> <div class="node"> <a name="Include-the-getopt_long-code-into-the-parser"></a> <a name="Include-the-getopt_005flong-code-into-the-parser"></a> <p><hr> Next: <a rel="next" accesskey="n" href="#Use-automake_002fautoconf">Use automake/autoconf</a>, Previous: <a rel="previous" accesskey="p" href="#No-getopt_005flong">No getopt_long</a>, Up: <a rel="up" accesskey="u" href="#No-getopt_005flong">No getopt_long</a> </div> <h3 class="section">12.1 Include the <code>getopt_long</code> code into the generated parser</h3> <p>Since version 2.17, gengetopt can include into the generated C parser file the code of <code>getopt_long</code>, so that the include code will be used to actually parse the command line arguments, instead of that taken from the C library. <p><a name="index-g_t_0040code_007b_002d_002dinclude_002dgetopt_007d-124"></a>This solution is actually quite easy, since you only need to specify the command line option <code>--include-getopt</code> (see <a href="#Invoking-gengetopt">Invoking gengetopt</a>), but it has two main drawbacks: <ul> <li>The generate C file of the parser will be much bigger; <li>You won't be able to use the latest version of <code>getopt_long</code> of the C library </ul> <p>It is up to you to choose between this and the automake/autoconf based solution. <p>Actually, this solution has the advantage that your program won't behave strangely when used with another implementation of <code>getopt_long</code>. <p>I prefer the automake/autoconf based solution, as described in <a href="#Use-automake_002fautoconf">Use automake/autoconf</a>, in particular the one described in <a href="#Use-Gnulib">Use Gnulib</a>, which is also the one I adopt for gengetopt itself. <div class="node"> <a name="Use-automake%2fautoconf"></a> <a name="Use-automake_002fautoconf"></a> <p><hr> Previous: <a rel="previous" accesskey="p" href="#Include-the-getopt_005flong-code-into-the-parser">Include the getopt_long code into the parser</a>, Up: <a rel="up" accesskey="u" href="#No-getopt_005flong">No getopt_long</a> </div> <h3 class="section">12.2 Use automake/autoconf to check for the existence of <code>getopt_long</code></h3> <p><a name="index-automake-125"></a><a name="index-autoconf-126"></a>Autoconf and Automake are great tools to generate a configure script that automatically checks for the configuration of your system and for possible missing functions required to compile your program. However, in case of detected missing functions, your program must be able to provide a replacement for such functions. In the next sections we describe two mechanisms for including the (possible) missing code for <code>getopt_long</code> and for checking its presence with automake/autoconf. Since version 2.19, gengetopt itself uses the first mechanism. <ul class="menu"> <li><a accesskey="1" href="#Use-Gnulib">Use Gnulib</a> <li><a accesskey="2" href="#Use-getopt_005flong-sources">Use getopt_long sources</a> </ul> <div class="node"> <a name="Use-Gnulib"></a> <p><hr> Next: <a rel="next" accesskey="n" href="#Use-getopt_005flong-sources">Use getopt_long sources</a>, Previous: <a rel="previous" accesskey="p" href="#Use-automake_002fautoconf">Use automake/autoconf</a>, Up: <a rel="up" accesskey="u" href="#Use-automake_002fautoconf">Use automake/autoconf</a> </div> <h4 class="subsection">12.2.1 Use Gnulib</h4> <p><a name="index-gnulib-127"></a>Since version 2.19 I also started to use Gnulib - The GNU Portability Library<a rel="footnote" href="#fn-38" name="fnd-38"><sup>38</sup></a>, “a central location for common GNU code, intended to be shared among GNU packages”. Gnulib provides an easy and smooth way to add to your package sources the sources of functions that you want to check during configure. It will also handle the checks for these functions in the configure script, and in case they're not in your system (or they're present but with some missing features) it compiles their sources into a library (that you will need to link your program to, as illustrated in the following). <p>Once you retrieved gnulib (for the moment it is available only through git, see the home page), you can invoke ‘<samp><span class="samp">gnulib-tool --import</span></samp>’ that will copy source files, create a <samp><span class="file">Makefile.am</span></samp> to build them, generate a file <samp><span class="file">gnulib-comp.m4</span></samp> with Autoconf M4 macro declarations used by <samp><span class="file">configure.ac</span></samp>, and generate a file <samp><span class="file">gnulib-cache.m4</span></samp> containing the cached specification of how Gnulib is used. In particular, you must specify the modules you want to import, and in our case, it is getopt: <pre class="example"> gnulib-tool --import getopt </pre> <p>By default, the source code is copied into <samp><span class="file">lib/</span></samp> and the M4 macros in <samp><span class="file">m4/</span></samp>. You can override these paths by using <code>--source-base=DIRECTORY</code> and <code>--m4-base=DIRECTORY</code>. For instance, gengetopt uses <code>gl</code> and <code>gl/m4</code>, respectively. We will use these directories in the rest of this section. <p>You must ensure Autoconf can find the macro definitions in <samp><span class="file">gnulib-comp.m4</span></samp>. Use the <code>ACLOCAL_AMFLAGS</code> specifier in your top-level <samp><span class="file">Makefile.am</span></samp> file (and the first time you run <code>aclocal</code> you have to use the <code>-I</code> as well); for instance, in the case of gengetopt we have: <pre class="example"> ACLOCAL_AMFLAGS = -I gl/m4 </pre> <p>You are now ready to call the M4 macros in <code>gnulib-comp.m4</code> from <samp><span class="file">configure.ac</span></samp>. The macro <code>gl_EARLY</code> must be called as soon as possible after verifying that the C compiler is working. Typically, this is immediately after <code>AC_PROG_CC</code>, as in: <pre class="example"> ... AC_PROG_CC gl_EARLY ... </pre> <p>The core part of the gnulib checks are done by the macro <code>gl_INIT</code>. Place it further down in the file, typically where you normally check for header files or functions. For example: <pre class="example"> ... # For gnulib. gl_INIT ... </pre> <p><code>gl_INIT</code> will in turn call the macros related with the gnulib functions, be it specific gnulib macros. So there is no need to call those macros yourself when you use the corresponding gnulib modules. <p>You must also make sure that the gnulib library is built. Add the <code>Makefile</code> in the gnulib source base directory to <code>AC_CONFIG_FILES</code>, as in: <pre class="example"> AC_CONFIG_FILES(... gl/Makefile ...) </pre> <p>You must also make sure that <code>make</code> will recurse into the gnulib directory. To achieve this, add the gnulib source base directory to a <code>SUBDIRS</code> Makefile.am statement, as in: <pre class="example"> SUBDIRS = gl </pre> <p>Finally, you have to add compiler and linker flags in the appropriate source directories, so that you can make use of the gnulib library. Since the ‘<samp><span class="samp">getopt</span></samp>’ module copies files into the build directory, <samp><span class="file">top_builddir/gl</span></samp> is needed as well as <samp><span class="file">top_srcdir/gl</span></samp>. For example: <pre class="example"> ... AM_CPPFLAGS = -I$(top_srcdir)/gl -I$(top_builddir)/gl ... LDADD = gl/libgnu.a ... </pre> <p>Don't forget to <code>#include</code> the various header files. In this example, you would need to make sure that ‘<samp><span class="samp">#include "getopt.h"</span></samp>’ is evaluated when compiling all source code files, that want to make use of <code>getopt</code> or <code>getopt_long</code>. If you simply use the files generated by gengetopt, you won't need include this header though, since it is already handled by the generated files. <p>Every now and then, check whether there are updates in the Gnulib modules, and if the modules you use (e.g., <code>getopt</code>) are upgraded, please remember to also update your files, simply by running: <pre class="example"> gnulib-tool --update </pre> <p>We refer to Gnulib documentation for further explanations and features. <div class="node"> <a name="Use-getopt_long-sources"></a> <a name="Use-getopt_005flong-sources"></a> <p><hr> Previous: <a rel="previous" accesskey="p" href="#Use-Gnulib">Use Gnulib</a>, Up: <a rel="up" accesskey="u" href="#Use-automake_002fautoconf">Use automake/autoconf</a> </div> <h4 class="subsection">12.2.2 Use getopt_long sources</h4> <p>NOTICE: this was the procedure used by gengetopt itself up to version 2.18. We suggest now to use the procedure described in <a href="#Use-Gnulib">Use Gnulib</a>, since the files described in the following might not be kept up-to-date. <p><a name="index-automake-128"></a><a name="index-autoconf-129"></a>We provide C files that actually implement <code>getopt_long</code> function: <samp><span class="file">getopt.c</span></samp> <samp><span class="file">getopt1.c</span></samp> and <samp><span class="file">gnugetopt.h</span></samp>. You'll find these files in the <samp><span class="file"><install prefix>/share/gengetopt</span></samp> directory where <samp><span class="file"><install prefix></span></samp> is the one you specified during compilation. If no prefix had been specified, <samp><span class="file">/usr/local</span></samp> is the default. If you downloaded gengetopt in binary form prefix will probably be <samp><span class="file">/usr/local</span></samp> or <samp><span class="file">/usr</span></samp>. <p>You can rename <samp><span class="file">gnugetopt.h</span></samp> to <samp><span class="file">getopt.h</span></samp> and then simply compile these files and link them to the executable of you program. However, if you use automake and autoconf here's a more elegant solution: you should download the file <samp><span class="file">adl_func_getopt_long.m4</span></samp> you find at this site: <p><a href="http://autoconf-archive.cryp.to">http://autoconf-archive.cryp.to</a> <p class="noindent">and add its contents to your <samp><span class="file">acinclude.m4</span></samp>. You can find this macro also in the <samp><span class="file">acinclude.m4</span></samp> in the sources of gengetopt. <p>This macro checks if getopt_long function is in C library; if it is not then it adds <samp><span class="file">getopt.o</span></samp> and <samp><span class="file">getopt1.o</span></samp> to the objects files that will be linked to your executable (<code>LIBOBJS</code>). <p>Then in <samp><span class="file">Makefile.am</span></samp> of your source directory you have to add the contents of <code>LIBOBJS</code> to the <code>LDADD</code> of the program that has to use getopt_long; e.g., if the program <samp><span class="file">foo</span></samp> has to use getopt_long, you have to add the following line <pre class="example"> foo_LDADD = @LIBOBJS@ </pre> <p>Now these files will be compiled and linked to your program only if necessary. <p>Moreover you have to add <samp><span class="file">getopt.c</span></samp> <samp><span class="file">getopt1.c</span></samp> and <samp><span class="file">gnugetopt.h</span></samp> to your distribution. Note that it is not necessary to put these file names among the <code>foo_SOURCES</code> contents), but you have to add <samp><span class="file">gnugetopt.h</span></samp> to <code>EXTRA_DIST</code>: <pre class="example"> EXTRA_DIST = gnugetopt.h </pre> <p>You may want to take a look at gengetopt's <samp><span class="file">configure.in</span></samp> and <samp><span class="file">src/Makefile.am</span></samp>: they both use the techniques described here. <div class="node"> <a name="Bugs"></a> <p><hr> Next: <a rel="next" accesskey="n" href="#Mailing-Lists">Mailing Lists</a>, Previous: <a rel="previous" accesskey="p" href="#No-getopt_005flong">No getopt_long</a>, Up: <a rel="up" accesskey="u" href="#Top">Top</a> </div> <h2 class="chapter">13 Known Bugs and Limitations</h2> <p><a name="index-Known-Bugs-130"></a><a name="index-Limits-131"></a><a name="index-Misfeatures-132"></a> If you find a bug in <samp><span class="command">gengetopt</span></samp>, please send electronic mail to <p><code>bug-gengetopt at gnu dot org</code> <p>Include the version number, which you can find by running ‘<samp><span class="samp">gengetopt --version</span></samp>’<!-- /@w -->. Also include in your message the output that the program produced and the output you expected. <p>If you have other questions, comments or suggestions about <samp><span class="command">gengetopt</span></samp>, contact the author via electronic mail (find the address at <a href="http://www.lorenzobettini.it">http://www.lorenzobettini.it</a>). The author will try to help you out, although he may not have time to fix your problems. <p>The list of to-dos in the <samp><span class="file">TODO</span></samp>. <ul class="menu"> <li><a accesskey="1" href="#Getopt-and-subsequent-calls">Getopt and subsequent calls</a> </ul> <div class="node"> <a name="Getopt-and-subsequent-calls"></a> <p><hr> Previous: <a rel="previous" accesskey="p" href="#Bugs">Bugs</a>, Up: <a rel="up" accesskey="u" href="#Bugs">Bugs</a> </div> <h3 class="section">13.1 Getopt and subsequent calls</h3> <p>It seems that <code>getopt_long</code>, at least the version in the GNU library, if invoked with different <code>argv</code> arrays, might access memory in a bad way leading to crashes or unexpected behaviors. This happens because it keeps pointers to locations of the previous arrays if not initialized each time by setting <code>optind = 0</code><a rel="footnote" href="#fn-39" name="fnd-39"><sup>39</sup></a>. Unfortunately this initialization behavior seems to be part only of the implementation of GNU library and actually it is not documented (you can see it by taking a look into the source of <samp><span class="file">getopt.c</span></samp>); other implementations of <code>getopt_long</code> might not be affected by this problem; alternatively, as reported by a user, <code>optind = 0</code> leads some <code>getopt_long</code> implementations to consider the program name as a command line option (since it is in position 0), which is bad anyway! <p>Probably this is usually not a problem since you usually parse only the command line, thus you only invoke the command line parser only once, and only with one instance of array (i.e., the <code>argv</code> passed to <code>main</code>). However, it can lead to problems when you use advanced features, as in the case of configuration file parsing (see <a href="#Configuration-files">Configuration files</a>) and multiple parsers (see <a href="#String-Parsers-and-Multiple-Parsers">String Parsers and Multiple Parsers</a>). <p>The parser generated by gengetopt checks whether the program name was actually considered a command line option, and in that case it removes it from the collected command line options; thus, this <code>optind</code> issue should not come up anyway. In case you still don't feel comfortable, you can include a correct <code>getopt_long</code> implementation in the generated parser, so that you can be sure you will always use the same implementation of <code>getopt_long</code> (<a href="#Include-the-getopt_005flong-code-into-the-parser">Include the getopt_long code into the parser</a>). <div class="node"> <a name="Mailing-Lists"></a> <p><hr> Next: <a rel="next" accesskey="n" href="#Index">Index</a>, Previous: <a rel="previous" accesskey="p" href="#Bugs">Bugs</a>, Up: <a rel="up" accesskey="u" href="#Top">Top</a> </div> <h2 class="chapter">14 Mailing Lists</h2> <p><a name="index-mailing-list-133"></a> The following mailing lists are available: <p><code>help-gengetopt at gnu dot org</code> <p class="noindent">for generic discussions about the program and for asking for help about it (open mailing list), <a href="http://mail.gnu.org/mailman/listinfo/help-gengetopt">http://mail.gnu.org/mailman/listinfo/help-gengetopt</a> <p><code>info-gengetopt at gnu dot org</code> <p class="noindent">for receiving information about new releases and features (read-only mailing list), <a href="http://mail.gnu.org/mailman/listinfo/info-gengetopt">http://mail.gnu.org/mailman/listinfo/info-gengetopt</a>. <p>If you want to subscribe to a mailing list just go to the URL and follow the instructions, or send me an e-mail and I'll subscribe you. <p>I'll describe new features in new releases also on my blog, at this URL: <p><a href="http://tronprog.blogspot.com/search/label/gengetopt">http://tronprog.blogspot.com/search/label/gengetopt</a> <div class="node"> <a name="Index"></a> <p><hr> Previous: <a rel="previous" accesskey="p" href="#Mailing-Lists">Mailing Lists</a>, Up: <a rel="up" accesskey="u" href="#Top">Top</a> </div> <h2 class="unnumbered">Index</h2> <ul class="index-cp" compact> <li><a href="#index-g_t_0040code_007b_002d_002darg_002dstruct_002dname_007d-75"><code>--arg-struct-name</code></a>: <a href="#Invoking-gengetopt">Invoking gengetopt</a></li> <li><a href="#index-g_t_0040code_007b_002d_002dconf_002dparser_007d-86"><code>--conf-parser</code></a>: <a href="#Invoking-gengetopt">Invoking gengetopt</a></li> <li><a href="#index-g_t_0040code_007b_002d_002ddefault_002doptional_007d-77"><code>--default-optional</code></a>: <a href="#Invoking-gengetopt">Invoking gengetopt</a></li> <li><a href="#index-g_t_0040code_007b_002d_002ddefault_002doptional_007d-46"><code>--default-optional</code></a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-g_t_0040code_007b_002d_002ddetailed_002dhelp_007d-38"><code>--detailed-help</code></a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-g_t_0040code_007b_002d_002dfull_002dhelp_007d-51"><code>--full-help</code></a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-g_t_0040code_007b_002d_002dfunc_002dname_007d-71"><code>--func-name</code></a>: <a href="#Invoking-gengetopt">Invoking gengetopt</a></li> <li><a href="#index-g_t_0040code_007b_002d_002dgen_002dversion_007d-85"><code>--gen-version</code></a>: <a href="#Invoking-gengetopt">Invoking gengetopt</a></li> <li><a href="#index-g_t_0040code_007b_002d_002dheader_002doutput_002ddir_007d-74"><code>--header-output-dir</code></a>: <a href="#Invoking-gengetopt">Invoking gengetopt</a></li> <li><a href="#index-g_t_0040code_007b_002d_002dinclude_002dgetopt_007d-124"><code>--include-getopt</code></a>: <a href="#Include-the-getopt_005flong-code-into-the-parser">Include the getopt_long code into the parser</a></li> <li><a href="#index-g_t_0040code_007b_002d_002dinclude_002dgetopt_007d-88"><code>--include-getopt</code></a>: <a href="#Invoking-gengetopt">Invoking gengetopt</a></li> <li><a href="#index-g_t_0040code_007b_002d_002dlong_002dhelp_007d-76"><code>--long-help</code></a>: <a href="#Invoking-gengetopt">Invoking gengetopt</a></li> <li><a href="#index-g_t_0040code_007b_002d_002dno_002dhandle_002derror_007d-83"><code>--no-handle-error</code></a>: <a href="#Invoking-gengetopt">Invoking gengetopt</a></li> <li><a href="#index-g_t_0040code_007b_002d_002dno_002dhandle_002dhelp_007d-79"><code>--no-handle-help</code></a>: <a href="#Invoking-gengetopt">Invoking gengetopt</a></li> <li><a href="#index-g_t_0040code_007b_002d_002dno_002dhandle_002dversion_007d-80"><code>--no-handle-version</code></a>: <a href="#Invoking-gengetopt">Invoking gengetopt</a></li> <li><a href="#index-g_t_0040code_007b_002d_002dno_002dhelp_007d-81"><code>--no-help</code></a>: <a href="#Invoking-gengetopt">Invoking gengetopt</a></li> <li><a href="#index-g_t_0040code_007b_002d_002dno_002dversion_007d-82"><code>--no-version</code></a>: <a href="#Invoking-gengetopt">Invoking gengetopt</a></li> <li><a href="#index-g_t_0040code_007b_002d_002doutput_002ddir_007d-72"><code>--output-dir</code></a>: <a href="#Invoking-gengetopt">Invoking gengetopt</a></li> <li><a href="#index-g_t_0040code_007b_002d_002dshow_002dfull_002dhelp_007d-90"><code>--show-full-help</code></a>: <a href="#Invoking-gengetopt">Invoking gengetopt</a></li> <li><a href="#index-g_t_0040code_007b_002d_002dshow_002dhelp_007d-89"><code>--show-help</code></a>: <a href="#Invoking-gengetopt">Invoking gengetopt</a></li> <li><a href="#index-g_t_0040code_007b_002d_002dshow_002drequired_007d-84"><code>--show-required</code></a>: <a href="#Invoking-gengetopt">Invoking gengetopt</a></li> <li><a href="#index-g_t_0040code_007b_002d_002dshow_002dversion_007d-91"><code>--show-version</code></a>: <a href="#Invoking-gengetopt">Invoking gengetopt</a></li> <li><a href="#index-g_t_0040code_007b_002d_002dsrc_002doutput_002ddir_007d-73"><code>--src-output-dir</code></a>: <a href="#Invoking-gengetopt">Invoking gengetopt</a></li> <li><a href="#index-g_t_0040code_007b_002d_002dstring_002dparser_007d-87"><code>--string-parser</code></a>: <a href="#Invoking-gengetopt">Invoking gengetopt</a></li> <li><a href="#index-g_t_0040code_007b_002d_002dunamed_002dopts_007d-78"><code>--unamed-opts</code></a>: <a href="#Invoking-gengetopt">Invoking gengetopt</a></li> <li><a href="#index-g_t_0040code_007b_002dC_002c_002d_002dconf_002dparser_007d-114"><code>-C,--conf-parser</code></a>: <a href="#Configuration-files">Configuration files</a></li> <li><a href="#index-g_t_0040code_007b_002dh_002c_002d_002ddetailed_002dhelp_007d-68"><code>-h,--detailed-help</code></a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-g_t_0040code_007b_002dh_002c_002d_002dhelp_007d-54"><code>-h,--help</code></a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-g_t_0040code_007b_002dS_002c_002d_002dstring_002dparser_007d-122"><code>-S,--string-parser</code></a>: <a href="#String-Parsers-and-Multiple-Parsers">String Parsers and Multiple Parsers</a></li> <li><a href="#index-g_t_0040code_007b_002dV_002c_002d_002dversion_007d-55"><code>-V,--version</code></a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-g_t_003ccmd_005fparser_005fname_003e_005fconfig_005ffile-115"><cmd_parser_name>_config_file</a>: <a href="#Configuration-files">Configuration files</a></li> <li><a href="#index-g_t_003ccmd_005fparser_005fname_003e_005fext-116"><cmd_parser_name>_ext</a>: <a href="#Configuration-files">Configuration files</a></li> <li><a href="#index-g_t_003ccmd_005fparser_005fname_003e_005frequired-117"><cmd_parser_name>_required</a>: <a href="#Configuration-files">Configuration files</a></li> <li><a href="#index-argoptional-47">argoptional</a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-args-32">args</a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-argtype-39">argtype</a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-argument_002c-defined-93">argument, defined</a>: <a href="#Terminology">Terminology</a></li> <li><a href="#index-argv-62">argv</a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-Audience-1">Audience</a>: <a href="#Audience">Audience</a></li> <li><a href="#index-autoconf-129">autoconf</a>: <a href="#Use-getopt_005flong-sources">Use getopt_long sources</a></li> <li><a href="#index-autoconf-126">autoconf</a>: <a href="#Use-automake_002fautoconf">Use automake/autoconf</a></li> <li><a href="#index-autoconf-13">autoconf</a>: <a href="#Installation">Installation</a></li> <li><a href="#index-automake-128">automake</a>: <a href="#Use-getopt_005flong-sources">Use getopt_long sources</a></li> <li><a href="#index-automake-125">automake</a>: <a href="#Use-automake_002fautoconf">Use automake/autoconf</a></li> <li><a href="#index-automake-12">automake</a>: <a href="#Installation">Installation</a></li> <li><a href="#index-check_005fambiguity-111">check_ambiguity</a>: <a href="#Parser-function-additional-parameters">Parser function additional parameters</a></li> <li><a href="#index-check_005frequired-110">check_required</a>: <a href="#Parser-function-additional-parameters">Parser function additional parameters</a></li> <li><a href="#index-Conditions-for-copying-Gengetopt-7">Conditions for copying Gengetopt</a>: <a href="#Copying">Copying</a></li> <li><a href="#index-configuration-files-113">configuration files</a>: <a href="#Configuration-files">Configuration files</a></li> <li><a href="#index-Copying-conditions-6">Copying conditions</a>: <a href="#Copying">Copying</a></li> <li><a href="#index-default-42">default</a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-dependon-43">dependon</a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-desc-35">desc</a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-description-31">description</a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-details-37">details</a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-download-9">download</a>: <a href="#Installation">Installation</a></li> <li><a href="#index-enum-103">enum</a>: <a href="#Options-with-enumerated-values">Options with enumerated values</a></li> <li><a href="#index-enumerated-options-101">enumerated options</a>: <a href="#Options-with-enumerated-values">Options with enumerated values</a></li> <li><a href="#index-gengetopt-options-70">gengetopt options</a>: <a href="#Invoking-gengetopt">Invoking gengetopt</a></li> <li><a href="#index-getopt_005flong-123">getopt_long</a>: <a href="#No-getopt_005flong">No getopt_long</a></li> <li><a href="#index-getting-started-with-Gengetopt-22">getting started with Gengetopt</a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-Git-11">Git</a>: <a href="#Installation">Installation</a></li> <li><a href="#index-gnulib-127">gnulib</a>: <a href="#Use-Gnulib">Use Gnulib</a></li> <li><a href="#index-gnulib-19">gnulib</a>: <a href="#Installation">Installation</a></li> <li><a href="#index-group-options-104">group options</a>: <a href="#Group-options">Group options</a></li> <li><a href="#index-hidden-49">hidden</a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-how-to-use-Gengetopt-21">how to use Gengetopt</a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-include-118">include</a>: <a href="#Configuration-files">Configuration files</a></li> <li><a href="#index-initialize-108">initialize</a>: <a href="#Parser-function-additional-parameters">Parser function additional parameters</a></li> <li><a href="#index-installation-8">installation</a>: <a href="#Installation">Installation</a></li> <li><a href="#index-invoking-69">invoking</a>: <a href="#Invoking-gengetopt">Invoking gengetopt</a></li> <li><a href="#index-Known-Bugs-130">Known Bugs</a>: <a href="#Bugs">Bugs</a></li> <li><a href="#index-libtool-14">libtool</a>: <a href="#Installation">Installation</a></li> <li><a href="#index-Limits-131">Limits</a>: <a href="#Bugs">Bugs</a></li> <li><a href="#index-long-33">long</a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-long-option_002c-defined-98">long option, defined</a>: <a href="#Terminology">Terminology</a></li> <li><a href="#index-mailing-list-133">mailing list</a>: <a href="#Mailing-Lists">Mailing Lists</a></li> <li><a href="#index-Misfeatures-132">Misfeatures</a>: <a href="#Bugs">Bugs</a></li> <li><a href="#index-mode-options-105">mode options</a>: <a href="#Mode-options">Mode options</a></li> <li><a href="#index-multiple-48">multiple</a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-multiple-option_002c-defined-99">multiple option, defined</a>: <a href="#Terminology">Terminology</a></li> <li><a href="#index-multiple-options-119">multiple options</a>: <a href="#Multiple-Options">Multiple Options</a></li> <li><a href="#index-multiple-parsers-120">multiple parsers</a>: <a href="#String-Parsers-and-Multiple-Parsers">String Parsers and Multiple Parsers</a></li> <li><a href="#index-on_002foff-53">on/off</a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-option-without-name-61">option without name</a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-option_002c-defined-94">option, defined</a>: <a href="#Terminology">Terminology</a></li> <li><a href="#index-optional-45">optional</a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-override-109">override</a>: <a href="#Parser-function-additional-parameters">Parser function additional parameters</a></li> <li><a href="#index-PACKAGE-25">PACKAGE</a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-package-24">package</a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-PACKAGE-3">PACKAGE</a>: <a href="#Audience">Audience</a></li> <li><a href="#index-PACKAGE_005fNAME-26">PACKAGE_NAME</a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-PACKAGE_005fNAME-4">PACKAGE_NAME</a>: <a href="#Audience">Audience</a></li> <li><a href="#index-parameter-60">parameter</a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-parameter_002c-defined-96">parameter, defined</a>: <a href="#Terminology">Terminology</a></li> <li><a href="#index-parameters-107">parameters</a>: <a href="#Parser-function-additional-parameters">Parser function additional parameters</a></li> <li><a href="#index-parser-parameters-106">parser parameters</a>: <a href="#Parser-function-additional-parameters">Parser function additional parameters</a></li> <li><a href="#index-patching-20">patching</a>: <a href="#Installation">Installation</a></li> <li><a href="#index-print_005ferrors-112">print_errors</a>: <a href="#Parser-function-additional-parameters">Parser function additional parameters</a></li> <li><a href="#index-purpose-29">purpose</a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-required-44">required</a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-required-option_002c-defined-100">required option, defined</a>: <a href="#Terminology">Terminology</a></li> <li><a href="#index-requirements-15">requirements</a>: <a href="#Installation">Installation</a></li> <li><a href="#index-section-58">section</a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-short-34">short</a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-short-option_002c-defined-97">short option, defined</a>: <a href="#Terminology">Terminology</a></li> <li><a href="#index-string-parsers-121">string parsers</a>: <a href="#String-Parsers-and-Multiple-Parsers">String Parsers and Multiple Parsers</a></li> <li><a href="#index-Terminology-92">Terminology</a>: <a href="#Terminology">Terminology</a></li> <li><a href="#index-text-59">text</a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-tutorial-23">tutorial</a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-typestr-40">typestr</a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-usage-30">usage</a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-value_002c-defined-95">value, defined</a>: <a href="#Terminology">Terminology</a></li> <li><a href="#index-values-102">values</a>: <a href="#Options-with-enumerated-values">Options with enumerated values</a></li> <li><a href="#index-values-41">values</a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-VERSION-28">VERSION</a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-version-27">version</a>: <a href="#Basic-Usage">Basic Usage</a></li> <li><a href="#index-VERSION-5">VERSION</a>: <a href="#Audience">Audience</a></li> <li><a href="#index-Who-should-use-Gengetopt-2">Who should use Gengetopt</a>: <a href="#Audience">Audience</a></li> <li><a href="#index-wrapping-36">wrapping</a>: <a href="#Basic-Usage">Basic Usage</a></li> </ul><div class="footnote"> <hr> <a name="texinfo-footnotes-in-document"></a><h4>Footnotes</h4><p class="footnote"><small>[<a name="fn-1" href="#fnd-1">1</a>]</small> Since version 2.22.4 of Gengetopt the CVS repository was dismissed in favor of Git (<a href="http://git-scm.com/">http://git-scm.com/</a>).</p> <p class="footnote"><small>[<a name="fn-2" href="#fnd-2">2</a>]</small> <a href="http://www.gnu.org/software/autoconf">http://www.gnu.org/software/autoconf</a></p> <p class="footnote"><small>[<a name="fn-3" href="#fnd-3">3</a>]</small> <a href="http://www.gnu.org/software/automake">http://www.gnu.org/software/automake</a></p> <p class="footnote"><small>[<a name="fn-4" href="#fnd-4">4</a>]</small> <a href="http://www.gnu.org/software/libtool">http://www.gnu.org/software/libtool</a></p> <p class="footnote"><small>[<a name="fn-5" href="#fnd-5">5</a>]</small> <a href="http://www.gnu.org/software/gnulib">http://www.gnu.org/software/gnulib</a></p> <p class="footnote"><small>[<a name="fn-6" href="#fnd-6">6</a>]</small> Since version 2.19.</p> <p class="footnote"><small>[<a name="fn-7" href="#fnd-7">7</a>]</small> Since version 2.19.</p> <p class="footnote"><small>[<a name="fn-8" href="#fnd-8">8</a>]</small> Since version 2.18</p> <p class="footnote"><small>[<a name="fn-9" href="#fnd-9">9</a>]</small> Since version 2.22.</p> <p class="footnote"><small>[<a name="fn-10" href="#fnd-10">10</a>]</small> Since version 2.22 the type can be specified</p> <p class="footnote"><small>[<a name="fn-11" href="#fnd-11">11</a>]</small> Since version 2.20.</p> <p class="footnote"><small>[<a name="fn-12" href="#fnd-12">12</a>]</small> Since version 2.15.</p> <p class="footnote"><small>[<a name="fn-13" href="#fnd-13">13</a>]</small> Since version 2.16.</p> <p class="footnote"><small>[<a name="fn-14" href="#fnd-14">14</a>]</small> Since version 2.22.</p> <p class="footnote"><small>[<a name="fn-15" href="#fnd-15">15</a>]</small> This is true since version 2.19. Before this version, strings were not allowed to spawn more than one line.</p> <p class="footnote"><small>[<a name="fn-16" href="#fnd-16">16</a>]</small> Since version 2.19.</p> <p class="footnote"><small>[<a name="fn-17" href="#fnd-17">17</a>]</small> This holds since version 2.15: in previous versions the option specifications had to be given in a fixed order.</p> <p class="footnote"><small>[<a name="fn-18" href="#fnd-18">18</a>]</small> Before version 2.22 neither <code>--help</code> was added and you had to handle the help option manually</p> <p class="footnote"><small>[<a name="fn-19" href="#fnd-19">19</a>]</small> Since version 2.16.</p> <p class="footnote"><small>[<a name="fn-20" href="#fnd-20">20</a>]</small> Since version 2.22.</p> <p class="footnote"><small>[<a name="fn-21" href="#fnd-21">21</a>]</small> Since version 2.18.</p> <p class="footnote"><small>[<a name="fn-22" href="#fnd-22">22</a>]</small> as it was up to version 2.22.2 of gengetopt.</p> <p class="footnote"><small>[<a name="fn-23" href="#fnd-23">23</a>]</small> This is taken from the comments in <samp><span class="file">getopt.in.h</span></samp> of gnulib.</p> <p class="footnote"><small>[<a name="fn-24" href="#fnd-24">24</a>]</small> Since version 2.22 this field is of type <code>unsigned int</code> instead of <code>int</code> for uniformity with multiple options.</p> <p class="footnote"><small>[<a name="fn-25" href="#fnd-25">25</a>]</small> The <code><option>_orig</code> was introduced in the release 2.14.</p> <p class="footnote"><small>[<a name="fn-26" href="#fnd-26">26</a>]</small> These strings and the <code><option>_help</code> were introduced in the release 2.17.</p> <p class="footnote"><small>[<a name="fn-27" href="#fnd-27">27</a>]</small> This function was introduced in the release 2.14.</p> <p class="footnote"><small>[<a name="fn-28" href="#fnd-28">28</a>]</small> Introduced in version 2.22, thanks to Papp Gyozo.</p> <p class="footnote"><small>[<a name="fn-29" href="#fnd-29">29</a>]</small> Since version 2.17.</p> <p class="footnote"><small>[<a name="fn-30" href="#fnd-30">30</a>]</small> Since version 2.22.3.</p> <p class="footnote"><small>[<a name="fn-31" href="#fnd-31">31</a>]</small> Since version 2.22.</p> <p class="footnote"><small>[<a name="fn-32" href="#fnd-32">32</a>]</small> Since version 2.21.</p> <p class="footnote"><small>[<a name="fn-33" href="#fnd-33">33</a>]</small> Introduced in version 2.22</p> <p class="footnote"><small>[<a name="fn-34" href="#fnd-34">34</a>]</small> The <code><cmd_parser_name>_params_init</code> was introduced in version 2.21, but it used to initialize all its fields to 0, which does not make much sense, since it's more helpful to have the fields initialized to their default values; in order not to silently break the semantics of previous code, the (void argument) creation function is now called <code><cmd_parser_name>_params_create</code> and <code><cmd_parser_name>_params_init</code> is now a procedure that initializes a passed pointer to the structure. This will make previous code not compilable, since the signature of <code><cmd_parser_name>_params_init</code> has changed; hopefully, this will force the programmer to realize that something has changed. I'm sorry for the (hopefully little) problems this change might imply.</p> <p class="footnote"><small>[<a name="fn-35" href="#fnd-35">35</a>]</small> The previous function <code><cmd_parser_name>_configfile</code> — notice the absence of the <code>_</code> — is deprecated and should be no longer used, since it might be removed in the future releases.</p> <p class="footnote"><small>[<a name="fn-36" href="#fnd-36">36</a>]</small> The previous function <code><cmd_parser_name>2</code> — notice the <code>2</code> — is deprecated and should be no longer used, since it might be removed in the future releases.</p> <p class="footnote"><small>[<a name="fn-37" href="#fnd-37">37</a>]</small> The previous function <code><cmd_parser_name>_string2</code> — notice the <code>2</code> — is deprecated and should be no longer used, since it might be removed in the future releases.</p> <p class="footnote"><small>[<a name="fn-38" href="#fnd-38">38</a>]</small> <a href="http://www.gnu.org/software/gnulib">http://www.gnu.org/software/gnulib</a></p> <p class="footnote"><small>[<a name="fn-39" href="#fnd-39">39</a>]</small> <code>optind</code> is the global variable in getopt implementation that is the index in ARGV of the next element to be scanned. This is used for communication to and from the caller and for communication between successive calls to <code>getopt_long</code>.</p> <hr></div> </body></html>