Getopt
------
`(require 'getopt)'
This routine implements Posix command line argument parsing. Notice
that returning values through global variables means that `getopt' is
_not_ reentrant.
- Variable: *optind*
Is the index of the current element of the command line. It is
initially one. In order to parse a new command line or reparse an
old one, *OPTING* must be reset.
- Variable: *optarg*
Is set by getopt to the (string) option-argument of the current
option.
- Procedure: getopt argc argv optstring
Returns the next option letter in ARGV (starting from `(vector-ref
argv *optind*)') that matches a letter in OPTSTRING. ARGV is a
vector or list of strings, the 0th of which getopt usually
ignores. ARGC is the argument count, usually the length of ARGV.
OPTSTRING is a string of recognized option characters; if a
character is followed by a colon, the option takes an argument
which may be immediately following it in the string or in the next
element of ARGV.
*OPTIND* is the index of the next element of the ARGV vector to be
processed. It is initialized to 1 by `getopt.scm', and `getopt'
updates it when it finishes with each element of ARGV.
`getopt' returns the next option character from ARGV that matches
a character in OPTSTRING, if there is one that matches. If the
option takes an argument, `getopt' sets the variable *OPTARG* to
the option-argument as follows:
* If the option was the last character in the string pointed to
by an element of ARGV, then *OPTARG* contains the next
element of ARGV, and *OPTIND* is incremented by 2. If the
resulting value of *OPTIND* is greater than or equal to ARGC,
this indicates a missing option argument, and `getopt'
returns an error indication.
* Otherwise, *OPTARG* is set to the string following the option
character in that element of ARGV, and *OPTIND* is
incremented by 1.
If, when `getopt' is called, the string `(vector-ref argv
*optind*)' either does not begin with the character `#\-' or is
just `"-"', `getopt' returns `#f' without changing *OPTIND*. If
`(vector-ref argv *optind*)' is the string `"--"', `getopt'
returns `#f' after incrementing *OPTIND*.
If `getopt' encounters an option character that is not contained in
OPTSTRING, it returns the question-mark `#\?' character. If it
detects a missing option argument, it returns the colon character
`#\:' if the first character of OPTSTRING was a colon, or a
question-mark character otherwise. In either case, `getopt' sets
the variable GETOPT:OPT to the option character that caused the
error.
The special option `"--"' can be used to delimit the end of the
options; `#f' is returned, and `"--"' is skipped.
RETURN VALUE
`getopt' returns the next option character specified on the command
line. A colon `#\:' is returned if `getopt' detects a missing
argument and the first character of OPTSTRING was a colon `#\:'.
A question-mark `#\?' is returned if `getopt' encounters an option
character not in OPTSTRING or detects a missing argument and the
first character of OPTSTRING was not a colon `#\:'.
Otherwise, `getopt' returns `#f' when all command line options
have been parsed.
Example:
#! /usr/local/bin/scm
;;;This code is SCM specific.
(define argv (program-arguments))
(require 'getopt)
(define opts ":a:b:cd")
(let loop ((opt (getopt (length argv) argv opts)))
(case opt
((#\a) (print "option a: " *optarg*))
((#\b) (print "option b: " *optarg*))
((#\c) (print "option c"))
((#\d) (print "option d"))
((#\?) (print "error" getopt:opt))
((#\:) (print "missing arg" getopt:opt))
((#f) (if (< *optind* (length argv))
(print "argv[" *optind* "]="
(list-ref argv *optind*)))
(set! *optind* (+ *optind* 1))))
(if (< *optind* (length argv))
(loop (getopt (length argv) argv opts))))
(slib:exit)
Getopt-
-------
- Function: getopt- argc argv optstring
The procedure `getopt--' is an extended version of `getopt' which
parses "long option names" of the form `--hold-the-onions' and
`--verbosity-level=extreme'. `Getopt--' behaves as `getopt'
except for non-empty options beginning with `--'.
Options beginning with `--' are returned as strings rather than
characters. If a value is assigned (using `=') to a long option,
`*optarg*' is set to the value. The `=' and value are not
returned as part of the option string.
No information is passed to `getopt--' concerning which long
options should be accepted or whether such options can take
arguments. If a long option did not have an argument, `*optarg'
will be set to `#f'. The caller is responsible for detecting and
reporting errors.
(define opts ":-:b:")
(define argc 5)
(define argv '("foo" "-b9" "--f1" "--2=" "--g3=35234.342" "--"))
(define *optind* 1)
(define *optarg* #f)
(require 'qp)
(do ((i 5 (+ -1 i)))
((zero? i))
(define opt (getopt-- argc argv opts))
(print *optind* opt *optarg*)))
-|
2 #\b "9"
3 "f1" #f
4 "2" ""
5 "g3" "35234.342"
5 #f "35234.342"