ARG(3)
π ARG(3) β the option-letter era π
ok so bestie, you're reading the ARG(3) page from the Library Functions Manual, and honestly? This one's a whole vibe. It's the gang of macros β ARGBEGIN, ARGEND, ARGC, ARGF, EARGF, arginit, argopt β that do the whole "process option letters from argv" thing. Like, the entire classic option-parsing friend group, together on one page. Let's get into it. π
β¨ NAME β¨
ARGBEGIN, ARGEND, ARGC, ARGF, EARGF, arginit, argopt β the iconic little crew that processes option letters from argv. πΈ
π¦ SYNOPSIS π¦
#include <u.h>
#include <libc.h>
ARGBEGIN {
char *ARGF();
char *EARGF(code);
Rune ARGC();
} ARGEND
extern char *argv0;
like, that's the whole shape of it. gorgeous. no notes. β¨
π DESCRIPTION π
okay so the first thing you literally have to know: these macros assume the names argc and argv are in scope β check exec(3) for the lore on where those come from. π
ARGBEGIN and ARGEND are the bookends, the matching set, the outfit and the shoes. They surround the code that processes your program's options. That code in the middle should be the cases of a C switch on option characters, and it gets executed once for each option character. cute, right?
Options end β like, the party's over β after:
- an argument
-- - before an argument
- - before an argument that doesn't begin with
-
periodt. that's the rule. π
ARGC is a function macro that returns the current option character, as an integer. it's giving "which flag are we on right now" energy.
ARGF is a function macro that returns the current option argument β a pointer to the rest of the option string if it's not empty, or the next argument in argv if there is one, or 0. π«Ά
β οΈ bestie, listen: ARGF must be called just once for each option that takes an argument. once. one time. do not go back for seconds.
EARGF is like ARGF but make it dramatic: instead of returning zero, it runs code and, if that returns, it calls abort(3). π a typical value for code is usage(), like this:
EARGF(usage())
After ARGBEGIN, argv0 is a copy of argv[0] β conventionally, the name of the program. main character energy, truly.
After ARGEND, argv points at a zero-terminated list of the remaining argc arguments. so like, all the actual real args that weren't flags. slay. β¨
π₯ EXAMPLE π₯
this C program can take option b and option f β and f requires an argument. look at her go:
#include <u.h>
#include <libc.h>
void
main(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("\n");
exits(nil);
}
and here's the output from running the command prog -bffile1 -r -f file2 arg1 arg2 β get ready:
prog -b -f(file1) badflag('r') -f(file2) 2 args: 'arg1' 'arg2'
no cap, that's the actual output. -b did its thing, -f(file1) came from the stuck-together -bffile1, r got roasted as a badflag, -f file2 pulled the next argument, and then 2 args: with 'arg1' 'arg2' left over. ate and left no crumbs. πβ¨
π SOURCE π
/include/libc.h
β¨ that's the whole page, bestie β¨ this was ARG(3) from the Library Functions Manual, source file /include/libc.h, and the footer's dated ARG(3) β go forth and parse those option letters, you absolute icon. π§ππ«Ά
π¦ where this page comes from
- alpine 3.24: plan9port-doc 0_git20260208-r0 Β· LPL-1.02
- arch rolling: plan9port 20261021-2 Β· custom
- nixos 26.05: plan9port 0-unstable-2025-11-10
- nixos 26.05: plan9port-wayland 0-unstable-2025-11-26
- nixpkgs unstable: plan9port 0-unstable-2025-11-10
- nixpkgs unstable: plan9port-wayland 0-unstable-2025-11-26
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms