📄 getopt.3
字号:
.\" Copyright (c) 1985 Regents of the University of California..\" All rights reserved. The Berkeley software License Agreement.\" specifies the terms and conditions for redistribution..\".\" @(#)getopt.3 6.4 (Berkeley) 5/27/86.\".TH GETOPT 3 "May 27, 1986".UC 6.SH NAMEgetopt \- get option letter from argv.SH SYNOPSIS.ft Bint getopt(argc, argv, optstring).brint argc;.brchar **argv;.brchar *optstring;.spextern char *optarg;.brextern int optind;.ft.SH DESCRIPTION.I Getoptreturns the next option letter in.I argvthat matches a letter in.IR optstring ..I Optstringis a string of recognized option letters;if a letter is followed by a colon, the option is expected to havean argument that may or may not be separated from it by white space..I Optargis set to point to the start of the option argument on return from.IR getopt ..PP.I Getoptplaces in.I optindthe.I argvindex of the next argument to be processed.Because.I optindis external, it is normally initialized to zero automaticallybefore the first call to .IR getopt ..PPWhen all options have been processed (i.e., up to the firstnon-option argument),.I getoptreturns.BR EOF .The special option.B \-\-may be used to delimit the end of the options;.B EOFwill be returned, and.B \-\-will be skipped..SH DIAGNOSTICS.I Getoptprints an error message on.I stderrand returns a question mark.RB ( ? )when it encounters an option letter not included in.IR optstring ..SH EXAMPLEThe following code fragment shows how one might process the argumentsfor a command that can take the mutually exclusive options.B aand.BR b ,and the options.B fand.BR o ,both of which require arguments:.PP.RS.nfmain(argc, argv)int argc;char **argv;{ int c; extern int optind; extern char *optarg; \&. \&. \&. while ((c = getopt(argc, argv, "abf:o:")) != EOF) switch (c) { case `a': if (bflg) errflg++; else aflg++; break; case `b': if (aflg) errflg++; else bproc(); break; case `f': ifile = optarg; break; case `o': ofile = optarg; break; case `?': default: errflg++; break; } if (errflg) { fprintf(stderr, "Usage: ..."); exit(2); } for (; optind < argc; optind++) { \&. \&. \&. } \&. \&. \&.}.RE.SH HISTORYWritten by Henry Spencer, working from a Bell Labs manual page.Modified by Keith Bostic to behave more like the System V version..SH BUGSIt is not obvious how`\-'standing alone should be treated; this version treats it asa non-option argument, which is not always right..PPOption arguments are allowed to begin with `\-';this is reasonable but reduces the amount of error checking possible..PP.I Getoptis quite flexible but the obvious price must be paid: there is muchit could do that it doesn't, likechecking mutually exclusive options, checking type ofoption arguments, etc.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -