ZetCode

Algol 68 command-line arguments

last modified November 13, 2026

A script reads the words of its command line through argc and argv. The first is the number of entries and the second is a procedure that returns one entry. This article describes the entries that the interpreter adds, the --script option that passes the remaining words to the program, a small option parser, and the places where a68g itself takes options.

Counting and listing the arguments

argc is an integer with the number of command-line entries, and argv(i) returns the i-th entry as a string. The interpreter puts itself, the option, and the script name in the first three places, so the arguments of the program start at index 4.

args.a68
BEGIN
  print((whole(argc - 3, 0), " argument(s)", newline));
  FOR i FROM 4 TO argc DO
    print((whole(i - 3, 0), ": ", argv(i), newline))
  OD
END

The loop counts from 4 to argc, so it visits only the arguments that the caller supplied. The numbering of the output starts at 1 again, which is the way a program usually reports its arguments. argv is a procedure, not a row, so the index is written in parentheses rather than in brackets.

$ a68g --script args.a68 alpha beta gamma
3 argument(s)
1: alpha
2: beta
3: gamma

The three leading entries

The value in argv(1) is the name of the interpreter as it was invoked, argv(2) is the --script option, and argv(3) is the file name of the script. The program therefore subtracts three to get the number of its own arguments, and a script that needs the name of the script itself finds it in argv(3).

The entries are strings, so a number on the command line arrives as text. A program that needs the value as an integer writes the string to a file or compares it with the strings it expects; the examples below compare options and treat the remaining words as names.

Options passed to the script

--script skips the rest of the option processing of a68g, so every following word is handed to the program, even when it begins with a minus sign. The program reads them in a loop that consumes an option and, for one of the options, the word after it.

options.a68
BEGIN
  BOOL verbose := FALSE;
  STRING name := "world";
  INT i := 4;
  WHILE i <= argc DO
    IF argv(i) = "-v" THEN
      verbose := TRUE
    ELIF argv(i) = "-n" AND i < argc THEN
      name := argv(i + 1);
      i +:= 1
    FI;
    i +:= 1
  OD;
  IF verbose THEN print(("about to greet", newline)) FI;
  print(("Hello, ", name, newline))
END

The index is an ordinary variable, because the loop has to skip the word after -n; the control variable of a FOR loop could not be changed. An unknown word is ignored, which is a reasonable choice for a small script but hides typing errors. The call below passes both options, so the program prints the note and the name.

$ a68g --script options.a68 -v -n Ada
about to greet
Hello, Ada

A default for a missing argument

A script that must work without arguments tests argc before it reads argv(4). The test is the difference between the number of entries and the three that the interpreter adds.

greet.a68
BEGIN
  IF argc < 4 THEN
    print(("Hello, world", newline))
  ELSE
    print(("Hello, ", argv(4), newline))
  FI
END

Without an argument the program greets the world, and with one it greets the name it was given. Reading argv(4) when argc is smaller would return an empty string in this implementation, but testing the count states the intention and works the same way in other implementations.

$ a68g --script greet.a68
Hello, world

An argument as a file name

The arguments are often names of files. The program below takes the first argument as the name of a file it writes, which combines the command line with the transput procedures of the files article.

writefile.a68
BEGIN
  IF argc < 4 THEN
    print(("usage: a68g --script writefile.a68 FILE", newline))
  ELSE
    FILE f;
    open(f, argv(4), stand out channel);
    put(f, ("written by a script", newline));
    close(f);
    print(("wrote ", argv(4), newline))
  FI
END

The usage line names the script and its argument, which is the usual help text of a small tool. The file is opened with the name from the command line, one line is written, and the program reports the name it used. A missing argument produces the usage line instead of an error.

$ a68g --script writefile.a68 out.txt
wrote out.txt

Where a68g takes its own options

a68g reads options from three places before it runs a program: the file .a68g.rc in the working directory, the environment variable A68G_OPTIONS, and its command line. An option on the command line overrides the environment variable, which overrides the file, and a pragmat inside the program overrides all three.

The --exit option, also written as --, stops the option processing so that the remaining words reach the program. With --script the processing stops anyway, which is why a script may use words such as -v for itself. The options --check, --heap, and -O2 take their arguments in the same way, and a68g --help lists them all.

Next steps

The files article explains the transput on the file that the last example opens, and reading input covers the values that a program takes from the terminal. The unformatted transput article describes print and put, and the introduction lists the options of a68g.

Source

Revised Report on the Algorithmic Language Algol 68

The Algol 68 Genie project

In this article we have counted and listed the arguments of a script, seen the three entries that the interpreter adds, passed options through --script, parsed two options in a loop, supplied a default for a missing argument, and used an argument as the name of a file. The files article continues with the transput that such a program performs.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Algol 68 tutorials.