Charles.Forsyth20060303-partialeea662d19 years agoHistory
168 lines · 3.2 KB · Text
RawDownload
.TH ARG 2.SH NAMEarg \- parse program arguments.SH SYNOPSIS.EXinclude "arg.m";arg := load Arg Arg->PATH; init:     fn(argv: list of string);setusage: fn(s: string);usage:    fn();progname: fn(): string;opt:      fn(): int;arg:      fn(): string;earg:     fn(): string;argv:     fn(): list of string;.EE.SH DESCRIPTION.B Argparses a program's argument list in a traditional form,as received from a shell or other program(see.IR command (2)).The list must be passed to.B initto set the state for the other functions..PP.B Argtakes the first argument to be the program name.Subsequent calls to.B prognamereturn it..PPOptions are arguments containing one or more letters preceded by.B \-(dash, hyphen, minus).The list of options endsbefore the first argument that does not begin with a.BR \- .Option lists also endafter an argument.BR \-\- ,to allow programsto accept argumentsthat would otherwise look like options(eg, file names for.IR rm (1)or a pattern for.IR grep (1)).Finally, option lists endbefore an argument.BR \- ,which is traditionally interpreted by some commands as referring to the standard input or output(depending on context)..PPSuccessive calls to.B optreturn option characters in turn; 0 is returned at the end of the list.A program might take a parameter to a given option (eg, an option of the form.BI -f fileor.BI -f " file" \f1).\f0Following a call to.BR opt ,a call to.BR argwill return the rest of the current argument string if not empty,failing that, the next argument string if any,and otherwise.BR nil ..B Eargis like.B argexcept that if there is no argument associatedwith the option, an error message is printed tostandard error, and a "\f5fail:usage\fP"exception raised..B Setusagesets the error message that will be printed inthis case (preceded by.RB ` usage: 'and followed by a newline)..PPThe argument list remaining after the last call to.B optis returned by.BR argv ..SH EXAMPLEThe following Limbo program takes options.BR b ,.B cand.BR f ,where.B ftakes a file name argument..br.ne 2i.PP.EX.ps -1.vs -1.ta \w'12345678'u +\w'12345678'u +\w'12345678'u +\w'12345678'u +\w'12345678'uimplement Prog;include "sys.m";    sys: Sys;include "draw.m";include "arg.m";    arg: Arg;Prog: module{    init: fn(nil: ref Draw->Context, nil: list of string);}; init(nil: ref Draw->Context, args: list of string){    sys = load Sys Sys->PATH;    arg = load Arg Arg->PATH;     bflag := cflag := 0;    file := "";    arg->init(args);    while((c := arg->opt()) != 0)        case c {        'b' => bflag = 1;        'c' => cflag = 1;        'f' => file = arg->arg();        * =>   sys->print("unknown option (%c)\en", c);        }    args = arg->argv();    sys->print("%s %d %d %s\en", arg->progname(), bflag, cflag, file);    for(; args != nil; args = tl args)        sys->print("%s\en", hd args);}.ps +1.vs +1.EE.PPWhen invoked as follows:.IP.B "prog -bc -ffile a b c".PPthe output is:.IP.EXprog 1 1 fileabc.EE.PPand when invoked by:.IP.B "./prog -b -f file -z -- -bc".PPthe output is:.IP.EXunknown option (z)\&./prog 1 0 file-bc.EE.SH SOURCE.B /appl/lib/arg.b.SH SEE ALSO.IR sh (1),.IR mash (1),.IR command (2)