Warning: This is the manual of the legacy Guile 2.2 series. You may want to read the manual of the current stable series instead.

Next: , Previous: , Up: SRFI Support   [Contents][Index]


7.5.25 SRFI-37 - args-fold

This is a processor for GNU getopt_long-style program arguments. It provides an alternative, less declarative interface than getopt-long in (ice-9 getopt-long) (see The (ice-9 getopt-long) Module). Unlike getopt-long, it supports repeated options and any number of short and long names per option. Access it with:

(use-modules (srfi srfi-37))

SRFI-37 principally provides an option type and the args-fold function. To use the library, create a set of options with option and use it as a specification for invoking args-fold.

Here is an example of a simple argument processor for the typical ‘--version’ and ‘--help’ options, which returns a backwards list of files given on the command line:

(args-fold (cdr (program-arguments))
           (let ((display-and-exit-proc
                  (lambda (msg)
                    (lambda (opt name arg loads)
                      (display msg) (quit)))))
             (list (option '(#\v "version") #f #f
                           (display-and-exit-proc "Foo version 42.0\n"))
                   (option '(#\h "help") #f #f
                           (display-and-exit-proc
                            "Usage: foo scheme-file ..."))))
           (lambda (opt name arg loads)
             (error "Unrecognized option `~A'" name))
           (lambda (op loads) (cons op loads))
           '())
Scheme Procedure: option names required-arg? optional-arg? processor

Return an object that specifies a single kind of program option.

names is a list of command-line option names, and should consist of characters for traditional getopt short options and strings for getopt_long-style long options.

required-arg? and optional-arg? are mutually exclusive; one or both must be #f. If required-arg?, the option must be followed by an argument on the command line, such as ‘--opt=value’ for long options, or an error will be signalled. If optional-arg?, an argument will be taken if available.

processor is a procedure that takes at least 3 arguments, called when args-fold encounters the option: the containing option object, the name used on the command line, and the argument given for the option (or #f if none). The rest of the arguments are args-fold “seeds”, and the processor should return seeds as well.

Scheme Procedure: option-names opt
Scheme Procedure: option-required-arg? opt
Scheme Procedure: option-optional-arg? opt
Scheme Procedure: option-processor opt

Return the specified field of opt, an option object, as described above for option.

Scheme Procedure: args-fold args options unrecognized-option-proc operand-proc seed …

Process args, a list of program arguments such as that returned by (cdr (program-arguments)), in order against options, a list of option objects as described above. All functions called take the “seeds”, or the last multiple-values as multiple arguments, starting with seed …, and must return the new seeds. Return the final seeds.

Call unrecognized-option-proc, which is like an option object’s processor, for any options not found in options.

Call operand-proc with any items on the command line that are not named options. This includes arguments after ‘--’. It is called with the argument in question, as well as the seeds.


Next: , Previous: , Up: SRFI Support   [Contents][Index]