{"id":5110,"date":"2022-12-20T18:37:07","date_gmt":"2022-12-20T21:37:07","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/getopt-man3p\/"},"modified":"2022-12-20T18:37:07","modified_gmt":"2022-12-20T21:37:07","slug":"getopt-man3p","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/getopt-man3p\/","title":{"rendered":"GETOPT (man3p)"},"content":{"rendered":"<h1 align=\"center\">GETOPT<\/h1>\n<p> <a href=\"#PROLOG\">PROLOG<\/a><br \/> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#RETURN VALUE\">RETURN VALUE<\/a><br \/> <a href=\"#ERRORS\">ERRORS<\/a><br \/> <a href=\"#EXAMPLES\">EXAMPLES<\/a><br \/> <a href=\"#APPLICATION USAGE\">APPLICATION USAGE<\/a><br \/> <a href=\"#RATIONALE\">RATIONALE<\/a><br \/> <a href=\"#FUTURE DIRECTIONS\">FUTURE DIRECTIONS<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#COPYRIGHT\">COPYRIGHT<\/a> <\/p>\n<hr>\n<h2>PROLOG <a name=\"PROLOG\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This manual page is part of the POSIX Programmer\u2019s Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.<\/p>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">getopt, optarg, opterr, optind, optopt \u2014 command option parsing<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">#include <unistd.h><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">int getopt(int <i>argc<\/i>, char bodies manpages.csv script_extrae_body.sh script.sh usr const <i>argv<\/i>[], const char *<i>optstring<\/i>); <br \/> extern char *optarg; <br \/> extern int opterr, optind, optopt;<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>getopt<\/i>() function is a command-line parser that shall follow Utility Syntax Guidelines 3, 4, 5, 6, 7, 9, and 10 in the Base Definitions volume of POSIX.1-2017, <i>Section 12.2<\/i>, <i>Utility Syntax Guidelines<\/i>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The parameters <i>argc<\/i> and <i>argv<\/i> are the argument count and argument array as passed to <i>main<\/i>() (see <i>exec<\/i>()). The argument <i>optstring<\/i> is a string of recognized option characters; if a character is followed by a <colon>, the option takes an argument. All option characters allowed by Utility Syntax Guideline 3 are allowed in <i>optstring<\/i>. The implementation may accept other characters as an extension.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The variable <i>optind<\/i> is the index of the next element of the <i>argv<\/i>[] vector to be processed. It shall be initialized to 1 by the system, and <i>getopt<\/i>() shall update it when it finishes with each element of <i>argv<\/i>[]. If the application sets <i>optind<\/i> to zero before calling <i>getopt<\/i>(), the behavior is unspecified. When an element of <i>argv<\/i>[] contains multiple option characters, it is unspecified how <i>getopt<\/i>() determines which options have already been processed.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>getopt<\/i>() function shall return the next option character (if one is found) from <i>argv<\/i> that matches a character in <i>optstring<\/i>, if there is one that matches. If the option takes an argument, <i>getopt<\/i>() shall set the variable <i>optarg<\/i> to point to the option-argument as follows:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"12%\"><\/td>\n<td width=\"3%\">\n<p>1.<\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"83%\">\n<p>If the option was the last character in the string pointed to by an element of <i>argv<\/i>, then <i>optarg<\/i> shall contain the next element of <i>argv<\/i>, and <i>optind<\/i> shall be incremented by 2. If the resulting value of <i>optind<\/i> is greater than <i>argc<\/i>, this indicates a missing option-argument, and <i>getopt<\/i>() shall return an error indication.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"12%\"><\/td>\n<td width=\"3%\">\n<p>2.<\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"83%\">\n<p>Otherwise, <i>optarg<\/i> shall point to the string following the option character in that element of <i>argv<\/i>, and <i>optind<\/i> shall be incremented by 1.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">If, when <i>getopt<\/i>() is called:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"><i>argv<\/i>[optind] is a null pointer <br \/> *<i>argv<\/i>[optind] is not the character \u2212 <i><br \/> argv<\/i>[optind] points to the string &#8220;\u2212&#8221;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>getopt<\/i>() shall return \u22121 without changing <i>optind<\/i>. If:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"><i>argv<\/i>[optind] points to the string &#8220;\u2212\u2212&#8221;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>getopt<\/i>() shall return \u22121 after incrementing <i>optind<\/i>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If <i>getopt<\/i>() encounters an option character that is not contained in <i>optstring<\/i>, it shall return the <question-mark> (<b>\u2019?\u2019<\/b>) character. If it detects a missing option-argument, it shall return the <colon> character (<b>\u2019:\u2019<\/b>) if the first character of <i>optstring<\/i> was a <colon>, or a <question-mark> character (<b>\u2019?\u2019<\/b>) otherwise. In either case, <i>getopt<\/i>() shall set the variable <i>optopt<\/i> to the option character that caused the error. If the application has not set the variable <i>opterr<\/i> to 0 and the first character of <i>optstring<\/i> is not a <colon>, <i>getopt<\/i>() shall also print a diagnostic message to <i>stderr<\/i> in the format specified for the <i>getopts<\/i> utility, unless the <i>stderr<\/i> stream has wide orientation, in which case the behavior is undefined.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>getopt<\/i>() function need not be thread-safe.<\/p>\n<h2>RETURN VALUE <a name=\"RETURN VALUE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>getopt<\/i>() function shall return the next option character specified on the command line.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">A <colon> (<b>\u2019:\u2019<\/b>) shall be returned if <i>getopt<\/i>() detects a missing argument and the first character of <i>optstring<\/i> was a <colon> (<b>\u2019:\u2019<\/b>).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">A <question-mark> (<b>\u2019?\u2019<\/b>) shall be returned if <i>getopt<\/i>() encounters an option character not in <i>optstring<\/i> or detects a missing argument and the first character of <i>optstring<\/i> was not a <colon> (<b>\u2019:\u2019<\/b>).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Otherwise, <i>getopt<\/i>() shall return \u22121 when all command line options are parsed.<\/p>\n<h2>ERRORS <a name=\"ERRORS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">If the application has not set the variable <i>opterr<\/i> to 0, the first character of <i>optstring<\/i> is not a <colon>, and a write error occurs while <i>getopt<\/i>() is printing a diagnostic message to <i>stderr<\/i>, then the error indicator for <i>stderr<\/i> shall be set; but <i>getopt<\/i>() shall still succeed and the value of <i>errno<\/i> after <i>getopt<\/i>() is unspecified.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>The following sections are informative.<\/i><\/p>\n<h2>EXAMPLES <a name=\"EXAMPLES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><i><b>Parsing Command Line Options<\/b><\/i> <br \/> The following code fragment shows how you might process the arguments for a utility that can take the mutually-exclusive options <i>a<\/i> and <i>b<\/i> and the options <i>f<\/i> and <i>o<\/i>, both of which require arguments:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">#include <stdio.h> <br \/> #include <stdlib.h> <br \/> #include <unistd.h><\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">int <br \/> main(int argc, char *argv[ ]) <br \/> { <br \/> int c; <br \/> int bflg = 0, aflg = 0, errflg = 0; <br \/> char *ifile; <br \/> char *ofile; <br \/> . . . <br \/> while ((c = getopt(argc, argv, &#8220;:abf:o:&#8221;)) != -1) { <br \/> switch(c) { <br \/> case &#8216;a&#8217;: <br \/> if (bflg) <br \/> errflg++; <br \/> else <br \/> aflg++; <br \/> break; <br \/> case &#8216;b&#8217;: <br \/> if (aflg) <br \/> errflg++; <br \/> else <br \/> bflg++; <br \/> break; <br \/> case &#8216;f&#8217;: <br \/> ifile = optarg; <br \/> break; <br \/> case &#8216;o&#8217;: <br \/> ofile = optarg; <br \/> break; <br \/> case &#8216;:&#8217;: \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var -f or -o without operand bodies\/ usr\/ <br \/> fprintf(stderr, <br \/> &#8220;Option -%c requires an operandn&#8221;, optopt); <br \/> errflg++; <br \/> break; <br \/> case &#8216;?&#8217;: <br \/> fprintf(stderr, <br \/> &#8220;Unrecognized option: &#8216;-%c&#8217;n&#8221;, optopt); <br \/> errflg++; <br \/> } <br \/> } <br \/> if (errflg) { <br \/> fprintf(stderr, &#8220;usage: . . . &#8220;); <br \/> exit(2); <br \/> } <br \/> for ( ; optind < argc; optind++) { <br \/> if (access(argv[optind], R_OK)) { <br \/> . . . <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This code accepts any of the following as equivalent:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">cmd -ao arg path path <br \/> cmd -a -o arg path path <br \/> cmd -o arg -a path path <br \/> cmd -a -o arg &#8212; path path <br \/> cmd -a -oarg path path <br \/> cmd -aoarg path path<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Selecting Options from the Command Line<\/b> <br \/> The following example selects the type of database routines the user wants to use based on the <i>Options<\/i> argument.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">#include <unistd.h> <br \/> #include <string.h> <br \/> &#8230; <br \/> const char *Options = &#8220;hdbtl&#8221;; <br \/> &#8230; <br \/> int dbtype, c; <br \/> char *st; <br \/> &#8230; <br \/> dbtype = 0; <br \/> while ((c = getopt(argc, argv, Options)) != -1) { <br \/> if ((st = strchr(Options, c)) != NULL) { <br \/> dbtype = st &#8211; Options; <br \/> break; <br \/> } <br \/> }<\/p>\n<h2>APPLICATION USAGE <a name=\"APPLICATION USAGE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>getopt<\/i>() function is only required to support option characters included in Utility Syntax Guideline 3. Many historical implementations of <i>getopt<\/i>() support other characters as options. This is an allowed extension, but applications that use extensions are not maximally portable. Note that support for multi-byte option characters is only possible when such characters can be represented as type <b>int<\/b>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Applications which use wide-character output functions with <i>stderr<\/i> should ensure that any calls to <i>getopt<\/i>() do not write to <i>stderr<\/i>, either by setting <i>opterr<\/i> to 0 or by ensuring the first character of <i>optstring<\/i> is always a <colon>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">While <i>ferror<\/i>(<i>stderr<\/i>) may be used to detect failures to write a diagnostic to <i>stderr<\/i> when <i>getopt<\/i>() returns <b>\u2019?\u2019<\/b>, the value of <i>errno<\/i> is unspecified in such a condition. Applications desiring more control over handling write failures should set <i>opterr<\/i> to 0 and independently perform output to <i>stderr<\/i>, rather than relying on <i>getopt<\/i>() to do the output.<\/p>\n<h2>RATIONALE <a name=\"RATIONALE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>optopt<\/i> variable represents historical practice and allows the application to obtain the identity of the invalid option.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The description has been written to make it clear that <i>getopt<\/i>(), like the <i>getopts<\/i> utility, deals with option-arguments whether separated from the option by <blank> characters or not. Note that the requirements on <i>getopt<\/i>() and <i>getopts<\/i> are more stringent than the Utility Syntax Guidelines.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>getopt<\/i>() function shall return \u22121, rather than EOF, so that <i><stdio.h><\/i> is not required.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The special significance of a <colon> as the first character of <i>optstring<\/i> makes <i>getopt<\/i>() consistent with the <i>getopts<\/i> utility. It allows an application to make a distinction between a missing argument and an incorrect option letter without having to examine the option letter. It is true that a missing argument can only be detected in one case, but that is a case that has to be considered.<\/p>\n<h2>FUTURE DIRECTIONS <a name=\"FUTURE DIRECTIONS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">None.<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>exec<\/i><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The Base Definitions volume of POSIX.1-2017, <i>Section 12.2<\/i>, <i>Utility Syntax Guidelines<\/i>, <b><unistd.h><\/b><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The Shell and Utilities volume of POSIX.1-2017, <i>getopts<\/i><\/p>\n<h2>COPYRIGHT <a name=\"COPYRIGHT\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology &#8212; Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http:\/\/www.opengroup.org\/unix\/online.html .<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https:\/\/www.kernel.org\/doc\/man-pages\/reporting_bugs.html .<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  getopt, optarg, opterr, optind, optopt \u2014 command option parsing <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3779,1],"tags":[2003,1594],"class_list":["post-5110","post","type-post","status-publish","format-standard","hentry","category-3pm-perl-llamadas-de-bibliotecas","category-sin-categoria","tag-getopt","tag-man3p"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/5110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=5110"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/5110\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=5110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=5110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=5110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}