From 27bdb9bdd130a91ed5dff6c973e6ba862e44fd1b Mon Sep 17 00:00:00 2001 From: Tom Willemsen Date: Sat, 29 Dec 2012 02:21:21 +0100 Subject: [PATCH] Add version and help options to linkwave --- src/linkwave.scm | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/src/linkwave.scm b/src/linkwave.scm index fbb25fd..12a2a9b 100644 --- a/src/linkwave.scm +++ b/src/linkwave.scm @@ -26,6 +26,8 @@ (require-extension sqlite3) (require-library posix srfi-4) +(define version "0.1.0") + (: string-no-null (string -> string)) (define (string-no-null str) ;; If STR contains a \0 byte at the end, remove it, otherwise return @@ -74,19 +76,45 @@ (or (string= (substring str 0 7) "http://") (string= (substring str 0 8) "https://")))) +(define (display-version) + ;; Display linkwave's version number. + (format #t "linkwave v~a~%" version)) + +(define (display-help) + ;; Display linkwave's help message. + (format #t (string-append + "Usage: linkwave [options]...~%" + " linkwave [...]~%" + "~%" + "Possible options:~%" + "~%" + "--help, -h Display this help and exit~%" + "--version, -v Output version information and exit~%"))) + (define (main args) ;; Open a database connection, do what the user asked and close it ;; again. (let ((db (open-database (data-file "linkwave.db")))) (if (null? args) (for-each-row print-row db "select * from bookmark") - (cond - ((url-string? (car args)) - (with-transaction - db (lambda () (add-bookmark db (car args) (cadr args) (caddr args) (cdddr args)) #t))) - (else - (format #t "Unrecognized option: ~a~%" (car args))))) - + (if (url-string? (car args)) + (with-transaction + db (lambda () (add-bookmark db (car args) (cadr args) (caddr args) (cdddr args)) #t)) + ;; If the first argument is not a URL, loop through all + ;; arguments and proceed accordingly. + (do ((arg (car args) (and (not (null? args)) + (car args)))) + ((or (null? arg) (not arg))) + (cond + ((or (string= arg "-v") (string= arg "--version")) + (display-version) + (exit 0)) + ((or (string= arg "-h") (string= arg "--help")) + (display-help) + (exit 0)) + (else + (format #t "Unrecognized option: ~a~%" (car args)))) + (set! args (cdr args))))) (finalize! db #t))) (main (command-line-arguments))