Skip to content

Instantly share code, notes, and snippets.

@gwatt
Last active February 10, 2018 17:51
Show Gist options
  • Save gwatt/1705019b8fc0317679cbd09830f70092 to your computer and use it in GitHub Desktop.
Save gwatt/1705019b8fc0317679cbd09830f70092 to your computer and use it in GitHub Desktop.
Store command line arguments in parameters
(define-syntax param-args
(syntax-rules ()
[(_ arg-list (opt param) ...)
(let loop ([args arg-list])
(if (null? args)
'()
(case (car args)
[(opt) (if (null? (cdr args))
(errorf 'param-args "Missing required argument for ~a" opt))
(param (cadr args))
(loop (cddr args))] ...
[else args])))]))
;;;
;;; Usage:
;;;
;;; (param-args some-arg-list
;;; ["--foo" foo-param]
;;; ["--bar" bar-param]
;;; ["--baz" baz-param])
;;;
;;; some-arg-list is a list of strings, like (command-line-arguments)
;;; if no matching options are found, the rest of the arguments are returned from the expression
;;; when an option matches, the corresponding function (foo-param, bar-baram, or baz-param) is called
;;; with the next element of some-arg-list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment