chickadee » optimism » parse-command-line

parse-command-line grammarprocedure
parse-command-line arguments grammarprocedure
parse-command-line matcher arguments grammarprocedure

parse-command-line parses a program's command line arguments into an association list according to an S-expressive options grammar.

It takes one required and two optional arguments: an option matching procedure, an S-expressive options grammar, and a list of command line argument strings. If matcher is not given a basic string comparison is used, while arguments defaults to the value of (cdr (command-line)).

grammar is a finite list of pairs whose cars are symbols and whose cdrs are pairs or atoms. All other cars in the grammar must be atoms; grammars may not be nested.

The given arguments are matched as symbols against the cars of the options grammar. When a match is found, an association from the matched symbol to the arguments immediately following the matched item in the arguments list is added, following the form of the matched pair.

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

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

Unmatched arguments are added to the resulting association list under the key --. Similarly, any arguments following a "--" in the arguments list are treated as unmatched.

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

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

In a matched options form, procedures are replaced by the result of invoking that procedure on the corresponding item in the arguments list. All other objects are replaced by the corresponding argument string directly.

(parse-command-line               ; => ((foo ("bar") 42 qux)
 '("foo" "bar" "42" "qux")        ;     (--))
 `((foo ,list