?? subgetopt.3
字號:
.TH subgetopt 3.SH NAMEsubgetopt \- get option character from command line.SH SYNTAX.B #include <subgetopt.h>char *\fBsgoptarg\fP;.brint \fBsgoptind\fP;.brint \fBsgoptpos\fP;.brint \fBsgoptdone\fP;.brint \fBsgoptproblem\fP;int \fBsgopt(\fP\fIargc,argv,opts\fR\fB)\fP;int \fIargc\fR;.brchar **\fIargv\fR;.brchar *\fIopts\fR;.SH DESCRIPTION.B sgoptreturns the next valid command-line option characterfrom.IR argv .Valid option characters are listed in the.I optsstring..I optsmay be empty.A character in.I optsmay be followed by a colon,in which case ittakes an.I option argument\fR.Avoid using the characters ?, :, and \- as option characters.Below.I option argumentis abbreviatedas.I optargand.I command-line argumentis abbreviated as.IR cmdarg .Options are listed in cmdargs which begin witha minus sign.Several options which do not take optargs may be combinedinto one cmdarg.An option which takes an optarg may be handled in two ways.If it appears at the very end of a cmdarg,then the entire next cmdarg is the optarg.But if there are any characters in the cmdargafter the option character,then those characters form the optarg.The optarg is returned in.BR sgoptarg .Next time.B sgoptlooks at the cmdarg which follows the optarg.If a cmdarg does not begin with a hyphen,or if it is a lone hyphen not followed by any characters,or if it begins with two hyphens,then it terminates option processing,and.B sgoptreturns an appropriate code.If there are two hyphens,.B sgoptwill advance attention to the next cmdarg,so it can be called again to read further options..SH "PROPER USAGE".B sgoptproblemshould be used only when.B sgoptreturns ?..B sgoptindand.B sgoptposare defined all the time..B sgoptargis defined all the time;it is null unless.B sgopthas just returned an option with optarg..B sgoptis typically used as follows..EX#include <subgetopt.h>main(argc,argv) int argc; char **argv; { int opt;while ((opt = sgopt(argc,argv,"a:s")) != sgoptdone).br switch(opt) {.br case 'a':.br printf("opt a with optarg %s\\n",sgoptarg); break;.br case 's':.br printf("opt s with no optarg\\n"); break;.br case '?':.br if (argv[sgoptind] && (sgoptind < argc)).br printf("illegal opt %c\\n",sgoptproblem);.br else.br printf("missing arg, opt %c\\n",sgoptproblem);.br exit(1);.br }argv += sgoptind;.brwhile (*argv) printf("argument %s\\n",*argv++);.brexit(0);.br}.EEThe end of the command line ismarked by either.IR argc ,or a null pointer in.IR argv ,whichever comes first.Normallythese two markers coincide,so it is redundantto test forboth.I argv\fB[sgoptind]and.B sgoptind < \fIargc\fR.The above code shows both tests as an illustration..B Multiple option sets:One useful technique is to call.B sgoptwith a primary.I optsuntil it returns EOF,then call.B sgoptwith a secondary.I optsuntil it returns EOF.The user can provide primary options, then a double hyphen,and then secondary options.No special handling is needed if some or all of the options areomitted.The same technique can be used for any number of option setsin series..B Multiple command lines:Before parsing a new.BR argv ,make sure toset.B sgoptindand.B sgoptposback to1 and 0..SH "PARSING STAGES".B sgoptkeeps track of its position in.I argvwith.B sgoptindand.BR sgoptpos ,which are initialized to 1 and 0.It looks at.I argv\fB[sgoptind][sgoptpos]and following characters..B sgoptindicatesthat no more options are available byreturning.BR sgoptdone ,which is initialized to.BR SUBGETOPTDONE ,which is defined as \-1..B sgoptbegins by setting.B optargto null..B Ending conditions:If.I argvis null, or.B sgoptindis larger than.IR argc ,or the current cmdarg.I argv\fB[sgoptind]is null,then.B sgoptreturns.BR optdone ..B Stage one:If the current characteris zero,.B sgoptIt then checks the ending conditions again..B Stage two:Ifthe current position is the begining of the cmdarg,.B sgoptchecks whetherthe current characteris a minus sign.If not it returns.BR optdone .It thenmovesto the next character.If that character is zero,.B sgoptmovesback to the beginning of the cmdarg,and returns.BR sgoptdone .If the character is a minus sign,.B sgoptmoves to the beginning of the next cmdarg,and returns.BR sgoptdone ..B Stage three:.B sgoptrecords the current character,.IR c ,and moves to the next character.There are three possibilities:(1).I cis an option character without optarg in.IR opts ,or(2).I cis an option character with optarg in.IR opts ,or(3).I cdoes not appear in.IR opts .(1)If.I cappears as an option character without optarg in.IR opts ,.B sgoptreturns.IR c .(2)If.I cappears as an option character with optarg in.IR opts ,.B sgoptsets.B sgoptargto the current position,and moves to the next cmdarg.If.B sgoptargis nonempty,.B sgoptreturns.IR c .Then.B sgoptsets.B sgoptargtothe current cmdarg.Ifthe current cmdarg is null,or past.IR argc ,.B sgoptsets.B sgoptproblemto.I cand returns ?.Otherwise.B sgoptmoves to the nextargumentand returns.IR c .(2)If.I cdoes not appear in.IR opts ,.B sgoptsets.B sgoptproblemto.I cand returns ?..SH "SYNTAX NOTE".B sgoptis actually a macro abbreviation for.BR subgetopt .The external.B sgvariables are also macrosfor.BR subget .These macros are defined in.BR <subgetopt.h> ,unless.B SUBGETOPTNOSHORTis definedwhen.B <subgetopt.h>is included..SH VERSIONsubgetopt version 0.9, 931129..SH AUTHORPlaced into the public domain by Daniel J. Bernstein.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -