Overview

command-line

Command line argument handling for R7RS Scheme.

Description

This Scheme library provides a way to collect command line arguments into an association list according to a simple S-expressive options grammar. It's meant to be easy to use and pure-R7RS.

Usage

One procedure is provided, parse-command-line, which takes as arguments an options grammar and an optional list of string command line arguments and returns an association list. If no list of arguments is given, the program's command line arguments are used.

(import (foldling command-line))

(parse-command-line
 '("-a" "-b" "one" "-c" "two" "three" "-d" "four" "5" "six")
 `((-a)
   (-b . foo)
   (-c bar baz)
   (-d ,string->symbol ,string->number)))

; => ((-a)
;     (-b . "one")
;     (-c "two" "three")
;     (-d four 5)
;     (-- "six"))

As a special case, an item in the grammar whose first element is a list will be split into separate entries, allowing an abbreviated syntax for multiple option specifications of the same form:

(parse-command-line
 '(((--foo --bar --baz) . qux)))

; => (parse-command-line
;     '((--foo . qux)
;       (--bar . qux)
;       (--baz . qux)))

Handlers for getopt(3) and getopt_long(3)-style command lines are provided by the getopt and getopt-long sublibraries, respectively. These can be provided as the first argument to parse-command-line, to customize its option-matching behavior.

(import (foldling command-line)
        (foldling command-line getopt)
        (foldling command-line getopt-long))

(parse-command-line               ; => ((-a)
 getopt                           ;     (-b)
 '("-abcone" "-d" "two")          ;     (-c . "one")
 '((-a)                           ;     (-d . "two")
   (-b)                           ;     (--))
   (-c . one)
   (-d . two)))

(parse-command-line               ; => ((-a . "one")
 getopt-long                      ;     (--foo)
 '("-aone" "--foo" "--bar=two")   ;     (--bar . "two")
 '((-a . one)                     ;     (--))
   (--foo)
   (--bar . two)))

Refer to command-line.scm for more API details.

Installation

To install for CHICKEN Scheme, run chicken-install from the project's root directory.

$ git clone git://bitbucket.org/evhan/command-line.git
$ cd command-line
$ chicken-install -test

Other Schemes should install the following files:

src/foldling/command-line.scm
src/foldling/command-line.sld
src/foldling/command-line/getopt.scm
src/foldling/command-line/getopt.sld
src/foldling/command-line/getopt-long.scm
src/foldling/command-line/getopt-long.sld

Author

Evan Hanson evhan@foldling.org

License

This software is written by Evan Hanson and placed in the Public Domain. All warranties are disclaimed.