126 lines · 2.1 KB · Text
RawDownload
.TH ARG 2.SH NAMEARGBEGIN, ARGEND, ARGC, ARGF, EARGF \- process option letters from argv.SH SYNOPSIS.B #include <u.h>.br.B #include <libc.h>.PP.nf.B ARGBEGIN {.B char *ARGF();.B char *EARGF(code);.B Rune ARGC();.B } ARGEND.PP.B extern char *argv0;.SH DESCRIPTIONThese macros assume the names.I argcand.I argvare in scope; see.IR exec (2)..I ARGBEGINand.I ARGENDsurround code for processing program options.The codeshould be the cases of a C switch onoption characters;it is executed once for each option character.Options end after an argument .BR -- ,before an argument.BR - ,or before an argument that doesn't begin with.BR - ..PPThe function macro.I ARGCreturns the current option character, as an integer..PPThe function macro.I ARGFreturns the current option argument:a pointer to the rest of the option string if not empty,or the next argument in.I argvif any, or 0..I ARGFmust be called just once for each optionargument.The macro.I EARGFis like.I ARGFbut instead of returning zeroruns.I codeand, if that returns, calls.IR abort (2).A typical value for.I codeis .BR usage() ,as in.BR EARGF(usage()) ..PPAfter.IR ARGBEGIN ,.I argv0is a copy of.BR argv[0](conventionally the name of the program)..PPAfter.IR ARGEND ,.I argvpoints at a zero-terminated list of the remaining.I argcarguments..SH EXAMPLEThis C program can take option.B band option.BR f ,which requires an argument..IP.EX.ta \w'12345678'u +\w'12345678'u +\w'12345678'u +\w'12345678'u +\w'12345678'u#include <u.h>#include <libc.h>voidmain(int argc, char *argv[]){	char *f;	print("%s", argv[0]);	ARGBEGIN {	case 'b':		print(" -b");		break;	case 'f':		print(" -f(%s)", (f=ARGF())? f: "no arg");		break;	default:		print(" badflag('%c')", ARGC());	} ARGEND	print(" %d args:", argc);	while(*argv)		print(" '%s'", *argv++);	print("\en");	exits(nil);}.EE.PPHere is the output from running the command.Bprog -bffile1 -r -f file2 arg1 arg2.IP.Bprog -b -f(file1) badflag('r') -f(file2) 2 args: 'arg1' 'arg2'.PP.SH SOURCE.B /sys/include/libc.h.SH SEE ALSO.IR getflags (8)