Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ (2) — Plan9 1st Edition

Media Vault

Software Library

Restoration Projects

Artifacts Sought

ARG(2)

NAME

ARG − process option letters from argv

SYNOPSIS

­#include <libc.h>
­ARGBEGIN {
­} ARGEND
­char ∗ARGF();
­Rune ARGC();

­extern char ∗argv0;

DESCRIPTION

Command-line arguments to programs (see exec(2)) are conventionally arranged as a set of ­options — strings beginning with a ­- (minus sign) — followed by plain arguments such as file names.  The ­ARG macros provide a convenient means for processing these options.  Option characters appear in nonempty clusters preceded by -.  After options processing is terminated (by an argument not starting with -, or by the argument ­-- which is then skipped, or by the argument -), execution resumes after the ARGEND. 

The body of a ­switch statement should be put between ­ARGBEGIN{ and }ARGEND; it is executed once for each option character (the character itself may be referenced by ARGC()).  If an option takes a string argument (that is, the rest of the current option string or if that is empty, the next argument), it can be referenced by ARGF().  After ARGEND, ­argv points at a zero-terminated list of the remaining ­argc arguments. 

­ARGBEGIN also sets up ­argv0 to point at ­argv[0] (conventionally the name of the program). 

EXAMPLES

This program processes arguments for a command that can take option ­b and option f, which requires an argument. 

#include <u.h>
#include <libc.h>
 main(int argc, char ∗argv[])
{
char ∗f;
 print("%s", argv[0]);
ARGBEGIN{
case ’b’:print(" -b"); break;
case ’f’:f = ARGF(); print(" -f(%s)", f?f:"no arg"); break;
default:print(" badflag(’%c’)", ARGC()); break;
}ARGEND
print(" %d args:", argc);
while(∗argv)
print(" ’%s’", ∗argv++);
print("\n");
exits(0);
}

When this program is run as

prog -bffile1 -r -f file2 arg1 argn

it yields

prog -b -f(file1) badflag(’r’) -f(file2) 2 args: ’arg1’ ’argn’

DIAGNOSTICS

­ARGF() returns 0 on error. 

Plan 9  —  January 03, 1993

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026