Parse out command-line arguments

--help, --repl, and the sort.
This commit is contained in:
Jaidyn Ann 2024-02-08 19:37:10 -06:00
parent b91590133c
commit ab78dba5a3

View File

@ -15,14 +15,17 @@
(import scheme (import scheme
(chicken base) (chicken base)
(chicken condition)
(chicken file) (chicken file)
(chicken pathname)
(chicken io) (chicken io)
(chicken repl) (chicken repl)
(chicken repl) (chicken process-context)
(chicken string) (chicken string)
(chicken time) (chicken time)
srfi-1 srfi-1
srfi-18 srfi-18
(prefix getopt-long getopt:)
nrepl nrepl
qt-light qt-light
(prefix uri-common uri:) (prefix uri-common uri:)
@ -35,19 +38,56 @@
;; Start & run the application. ;; Start & run the application.
(define (init) (define (init)
;; Set up some global state, prepare the QT app. (let [(cli-args (parse-cli-args (command-line-arguments)))]
(set! qt-app (qt:init)) (if (alist-ref 'help cli-args)
(set! qt-win (create-window)) (cli-usage))
(init-window qt-win) ;; Set up some global state, prepare the QT app.
(set! qt-app (qt:init))
(set! qt-win (create-window))
(init-window qt-win)
(let ;; Start QT loop. (let ;; Start QT loop.
[(qt-thread (thread-start! qt-loop)) [(qt-thread (thread-start! qt-loop))
;; Kick off the remote-REPL… ;; Kick off the remote-REPL…
(nrepl-thread (thread-start! (lambda () (nrepl 1234))))] (nrepl-thread
;; … and also provide a local REPL. (if (alist-ref 'repl cli-args)
(repl) (thread-start!
;; Wait for the QT program, even after stdin is closed off. (lambda ()
(thread-join! qt-thread))) (nrepl (string->number (alist-ref 'repl cli-args)))))))]
;; … and also provide a local REPL.
(repl)
;; Wait for the QT program, even after stdin is closed off.
(thread-join! qt-thread))))
;; Print a “usage” help message, telling the user how to run the program.
(define (cli-usage)
(print "usage: " (pathname-file (program-name)) " [-h] [--repl PORT]")
(print)
(print (pathname-file (program-name)) " is a simple contacts program for managing")
(print "vCard-format contacts.")
(print)
(print (getopt:usage cli-args-grammar))
(exit))
;; Parse out command-line args into an alist.
;; '("dad" "-h" "--repl=12" "mm") → '(("dad" "mm") (help #t) (repl 12))
(define (parse-cli-args args)
(handle-exceptions
exn
(and
(print ((condition-property-accessor 'exn 'message) exn))
'((help . #t)))
(getopt:getopt-long args cli-args-grammar)))
;; Definition of our command-line arguments, in getopt-longs format.
(define cli-args-grammar
'((help "display this help message"
(single-char #\h))
(repl "start a TCP-accesible REPL at the given port"
(value #t))))
;; Loop through QTs processing, again and again. ;; Loop through QTs processing, again and again.
@ -145,4 +185,7 @@
vcard-alist)) vcard-alist))
(init) (init)