| | @@ -936,6 +936,8 @@ |
| 936 | 936 | | `Map` | a generic hash map, `Map(K, V; hash, eq)` | |
| 937 | 937 | | `Sort` | sorting a slice in place, `Sort(T; less)` | |
| 938 | 938 | | `Math` | `sqrt`, `floor`, `sin`, `cos`, `atan2`, `exp`, `ln` and `pi`, for `F64` | |
| 939 | +| `Files` | an argument as a string, and a whole file read or written | |
| 940 | +| `Opt` | a command line read through one table, by `getopt`'s rules with long options; its help and its mistakes | |
| 939 | 941 | |
| 940 | 942 | ``` |
| 941 | 943 | module Mem; |
| | @@ -1214,6 +1216,135 @@ |
| 1214 | 1216 | nearest; from 1e15 on, with an exponent, as `1.50e17`; and `nan`, |
| 1215 | 1217 | `inf` and `-inf` as themselves. |
| 1216 | 1218 | |
| 1219 | +**Files and Opt** are what most programs need of their command line. |
| 1220 | + |
| 1221 | +``` |
| 1222 | +module Files; |
| 1223 | +proc arg*(i: I64): []U8; (* argument i; 0 is the program's name *) |
| 1224 | +proc read*(path: []U8; b: ^Buf.Buf): BOOL; (* the whole file added to b *) |
| 1225 | +proc write*(path, data: []U8): BOOL; (* the file made to hold data *) |
| 1226 | + |
| 1227 | +module Opt; |
| 1228 | +type |
| 1229 | + Row* = variant |
| 1230 | + usage(scope, line: []U8); (* a synopsis line, without "usage: " *) |
| 1231 | + sub(scope, name, help: []U8); (* a subcommand *) |
| 1232 | + option(scope: []U8; letter: U8; long, value, help: []U8); |
| 1233 | + end; |
| 1234 | + Parser* = record |
| 1235 | + letter*: U8; (* the option just read *) |
| 1236 | + value*: []U8; (* its value; "" for a flag *) |
| 1237 | + long*: BOOL; (* it was written by its long name *) |
| 1238 | + ... |
| 1239 | + end; |
| 1240 | +proc init*(p: ^Parser; name: []U8; table: []Row); (* the arguments from 1 on, by table *) |
| 1241 | +proc next*(p: ^Parser): BOOL; (* the next option; false when they end *) |
| 1242 | +proc operand*(p: ^Parser): ([]U8, BOOL); (* the next operand; false when there are no more *) |
| 1243 | +proc scope*(p: ^Parser; name: []U8); (* subcommand name's options, from the word after it *) |
| 1244 | +proc number*(p: ^Parser; lo, hi: I64): I64; (* the value just read, a number from lo to hi *) |
| 1245 | +proc fail*(p: ^Parser; text: []U8); (* the program's own mistake *) |
| 1246 | +``` |
| 1247 | + |
| 1248 | +`Opt` reads a command line by the rules of the C library's `getopt`, |
| 1249 | +with GNU's long options, so that a program's options behave as every |
| 1250 | +other Unix program's. The program describes itself once, in a table of |
| 1251 | +`Row`s: its synopsis lines, its subcommands and its options. The help |
| 1252 | +and the messages for mistakes are made from the table, so they can't |
| 1253 | +drift from it; the program's test of `p.letter` is the only other |
| 1254 | +place an option appears. The parser is a record the program owns, and |
| 1255 | +the table a slice it keeps as long as the parser: a literal written in |
| 1256 | +the call to `init` lives as long as the procedure that makes it. |
| 1257 | + |
| 1258 | +A row's `scope` is `""` for the program itself, or a subcommand's |
| 1259 | +name: `"start"`, or `"remote.add"` for a subcommand of `remote`. An |
| 1260 | +option has a `letter`, a `long` name, or both. One with a long name |
| 1261 | +only has a key of its own below `'!'` for a letter, so that the |
| 1262 | +program can tell it from the others. With a `value`, the name the |
| 1263 | +help and the messages give it, the option takes a value; with `""` it |
| 1264 | +is a flag. |
| 1265 | + |
| 1266 | +``` |
| 1267 | +const wait = 1; (* the key of --wait, which has no letter *) |
| 1268 | +... |
| 1269 | +Opt.init(ADR(p), "keep", [ |
| 1270 | + Opt.usage("", "keep [-v] [--wait secs] start ..."), |
| 1271 | + Opt.sub("", "start", "start a job"), |
| 1272 | + Opt.option("", 'v', "verbose", "", "say more"), |
| 1273 | + Opt.option("", wait, "wait", "secs", "give the job secs to start"), |
| 1274 | + Opt.usage("start", "keep start [-b label] name command [argument ...]"), |
| 1275 | + Opt.option("start", 'b', "bar", "label", "show the job in the bar as label")]); |
| 1276 | +while Opt.next(ADR(p)) do |
| 1277 | + if p.letter = 'v' then verbose := true; |
| 1278 | + elsif p.letter = wait then secs := Opt.number(ADR(p), 1, 600); |
| 1279 | + end; |
| 1280 | +end; |
| 1281 | +w, ok := Opt.operand(ADR(p)); |
| 1282 | +if not ok or (w # "start") then Opt.fail(ADR(p), "no command; see keep -h"); end; |
| 1283 | +Opt.scope(ADR(p), "start"); |
| 1284 | +while Opt.next(ADR(p)) do |
| 1285 | + if p.letter = 'b' then bar := p.value; end; |
| 1286 | +end; |
| 1287 | +job, ok := Opt.operand(ADR(p)); |
| 1288 | +``` |
| 1289 | + |
| 1290 | +The rules are `getopt`'s: |
| 1291 | + |
| 1292 | +- Flags may share a dash: `-lw` is `-l -w`. A letter's value is the |
| 1293 | + rest of its word, `-bdev`, or the whole next word, `-b dev`, even one |
| 1294 | + that starts with a dash or is empty; in a group, the first letter |
| 1295 | + that takes a value takes the rest, `-qn12`. |
| 1296 | +- A long option is written in full, with its value after a `=` or in |
| 1297 | + the next word: `--bar=dev`, `--bar dev`. An abbreviation is unknown, |
| 1298 | + not guessed, so that adding an option never breaks a script. A value |
| 1299 | + given to a flag, `--follow=x`, is a mistake. |
| 1300 | +- `--` ends the options, and isn't an operand; `-` alone is one. The |
| 1301 | + options end at the first operand, so in `keep start site python -m |
| 1302 | + http.server` the `-m` is python's. |
| 1303 | +- Each scope has its own options. An option given twice is read twice. |
| 1304 | + |
| 1305 | +`number` reads the value as `Str.toInt` does. `operand` reads what |
| 1306 | +options are left first, and the program doesn't see them. |
| 1307 | + |
| 1308 | +`-h` or `--help` among a scope's options prints its help on standard |
| 1309 | +output and ends the program with status 0. `init` and `scope` answer |
| 1310 | +it, looking through the scope's options before `next` gives one, so |
| 1311 | +that the program has done nothing yet: `keep -v -h -x` prints the |
| 1312 | +help, and the program never sees `-v`, nor the mistake. The help is |
| 1313 | +the scope's synopsis lines, a blank line, and its subcommands, its |
| 1314 | +options and the help itself, each with its line of help: |
| 1315 | + |
| 1316 | +``` |
| 1317 | +usage: keep start [-b label] name command [argument ...] |
| 1318 | + |
| 1319 | + -b, --bar label show the job in the bar as label |
| 1320 | + -h, --help print this and exit |
| 1321 | +``` |
| 1322 | + |
| 1323 | +A scope with several synopsis lines has the others indented under the |
| 1324 | +first; one with none, `usage:` and its words, as `usage: keep start`. |
| 1325 | + |
| 1326 | +A mistake on the command line is a sentence on standard error, the |
| 1327 | +program's name first, then the scope's synopsis lines, as the help |
| 1328 | +begins, and status 2: |
| 1329 | + |
| 1330 | +``` |
| 1331 | +keep: unknown option -x |
| 1332 | +usage: keep start [-b label] name command [argument ...] |
| 1333 | +``` |
| 1334 | + |
| 1335 | +The sentences are `unknown option -x` (or `--name`, up to its `=`), |
| 1336 | +`-b needs a label`, `--follow takes no value` and `--wait wants a |
| 1337 | +number from 1 to 600, not '0'`. `fail` writes the program's own |
| 1338 | +mistakes the same way, but alone, so that the sentence can say where |
| 1339 | +the help is: `keep: no command; see keep -h`. |
| 1340 | + |
| 1341 | +A mistake in the table is the program's, not its user's. `init` checks |
| 1342 | +the table before it reads a word, and ends the program with status 1: |
| 1343 | +`keep: option table, row 3: the option is in the scope already`. The |
| 1344 | +others are an option with neither a letter nor a long name, a long |
| 1345 | +name with `=` in it, a letter `-`, and `-h` or `--help` in the table, |
| 1346 | +since they are `Opt`'s. |
| 1347 | + |
| 1217 | 1348 | **Out and C's stdio.** `Out` keeps a buffer of 4 KB. It writes it out |
| 1218 | 1349 | when it is full, at each line feed when standard output is a terminal, |
| 1219 | 1350 | at `Out.flush`, and when the program ends by `Rt.exit`. C's `stdio` |
| | @@ -1278,4 +1409,4 @@ |
| 1278 | 1409 | sleeping and random bytes; and network sockets. `Sys.spawn` takes a |
| 1279 | 1410 | `Program`, and `Sys.wait` says which program changed and how. `Str` |
| 1280 | 1411 | gains UTF-8 and reading a number, `Map` removing and walking, `Fmt` |
| 1281 | | - reals; `Sort` and `Math` are new (section 11). |
| 1412 | + reals; `Sort`, `Math`, `Files` and `Opt` are new (section 11). |