Add version and help options to linkwave

This commit is contained in:
Tom Willemsen 2012-12-29 02:21:21 +01:00
parent 71e54b6a61
commit 27bdb9bdd1

View file

@ -26,6 +26,8 @@
(require-extension sqlite3) (require-extension sqlite3)
(require-library posix srfi-4) (require-library posix srfi-4)
(define version "0.1.0")
(: string-no-null (string -> string)) (: string-no-null (string -> string))
(define (string-no-null str) (define (string-no-null str)
;; If STR contains a \0 byte at the end, remove it, otherwise return ;; If STR contains a \0 byte at the end, remove it, otherwise return
@ -74,19 +76,45 @@
(or (string= (substring str 0 7) "http://") (or (string= (substring str 0 7) "http://")
(string= (substring str 0 8) "https://")))) (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 <url> <name> <description> [<tag>...]~%"
"~%"
"Possible options:~%"
"~%"
"--help, -h Display this help and exit~%"
"--version, -v Output version information and exit~%")))
(define (main args) (define (main args)
;; Open a database connection, do what the user asked and close it ;; Open a database connection, do what the user asked and close it
;; again. ;; again.
(let ((db (open-database (data-file "linkwave.db")))) (let ((db (open-database (data-file "linkwave.db"))))
(if (null? args) (if (null? args)
(for-each-row print-row db "select * from bookmark") (for-each-row print-row db "select * from bookmark")
(cond (if (url-string? (car args))
((url-string? (car args))
(with-transaction (with-transaction
db (lambda () (add-bookmark db (car args) (cadr args) (caddr args) (cdddr args)) #t))) 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 (else
(format #t "Unrecognized option: ~a~%" (car args))))) (format #t "Unrecognized option: ~a~%" (car args))))
(set! args (cdr args)))))
(finalize! db #t))) (finalize! db #t)))
(main (command-line-arguments)) (main (command-line-arguments))