blob: 805e6615c222b349f218aa79d40b4aa11e1618e8 [file] [log] [blame]
<html lang="en">
<head>
<title>Getopt Long Options - The GNU C Library</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="The GNU C Library">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Getopt.html#Getopt" title="Getopt">
<link rel="prev" href="Example-of-Getopt.html#Example-of-Getopt" title="Example of Getopt">
<link rel="next" href="Getopt-Long-Option-Example.html#Getopt-Long-Option-Example" title="Getopt Long Option Example">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<!--
This file documents the GNU C library.
This is Edition 0.12, last updated 2007-10-27,
of `The GNU C Library Reference Manual', for version
2.8 (Sourcery G++ Lite 2011.03-41).
Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002,
2003, 2007, 2008, 2010 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.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being ``Free Software Needs Free Documentation''
and ``GNU Lesser General Public License'', 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 the freedom to
copy and modify this GNU manual. Buying copies from the FSF
supports it in developing GNU and promoting software freedom.''-->
<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>
<link rel="stylesheet" type="text/css" href="../cs.css">
</head>
<body>
<div class="node">
<a name="Getopt-Long-Options"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="Getopt-Long-Option-Example.html#Getopt-Long-Option-Example">Getopt Long Option Example</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Example-of-Getopt.html#Example-of-Getopt">Example of Getopt</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Getopt.html#Getopt">Getopt</a>
<hr>
</div>
<h4 class="subsection">25.2.3 Parsing Long Options with <code>getopt_long</code></h4>
<p>To accept GNU-style long options as well as single-character options,
use <code>getopt_long</code> instead of <code>getopt</code>. This function is
declared in <samp><span class="file">getopt.h</span></samp>, not <samp><span class="file">unistd.h</span></samp>. You should make every
program accept long options if it uses any options, for this takes
little extra work and helps beginners remember how to use the program.
<!-- getopt.h -->
<!-- GNU -->
<div class="defun">
&mdash; Data Type: <b>struct option</b><var><a name="index-struct-option-3029"></a></var><br>
<blockquote><p>This structure describes a single long option name for the sake of
<code>getopt_long</code>. The argument <var>longopts</var> must be an array of
these structures, one for each long option. Terminate the array with an
element containing all zeros.
<p>The <code>struct option</code> structure has these fields:
<dl>
<dt><code>const char *name</code><dd>This field is the name of the option. It is a string.
<br><dt><code>int has_arg</code><dd>This field says whether the option takes an argument. It is an integer,
and there are three legitimate values: <code>no_argument</code><!-- /@w -->,
<code>required_argument</code> and <code>optional_argument</code>.
<br><dt><code>int *flag</code><dt><code>int val</code><dd>These fields control how to report or act on the option when it occurs.
<p>If <code>flag</code> is a null pointer, then the <code>val</code> is a value which
identifies this option. Often these values are chosen to uniquely
identify particular long options.
<p>If <code>flag</code> is not a null pointer, it should be the address of an
<code>int</code> variable which is the flag for this option. The value in
<code>val</code> is the value to store in the flag to indicate that the option
was seen.
</dl>
</p></blockquote></div>
<!-- getopt.h -->
<!-- GNU -->
<div class="defun">
&mdash; Function: int <b>getopt_long</b> (<var>int argc, char *const *argv, const char *shortopts, const struct option *longopts, int *indexptr</var>)<var><a name="index-getopt_005flong-3030"></a></var><br>
<blockquote><p>Decode options from the vector <var>argv</var> (whose length is <var>argc</var>).
The argument <var>shortopts</var> describes the short options to accept, just as
it does in <code>getopt</code>. The argument <var>longopts</var> describes the long
options to accept (see above).
<p>When <code>getopt_long</code> encounters a short option, it does the same
thing that <code>getopt</code> would do: it returns the character code for the
option, and stores the options argument (if it has one) in <code>optarg</code>.
<p>When <code>getopt_long</code> encounters a long option, it takes actions based
on the <code>flag</code> and <code>val</code> fields of the definition of that
option.
<p>If <code>flag</code> is a null pointer, then <code>getopt_long</code> returns the
contents of <code>val</code> to indicate which option it found. You should
arrange distinct values in the <code>val</code> field for options with
different meanings, so you can decode these values after
<code>getopt_long</code> returns. If the long option is equivalent to a short
option, you can use the short option's character code in <code>val</code>.
<p>If <code>flag</code> is not a null pointer, that means this option should just
set a flag in the program. The flag is a variable of type <code>int</code>
that you define. Put the address of the flag in the <code>flag</code> field.
Put in the <code>val</code> field the value you would like this option to
store in the flag. In this case, <code>getopt_long</code> returns <code>0</code>.
<p>For any long option, <code>getopt_long</code> tells you the index in the array
<var>longopts</var> of the options definition, by storing it into
<code>*</code><var>indexptr</var>. You can get the name of the option with
<var>longopts</var><code>[*</code><var>indexptr</var><code>].name</code>. So you can distinguish among
long options either by the values in their <code>val</code> fields or by their
indices. You can also distinguish in this way among long options that
set flags.
<p>When a long option has an argument, <code>getopt_long</code> puts the argument
value in the variable <code>optarg</code> before returning. When the option
has no argument, the value in <code>optarg</code> is a null pointer. This is
how you can tell whether an optional argument was supplied.
<p>When <code>getopt_long</code> has no more options to handle, it returns
<code>-1</code>, and leaves in the variable <code>optind</code> the index in
<var>argv</var> of the next remaining argument.
</p></blockquote></div>
<p>Since long option names were used before the <code>getopt_long</code>
options was invented there are program interfaces which require programs
to recognize options like &lsquo;<samp><span class="samp">-option&nbsp;value</span></samp>&rsquo;<!-- /@w --> instead of
&lsquo;<samp><span class="samp">--option&nbsp;value</span></samp>&rsquo;<!-- /@w -->. To enable these programs to use the GNU
getopt functionality there is one more function available.
<!-- getopt.h -->
<!-- GNU -->
<div class="defun">
&mdash; Function: int <b>getopt_long_only</b> (<var>int argc, char *const *argv, const char *shortopts, const struct option *longopts, int *indexptr</var>)<var><a name="index-getopt_005flong_005fonly-3031"></a></var><br>
<blockquote>
<p>The <code>getopt_long_only</code> function is equivalent to the
<code>getopt_long</code> function but it allows to specify the user of the
application to pass long options with only &lsquo;<samp><span class="samp">-</span></samp>&rsquo; instead of
&lsquo;<samp><span class="samp">--</span></samp>&rsquo;. The &lsquo;<samp><span class="samp">--</span></samp>&rsquo; prefix is still recognized but instead of
looking through the short options if a &lsquo;<samp><span class="samp">-</span></samp>&rsquo; is seen it is first
tried whether this parameter names a long option. If not, it is parsed
as a short option.
<p>Assuming <code>getopt_long_only</code> is used starting an application with
<pre class="smallexample"> app -foo
</pre>
<p class="noindent">the <code>getopt_long_only</code> will first look for a long option named
&lsquo;<samp><span class="samp">foo</span></samp>&rsquo;. If this is not found, the short options &lsquo;<samp><span class="samp">f</span></samp>&rsquo;, &lsquo;<samp><span class="samp">o</span></samp>&rsquo;,
and again &lsquo;<samp><span class="samp">o</span></samp>&rsquo; are recognized.
</p></blockquote></div>
</body></html>