Add --script option

When passing in the --script option all output will be formatted in a
machine-readable way.
This commit is contained in:
Tom Willemsen 2013-03-23 17:58:31 +01:00
parent 50bb0f58ea
commit 3b165c81d9

View file

@ -30,6 +30,9 @@
(defvar *max-command-name-length* 0 (defvar *max-command-name-length* 0
"Length of the longest command name.") "Length of the longest command name.")
(defvar *script* nil
"Whether or not to output in a machine-readable format.")
(defmacro call-command (name &rest args) (defmacro call-command (name &rest args)
(let ((command-name (make-command-name (symbol-name name)))) (let ((command-name (make-command-name (symbol-name name))))
`(,command-name ,@args))) `(,command-name ,@args)))
@ -133,19 +136,28 @@ The result contains the url, name and the description of the bookmark."
"Parse command-line arguments ARGS. "Parse command-line arguments ARGS.
The executable name should already have been removed." The executable name should already have been removed."
(let ((cmd-name (make-command-name (car args)))) (loop while (and args (char= (char (car args) 0) #\-))
(if (fboundp cmd-name) do (case (intern (string-upcase (string-left-trim "-" (car args)))
(funcall cmd-name (cdr args)) :org.ryuslash.clark)
(progn (script (setf *script* t args (cdr args)))))
(format t "Unknown command: ~A~%" (car args)) (if args
(call-command help nil))))) (let ((cmd-name (make-command-name (car args))))
(if (fboundp cmd-name)
(funcall cmd-name (cdr args))
(progn
(format t "Unknown command: ~A~%" (car args))
(call-command help nil))))
(map nil #'print-bookmark (get-bookmarks))))
(defun print-bookmark (bm) (defun print-bookmark (bm)
"Print information about bookmark BM. "Print information about bookmark BM.
BM should be a list containing the url and name of the bookmark." BM should be a list containing the url, name and description of the
(destructuring-bind (url name) bm bookmark."
(format t "~A~%~A~%~%" url name))) (destructuring-bind (url name description) bm
(if *script*
(format t "~A~A~A" name description url)
(format t "~A~% ~A~% ~A~%~%" url name description))))
(defcommand add (defcommand add
"Add a new bookmark." "Add a new bookmark."
@ -188,20 +200,17 @@ otherwise."
Search the database for STR. Matches are made for substrings of a Search the database for STR. Matches are made for substrings of a
bookmark's name or an exact match for a tag." bookmark's name or an exact match for a tag."
(map (map nil #'print-bookmark
nil (lambda (bm) (execute-to-list
(destructuring-bind (url name description) bm *db* (concatenate 'string
(format t "~A~% ~A~% ~A~%~%" url name description))) "SELECT url, name, description "
(execute-to-list "FROM bookmark "
*db* (concatenate 'string "WHERE name LIKE ? "
"SELECT url, name, description " "OR ? IN (SELECT name "
"FROM bookmark " "FROM tag "
"WHERE name LIKE ? " "JOIN bookmark_tag ON (tag_id = tag.rowid) "
"OR ? IN (SELECT name " "WHERE bookmark_id = bookmark.rowid)")
"FROM tag " (format nil "%~A%" (car args)) (car args))))
"JOIN bookmark_tag ON (tag_id = tag.rowid) "
"WHERE bookmark_id = bookmark.rowid)")
(format nil "%~A%" (car args)) (car args))))
(defcommand version (defcommand version
"Show version." "Show version."
@ -217,7 +226,5 @@ Print the version number and exit."
Connect to the database, parse command-line arguments, execute and Connect to the database, parse command-line arguments, execute and
then disconnect." then disconnect."
(load-db) (load-db)
(if (> (length args) 1) (parse-args (cdr args))
(parse-args (cdr args))
(map nil #'print-bookmark (get-bookmarks)))
(disconnect *db*)) (disconnect *db*))