File: command-line-parser.thb

package info (click to toggle)
theme-d 7.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,036 kB
  • sloc: lisp: 9,625; sh: 5,321; makefile: 715; ansic: 477
file content (133 lines) | stat: -rw-r--r-- 4,491 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
;; -*-theme-d-*-

;; Copyright (C) 2019, 2022 Tommi Höynälänmaa
;; Distributed under GNU Lesser General Public License version 3,
;; see file doc/LGPL-3.

(define-body (standard-library command-line-parser)

  
  (import (standard-library string-utilities)
	  (standard-library list-utilities))

  
  (define-simple-proc raise1 (((s-kind <rte-exception-kind>)
                               (al-info <rte-exception-info>))
                              <none> (pure never-returns))
    (raise (make-rte-exception s-kind al-info)))


  (define-simple-proc handle-long-argument
      (((l-argdescs <argument-descriptor-list>) (str <string>))
       <none> nonpure)
    (let* ((str-option (string-drop str 2))
	   (ich (string-char-index str-option #\=))
	   (str-actual-option
	    (if (= ich -1)
		str-option
		(string-take str-option ich)))
	   (str-option-arg
	    (if (not (= ich -1))
		(string-drop str-option (+ ich 1))
		""))
	   (argdesc
	    (find (lambda (((argdesc1 <argument-descriptor>)) <boolean> pure)
		    (match-type (field-ref argdesc1 'x-name)
		      ((str-name <string>)
		       (equal-values? str-actual-option str-name))
		      (else #f)))
		  l-argdescs
		  null)))
      (match-type argdesc
	((<null>) (raise1 'unknown-command-line-argument
			  (list (cons 'str-arg str))))
	((argdesc1 <argument-descriptor>)
	 (let ((takes-argument? (field-ref argdesc1 'takes-argument?))
	       (argument-present? (not (= ich -1))))
	   (cond
	    ((and takes-argument? argument-present?)
	     (let ((proc (field-ref argdesc1 'proc-handler)))
	       (proc str-option-arg)))
	    ((and (not takes-argument?) (not argument-present?))
	     (let ((proc (field-ref argdesc1 'proc-handler)))
	       (proc "")))
	    ((and takes-argument? (not argument-present?))
	     (raise1 'required-argument-missing
		     (list (cons 'argdesc argdesc1))))
	    ((and (not takes-argument?) argument-present?)
	     (raise1 'unwanted-argument
		     (list (cons 'argdesc argdesc1))))
	    (else
	     ;; We should never enter here.
	     (raise-simple 'internal-error))))))))

  
  (define-simple-proc handle-short-argument
      (((l-argdescs <argument-descriptor-list>) (str <string>)
	(i0 <integer>) (l-command-line (:uniform-list <string>)))
       <integer> nonpure)
    (if (<= (string-length str) 1)
	(raise-simple 'command-line-syntax-error)
	(let* ((ch-option (string-ref str 1))
	       (argdesc
		(find (lambda (((argdesc1 <argument-descriptor>)) <boolean>
			       pure)
			(match-type (field-ref argdesc1 'x-name)
			  ((ch-name <character>)
			   (equal-values? ch-option ch-name))
			  (else #f)))
		      l-argdescs
		      null))
	       (i-cmd-len (length l-command-line)))
	  (match-type argdesc
	    ((<null>)
	     (raise1 'unknown-command-line-argument
		     (list (cons 'str-arg str))))
	    ((argdesc1 <argument-descriptor>)
	     (if (field-ref argdesc1 'takes-argument?)
		 (begin
		   (if (< (+ i0 1) i-cmd-len)
		       (let ((str-option-arg
			      (uniform-list-ref l-command-line (+ i0 1)))
			     (proc (field-ref argdesc1 'proc-handler)))
			 (proc str-option-arg))
		       (raise1 'missing-option-argument
			       (list (cons 'str-arg str))))
		   (+ i0 2))
		 (let ((proc (field-ref argdesc1 'proc-handler)))
		   (proc "")
		   (+ i0 1))))))))

  
  (define-simple-method parse-command-line
      (((l-command-line (:uniform-list <string>))
	(l-argdescs <argument-descriptor-list>)
	(proc-handle-proper-args
	 (:procedure ((:uniform-list <string>)) <none> nonpure)))
       <none> nonpure)
    (let ((i-cmd-len (length l-command-line)))
      (let-mutable ((all-options-handled? <boolean> #f)
		    (i0 <integer> 0))
	(until ((or (>= i0 i-cmd-len) all-options-handled?))
	  (let* ((str-cur (uniform-list-ref l-command-line i0))
		 (i-cur-len (string-length str-cur)))
	    (cond
	     ((= i-cur-len 0) (raise-simple 'internal-error))
	     ((equal-values? str-cur "--")
	      (set! all-options-handled? #t)
	      (set! i0 (+ i0 1)))
	     ((and (> (string-length str-cur) 2)
		   (equal-values? (string-take str-cur 2) "--"))
	      (handle-long-argument l-argdescs str-cur)
	      (set! i0 (+ i0 1)))
	     ((equal-values? (string-ref str-cur 0) #\-)
	      (set! i0
		    (handle-short-argument l-argdescs str-cur i0
					   l-command-line)))
	     (else
	      (set! all-options-handled? #t)))))
	(let ((l-proper-args
	       (if all-options-handled?
		   (drop l-command-line i0)
		   null)))
	  (proc-handle-proper-args l-proper-args))))))