📄 getopt_long.3
字号:
.\" $NetBSD: getopt_long.3,v 1.3 2000/04/07 00:02:32 wiz Exp $.\".\" Copyright (c) 1988, 1991, 1993.\" The Regents of the University of California. All rights reserved..\".\" Redistribution and use in source and binary forms, with or without.\" modification, are permitted provided that the following conditions.\" are met:.\" 1. Redistributions of source code must retain the above copyright.\" notice, this list of conditions and the following disclaimer..\" 2. Redistributions in binary form must reproduce the above copyright.\" notice, this list of conditions and the following disclaimer in the.\" documentation and/or other materials provided with the distribution..\" 3. All advertising materials mentioning features or use of this software.\" must display the following acknowledgement:.\" This product includes software developed by the University of.\" California, Berkeley and its contributors..\" 4. Neither the name of the University nor the names of its contributors.\" may be used to endorse or promote products derived from this software.\" without specific prior written permission..\".\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION).\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF.\" SUCH DAMAGE..\".\" @(#)getopt.3 8.5 (Berkeley) 4/27/95.\".Dd April 1, 2000.Dt GETOPT_LONG 3.Os .Sh NAME.Nm getopt_long.Nd get long options from command line argument list.Sh LIBRARY.Lb libc.Sh SYNOPSIS.Fd #include <getopt.h>.Ft int.Fn getopt_long "int argc" "char * const *argv" "const char *optstring" "struct options *long options" "int *index".Sh DESCRIPTIONThe.Fn getopt_longfunction is similar to .Xr getopt 3but it accepts options in two forms: words and characters. The.Fn getopt_longfunction provides a superset of of the functionality of .Xr getopt 3 ..Fn getopt_longcan be used in two ways. In the first way, every long option understoodby the program has a corresponding short option, and the optionstructure is only used to translate from long options to shortoptions. When used in this fashion, .Fn getopt_longbehaves identically to .Xr getopt 3 .This is a good way to add long option processing to an existing programwith the minimum of rewriting..PpIn the second mechanism, a long option sets a flag in the .Fa optionstructure passed, or will store a pointer to the command line argumentin the .Fa option structure passed to it for options that take arguments. Additionally,the long option's argument may be specified as a single argument withan equal sign, e.g. .Bd -literalmyprogram --myoption=somevalue.Ed.PpWhen a long option is processed the call to .Fn getopt_longwill return 0. For this reason, long option processing withoutshortcuts is not backwards compatible with .Xr getopt 3 ..PpIt is possible to combine these methods, providing for long optionsprocessing with short option equivalents for some options. Lessfrequently used options would be processed as long options only..Sh USAGE.PpThe .Fn getopt_longcall requires a structure to be initialized describing the longoptions. The structure is:.Bd -literalstruct option { char *name; int has_arg; int *flag; int val;};.Ed.PpThe .Fa namefield should contain the option name without the leading double dash..PpThe .Fa has_argfield should be one of:.Bl -tag -width "optional_argument".It Li no_argumentno argument to the option is expect..It Li required_argumentan argument to the option is required..It Li optional_argumentan argument to the option may be presented..El.PpIf.Fa flagis non-NULL, then the integer pointed to by it will be set to the value in the .Fa valfield. If the .Fa flag field is NULL, then the .Fa valfield will be returned. Setting .Fa flagto NULL and setting.Fa valto the corresponding short option will make this function act justlike.Xr getopt 3 ..Sh EXAMPLE.Bd -literal -compactextern char *optarg;extern int optind;int bflag, ch, fd;int daggerset;/* options descriptor */static struct option longopts[] = { { "buffy", no_argument, 0, 'b' }, { "floride", required_argument, 0, 'f' }, { "daggerset", no_argument, &daggerset, 1 }, { 0, 0, 0, 0 }};bflag = 0;while ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1) switch(ch) { case 'b': bflag = 1; break; case 'f': if ((fd = open(optarg, O_RDONLY, 0)) < 0) { (void)fprintf(stderr, "myname: %s: %s\en", optarg, strerror(errno)); exit(1); } break; case 0: if(daggerset) { fprintf(stderr,"Buffy will put use her dagger to " "apply floride to dracula's teeth\en"); } break; case '?': default: usage();}argc -= optind;argv += optind;.Ed.Sh HISTORYThe.Fn getopt_longfunction first appeared in GNU libiberty. The first NetBSD implementationappeared in 1.5..Sh IMPLEMENTATION DIFFERENCES.PpThis section describes differences to the GNU implementationfound in glibc-2.1.3:.Bl -tag -width "xxx".It Li ohandling of - as first char of option string in presence ofenvironment variable POSIXLY_CORRECT:.Bl -tag -width "NetBSD".It Li GNUignores POSIXLY_CORRECT and returns non-options asarguments to option '\e1'..It Li NetBSDhonors POSIXLY_CORRECT and stops at the first non-option..El.It Li ohandling of :: in options string in presence of POSIXLY_CORRECT:.Bl -tag -width "NetBSD".It Li BothGNU and NetBSD ignore POSIXLY_CORRECT here and take :: tomean the preceding option takes an optional argument..El.It Li oreturn value in case of missing argument if first character(after + or -) in option string is not ':':.Bl -tag -width "NetBSD".It Li GNUreturns '?'.It NetBSDreturns ':' (since NetBSD's getopt does)..El.It Li ohandling of --a in getopt:.Bl -tag -width "NetBSD".It Li GNUparses this as option '-', option 'a'..It Li NetBSDparses this as '--', and returns -1 (ignoring the a). (Because the original getopt does.).El.It Li osetting of optopt for long options with flag != NULL:.Bl -tag -width "NetBSD".It Li GNUsets optopt to val..It Li NetBSDsets optopt to 0 (since val would never be returned)..El.It Li ohandling of -W with W; in option string in getopt (not getopt_long):.Bl -tag -width "NetBSD".It Li GNUcauses a segfault..It Li NetBSDreturns -1, with optind pointing past the argument of -W (as if `-W arg' were `--arg', and thus '--' had been found)..\" How should we treat W; in the option string when called via.\" getopt? Ignore the ';' or treat it as a ':'? Issue a warning?.El.It Li osetting of optarg for long options without an argument that areinvoked via -W (W; in option string):.Bl -tag -width "NetBSD".It Li GNUsets optarg to the option name (the argument of -W)..It Li NetBSDsets optarg to NULL (the argument of the long option)..El.It Li ohandling of -W with an argument that is not (a prefix to) a knownlong option (W; in option string):.Bl -tag -width "NetBSD".It Li GNUreturns -W with optarg set to the unknown option..It Li NetBSDtreats this as an error (unknown option) and returns '?' withoptopt set to 0 and optarg set to NULL (as GNU's man pagedocuments)..El.It Li oThe error messages are different..It Li oNetBSD does not permute the argument vector at the same points inthe calling sequence as GNU does. The aspects normally used bythe caller (ordering after -1 is returned, value of optind relativeto current positions) are the same, though. (We do fewer variableswaps.).El.Sh BUGSThe implementation, can completelely replace.Xr getopt 3 ,but right now we are using separate code.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -