getopt-long.scm 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. ;;; Copyright (C) 1998, 2001, 2006 Free Software Foundation, Inc.
  2. ;;;
  3. ;; This library is free software; you can redistribute it and/or
  4. ;; modify it under the terms of the GNU Lesser General Public
  5. ;; License as published by the Free Software Foundation; either
  6. ;; version 2.1 of the License, or (at your option) any later version.
  7. ;;
  8. ;; This library is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;; Lesser General Public License for more details.
  12. ;;
  13. ;; You should have received a copy of the GNU Lesser General Public
  14. ;; License along with this library; if not, write to the Free Software
  15. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. ;;; Author: Russ McManus (rewritten by Thien-Thi Nguyen)
  17. ;;; Commentary:
  18. ;;; This module implements some complex command line option parsing, in
  19. ;;; the spirit of the GNU C library function `getopt_long'. Both long
  20. ;;; and short options are supported.
  21. ;;;
  22. ;;; The theory is that people should be able to constrain the set of
  23. ;;; options they want to process using a grammar, rather than some arbitrary
  24. ;;; structure. The grammar makes the option descriptions easy to read.
  25. ;;;
  26. ;;; `getopt-long' is a procedure for parsing command-line arguments in a
  27. ;;; manner consistent with other GNU programs. `option-ref' is a procedure
  28. ;;; that facilitates processing of the `getopt-long' return value.
  29. ;;; (getopt-long ARGS GRAMMAR)
  30. ;;; Parse the arguments ARGS according to the argument list grammar GRAMMAR.
  31. ;;;
  32. ;;; ARGS should be a list of strings. Its first element should be the
  33. ;;; name of the program; subsequent elements should be the arguments
  34. ;;; that were passed to the program on the command line. The
  35. ;;; `program-arguments' procedure returns a list of this form.
  36. ;;;
  37. ;;; GRAMMAR is a list of the form:
  38. ;;; ((OPTION (PROPERTY VALUE) ...) ...)
  39. ;;;
  40. ;;; Each OPTION should be a symbol. `getopt-long' will accept a
  41. ;;; command-line option named `--OPTION'.
  42. ;;; Each option can have the following (PROPERTY VALUE) pairs:
  43. ;;;
  44. ;;; (single-char CHAR) --- Accept `-CHAR' as a single-character
  45. ;;; equivalent to `--OPTION'. This is how to specify traditional
  46. ;;; Unix-style flags.
  47. ;;; (required? BOOL) --- If BOOL is true, the option is required.
  48. ;;; getopt-long will raise an error if it is not found in ARGS.
  49. ;;; (value BOOL) --- If BOOL is #t, the option accepts a value; if
  50. ;;; it is #f, it does not; and if it is the symbol
  51. ;;; `optional', the option may appear in ARGS with or
  52. ;;; without a value.
  53. ;;; (predicate FUNC) --- If the option accepts a value (i.e. you
  54. ;;; specified `(value #t)' for this option), then getopt
  55. ;;; will apply FUNC to the value, and throw an exception
  56. ;;; if it returns #f. FUNC should be a procedure which
  57. ;;; accepts a string and returns a boolean value; you may
  58. ;;; need to use quasiquotes to get it into GRAMMAR.
  59. ;;;
  60. ;;; The (PROPERTY VALUE) pairs may occur in any order, but each
  61. ;;; property may occur only once. By default, options do not have
  62. ;;; single-character equivalents, are not required, and do not take
  63. ;;; values.
  64. ;;;
  65. ;;; In ARGS, single-character options may be combined, in the usual
  66. ;;; Unix fashion: ("-x" "-y") is equivalent to ("-xy"). If an option
  67. ;;; accepts values, then it must be the last option in the
  68. ;;; combination; the value is the next argument. So, for example, using
  69. ;;; the following grammar:
  70. ;;; ((apples (single-char #\a))
  71. ;;; (blimps (single-char #\b) (value #t))
  72. ;;; (catalexis (single-char #\c) (value #t)))
  73. ;;; the following argument lists would be acceptable:
  74. ;;; ("-a" "-b" "bang" "-c" "couth") ("bang" and "couth" are the values
  75. ;;; for "blimps" and "catalexis")
  76. ;;; ("-ab" "bang" "-c" "couth") (same)
  77. ;;; ("-ac" "couth" "-b" "bang") (same)
  78. ;;; ("-abc" "couth" "bang") (an error, since `-b' is not the
  79. ;;; last option in its combination)
  80. ;;;
  81. ;;; If an option's value is optional, then `getopt-long' decides
  82. ;;; whether it has a value by looking at what follows it in ARGS. If
  83. ;;; the next element is does not appear to be an option itself, then
  84. ;;; that element is the option's value.
  85. ;;;
  86. ;;; The value of a long option can appear as the next element in ARGS,
  87. ;;; or it can follow the option name, separated by an `=' character.
  88. ;;; Thus, using the same grammar as above, the following argument lists
  89. ;;; are equivalent:
  90. ;;; ("--apples" "Braeburn" "--blimps" "Goodyear")
  91. ;;; ("--apples=Braeburn" "--blimps" "Goodyear")
  92. ;;; ("--blimps" "Goodyear" "--apples=Braeburn")
  93. ;;;
  94. ;;; If the option "--" appears in ARGS, argument parsing stops there;
  95. ;;; subsequent arguments are returned as ordinary arguments, even if
  96. ;;; they resemble options. So, in the argument list:
  97. ;;; ("--apples" "Granny Smith" "--" "--blimp" "Goodyear")
  98. ;;; `getopt-long' will recognize the `apples' option as having the
  99. ;;; value "Granny Smith", but it will not recognize the `blimp'
  100. ;;; option; it will return the strings "--blimp" and "Goodyear" as
  101. ;;; ordinary argument strings.
  102. ;;;
  103. ;;; The `getopt-long' function returns the parsed argument list as an
  104. ;;; assocation list, mapping option names --- the symbols from GRAMMAR
  105. ;;; --- onto their values, or #t if the option does not accept a value.
  106. ;;; Unused options do not appear in the alist.
  107. ;;;
  108. ;;; All arguments that are not the value of any option are returned
  109. ;;; as a list, associated with the empty list.
  110. ;;;
  111. ;;; `getopt-long' throws an exception if:
  112. ;;; - it finds an unrecognized property in GRAMMAR
  113. ;;; - the value of the `single-char' property is not a character
  114. ;;; - it finds an unrecognized option in ARGS
  115. ;;; - a required option is omitted
  116. ;;; - an option that requires an argument doesn't get one
  117. ;;; - an option that doesn't accept an argument does get one (this can
  118. ;;; only happen using the long option `--opt=value' syntax)
  119. ;;; - an option predicate fails
  120. ;;;
  121. ;;; So, for example:
  122. ;;;
  123. ;;; (define grammar
  124. ;;; `((lockfile-dir (required? #t)
  125. ;;; (value #t)
  126. ;;; (single-char #\k)
  127. ;;; (predicate ,file-is-directory?))
  128. ;;; (verbose (required? #f)
  129. ;;; (single-char #\v)
  130. ;;; (value #f))
  131. ;;; (x-includes (single-char #\x))
  132. ;;; (rnet-server (single-char #\y)
  133. ;;; (predicate ,string?))))
  134. ;;;
  135. ;;; (getopt-long '("my-prog" "-vk" "/tmp" "foo1" "--x-includes=/usr/include"
  136. ;;; "--rnet-server=lamprod" "--" "-fred" "foo2" "foo3")
  137. ;;; grammar)
  138. ;;; => ((() "foo1" "-fred" "foo2" "foo3")
  139. ;;; (rnet-server . "lamprod")
  140. ;;; (x-includes . "/usr/include")
  141. ;;; (lockfile-dir . "/tmp")
  142. ;;; (verbose . #t))
  143. ;;; (option-ref OPTIONS KEY DEFAULT)
  144. ;;; Return value in alist OPTIONS using KEY, a symbol; or DEFAULT if not
  145. ;;; found. The value is either a string or `#t'.
  146. ;;;
  147. ;;; For example, using the `getopt-long' return value from above:
  148. ;;;
  149. ;;; (option-ref (getopt-long ...) 'x-includes 42) => "/usr/include"
  150. ;;; (option-ref (getopt-long ...) 'not-a-key! 31) => 31
  151. ;;; Code:
  152. (define-module (ice-9 getopt-long)
  153. :use-module ((ice-9 common-list) :select (some remove-if-not))
  154. :export (getopt-long option-ref))
  155. (define option-spec-fields '(name
  156. value
  157. required?
  158. single-char
  159. predicate
  160. value-policy))
  161. (define option-spec (make-record-type 'option-spec option-spec-fields))
  162. (define make-option-spec (record-constructor option-spec option-spec-fields))
  163. (define (define-one-option-spec-field-accessor field)
  164. `(define ,(symbol-append 'option-spec-> field) ;;; name slib-compat
  165. (record-accessor option-spec ',field)))
  166. (define (define-one-option-spec-field-modifier field)
  167. `(define ,(symbol-append 'set-option-spec- field '!) ;;; name slib-compat
  168. (record-modifier option-spec ',field)))
  169. (defmacro define-all-option-spec-accessors/modifiers ()
  170. `(begin
  171. ,@(map define-one-option-spec-field-accessor option-spec-fields)
  172. ,@(map define-one-option-spec-field-modifier option-spec-fields)))
  173. (define-all-option-spec-accessors/modifiers)
  174. (define make-option-spec
  175. (let ((ctor (record-constructor option-spec '(name))))
  176. (lambda (name)
  177. (ctor name))))
  178. (define (parse-option-spec desc)
  179. (let ((spec (make-option-spec (symbol->string (car desc)))))
  180. (for-each (lambda (desc-elem)
  181. (let ((given (lambda () (cadr desc-elem))))
  182. (case (car desc-elem)
  183. ((required?)
  184. (set-option-spec-required?! spec (given)))
  185. ((value)
  186. (set-option-spec-value-policy! spec (given)))
  187. ((single-char)
  188. (or (char? (given))
  189. (error "`single-char' value must be a char!"))
  190. (set-option-spec-single-char! spec (given)))
  191. ((predicate)
  192. (set-option-spec-predicate!
  193. spec ((lambda (pred)
  194. (lambda (name val)
  195. (or (not val)
  196. (pred val)
  197. (error "option predicate failed:" name))))
  198. (given))))
  199. (else
  200. (error "invalid getopt-long option property:"
  201. (car desc-elem))))))
  202. (cdr desc))
  203. spec))
  204. (define (split-arg-list argument-list)
  205. ;; Scan ARGUMENT-LIST for "--" and return (BEFORE-LS . AFTER-LS).
  206. ;; Discard the "--". If no "--" is found, AFTER-LS is empty.
  207. (let loop ((yes '()) (no argument-list))
  208. (cond ((null? no) (cons (reverse yes) no))
  209. ((string=? "--" (car no)) (cons (reverse yes) (cdr no)))
  210. (else (loop (cons (car no) yes) (cdr no))))))
  211. (define short-opt-rx (make-regexp "^-([a-zA-Z]+)(.*)"))
  212. (define long-opt-no-value-rx (make-regexp "^--([^=]+)$"))
  213. (define long-opt-with-value-rx (make-regexp "^--([^=]+)=(.*)"))
  214. (define (match-substring match which)
  215. ;; condensed from (ice-9 regex) `match:{substring,start,end}'
  216. (let ((sel (vector-ref match (1+ which))))
  217. (substring (vector-ref match 0) (car sel) (cdr sel))))
  218. (define (expand-clumped-singles opt-ls)
  219. ;; example: ("--xyz" "-abc5d") => ("--xyz" "-a" "-b" "-c" "5d")
  220. (let loop ((opt-ls opt-ls) (ret-ls '()))
  221. (cond ((null? opt-ls)
  222. (reverse ret-ls)) ;;; retval
  223. ((regexp-exec short-opt-rx (car opt-ls))
  224. => (lambda (match)
  225. (let ((singles (reverse
  226. (map (lambda (c)
  227. (string-append "-" (make-string 1 c)))
  228. (string->list
  229. (match-substring match 1)))))
  230. (extra (match-substring match 2)))
  231. (loop (cdr opt-ls)
  232. (append (if (string=? "" extra)
  233. singles
  234. (cons extra singles))
  235. ret-ls)))))
  236. (else (loop (cdr opt-ls)
  237. (cons (car opt-ls) ret-ls))))))
  238. (define (looks-like-an-option string)
  239. (some (lambda (rx)
  240. (regexp-exec rx string))
  241. `(,short-opt-rx
  242. ,long-opt-with-value-rx
  243. ,long-opt-no-value-rx)))
  244. (define (process-options specs argument-ls)
  245. ;; Use SPECS to scan ARGUMENT-LS; return (FOUND . ETC).
  246. ;; FOUND is an unordered list of option specs for found options, while ETC
  247. ;; is an order-maintained list of elements in ARGUMENT-LS that are neither
  248. ;; options nor their values.
  249. (let ((idx (map (lambda (spec)
  250. (cons (option-spec->name spec) spec))
  251. specs))
  252. (sc-idx (map (lambda (spec)
  253. (cons (make-string 1 (option-spec->single-char spec))
  254. spec))
  255. (remove-if-not option-spec->single-char specs))))
  256. (let loop ((argument-ls argument-ls) (found '()) (etc '()))
  257. (let ((eat! (lambda (spec ls)
  258. (let ((val!loop (lambda (val n-ls n-found n-etc)
  259. (set-option-spec-value!
  260. spec
  261. ;; handle multiple occurrances
  262. (cond ((option-spec->value spec)
  263. => (lambda (cur)
  264. ((if (list? cur) cons list)
  265. val cur)))
  266. (else val)))
  267. (loop n-ls n-found n-etc)))
  268. (ERR:no-arg (lambda ()
  269. (error (string-append
  270. "option must be specified"
  271. " with argument:")
  272. (option-spec->name spec)))))
  273. (cond
  274. ((eq? 'optional (option-spec->value-policy spec))
  275. (if (or (null? (cdr ls))
  276. (looks-like-an-option (cadr ls)))
  277. (val!loop #t
  278. (cdr ls)
  279. (cons spec found)
  280. etc)
  281. (val!loop (cadr ls)
  282. (cddr ls)
  283. (cons spec found)
  284. etc)))
  285. ((eq? #t (option-spec->value-policy spec))
  286. (if (or (null? (cdr ls))
  287. (looks-like-an-option (cadr ls)))
  288. (ERR:no-arg)
  289. (val!loop (cadr ls)
  290. (cddr ls)
  291. (cons spec found)
  292. etc)))
  293. (else
  294. (val!loop #t
  295. (cdr ls)
  296. (cons spec found)
  297. etc)))))))
  298. (if (null? argument-ls)
  299. (cons found (reverse etc)) ;;; retval
  300. (cond ((regexp-exec short-opt-rx (car argument-ls))
  301. => (lambda (match)
  302. (let* ((c (match-substring match 1))
  303. (spec (or (assoc-ref sc-idx c)
  304. (error "no such option:" c))))
  305. (eat! spec argument-ls))))
  306. ((regexp-exec long-opt-no-value-rx (car argument-ls))
  307. => (lambda (match)
  308. (let* ((opt (match-substring match 1))
  309. (spec (or (assoc-ref idx opt)
  310. (error "no such option:" opt))))
  311. (eat! spec argument-ls))))
  312. ((regexp-exec long-opt-with-value-rx (car argument-ls))
  313. => (lambda (match)
  314. (let* ((opt (match-substring match 1))
  315. (spec (or (assoc-ref idx opt)
  316. (error "no such option:" opt))))
  317. (if (option-spec->value-policy spec)
  318. (eat! spec (append
  319. (list 'ignored
  320. (match-substring match 2))
  321. (cdr argument-ls)))
  322. (error "option does not support argument:"
  323. opt)))))
  324. (else
  325. (loop (cdr argument-ls)
  326. found
  327. (cons (car argument-ls) etc)))))))))
  328. (define (getopt-long program-arguments option-desc-list)
  329. "Process options, handling both long and short options, similar to
  330. the glibc function 'getopt_long'. PROGRAM-ARGUMENTS should be a value
  331. similar to what (program-arguments) returns. OPTION-DESC-LIST is a
  332. list of option descriptions. Each option description must satisfy the
  333. following grammar:
  334. <option-spec> :: (<name> . <attribute-ls>)
  335. <attribute-ls> :: (<attribute> . <attribute-ls>)
  336. | ()
  337. <attribute> :: <required-attribute>
  338. | <arg-required-attribute>
  339. | <single-char-attribute>
  340. | <predicate-attribute>
  341. | <value-attribute>
  342. <required-attribute> :: (required? <boolean>)
  343. <single-char-attribute> :: (single-char <char>)
  344. <value-attribute> :: (value #t)
  345. (value #f)
  346. (value optional)
  347. <predicate-attribute> :: (predicate <1-ary-function>)
  348. The procedure returns an alist of option names and values. Each
  349. option name is a symbol. The option value will be '#t' if no value
  350. was specified. There is a special item in the returned alist with a
  351. key of the empty list, (): the list of arguments that are not options
  352. or option values.
  353. By default, options are not required, and option values are not
  354. required. By default, single character equivalents are not supported;
  355. if you want to allow the user to use single character options, you need
  356. to add a `single-char' clause to the option description."
  357. (let* ((specifications (map parse-option-spec option-desc-list))
  358. (pair (split-arg-list (cdr program-arguments)))
  359. (split-ls (expand-clumped-singles (car pair)))
  360. (non-split-ls (cdr pair))
  361. (found/etc (process-options specifications split-ls))
  362. (found (car found/etc))
  363. (rest-ls (append (cdr found/etc) non-split-ls)))
  364. (for-each (lambda (spec)
  365. (let ((name (option-spec->name spec))
  366. (val (option-spec->value spec)))
  367. (and (option-spec->required? spec)
  368. (or (memq spec found)
  369. (error "option must be specified:" name)))
  370. (and (memq spec found)
  371. (eq? #t (option-spec->value-policy spec))
  372. (or val
  373. (error "option must be specified with argument:"
  374. name)))
  375. (let ((pred (option-spec->predicate spec)))
  376. (and pred (pred name val)))))
  377. specifications)
  378. (cons (cons '() rest-ls)
  379. (let ((multi-count (map (lambda (desc)
  380. (cons (car desc) 0))
  381. option-desc-list)))
  382. (map (lambda (spec)
  383. (let ((name (string->symbol (option-spec->name spec))))
  384. (cons name
  385. ;; handle multiple occurrances
  386. (let ((maybe-ls (option-spec->value spec)))
  387. (if (list? maybe-ls)
  388. (let* ((look (assq name multi-count))
  389. (idx (cdr look))
  390. (val (list-ref maybe-ls idx)))
  391. (set-cdr! look (1+ idx)) ; ugh!
  392. val)
  393. maybe-ls)))))
  394. found)))))
  395. (define (option-ref options key default)
  396. "Return value in alist OPTIONS using KEY, a symbol; or DEFAULT if not found.
  397. The value is either a string or `#t'."
  398. (or (assq-ref options key) default))
  399. ;;; getopt-long.scm ends here