From c7f9d276387028026bfa274e3e6b51e56a9bff5e Mon Sep 17 00:00:00 2001 From: Tom Willemsen Date: Sat, 23 Mar 2013 16:31:22 +0100 Subject: [PATCH] Improve help messages The `help' command can now be used to view help about other commands. --- lisp/clark.lisp | 61 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 16 deletions(-) diff --git a/lisp/clark.lisp b/lisp/clark.lisp index 5dc294b..ed7743f 100644 --- a/lisp/clark.lisp +++ b/lisp/clark.lisp @@ -30,17 +30,17 @@ (defvar *max-command-name-length* 0 "Lenght of the longest command name.") -(defmacro defcommand (name doc &body body) +(defmacro defcommand (name sdoc ldoc &body body) "Define a new command usable on the command-line." (let* ((sname (symbol-name name)) (command-name (make-command-name sname))) `(progn (defun ,command-name (args) - ,doc + ,sdoc ,@body) (setf *help-messages* (nconc *help-messages* - '((,(string-downcase sname) ,doc ""))) + '((,(string-downcase sname) ,sdoc ,ldoc))) *max-command-name-length* (max *max-command-name-length* (length ,sname)))))) @@ -88,6 +88,22 @@ The result contains the url and the name of the bookmark." "Get the rowid of tag NAME." (execute-single *db* "SELECT rowid FROM tag WHERE name = ?" name)) +(defun help-message () + (format t (concatenate + 'string + "Usage: clark [ [ ...]]~%" + "~%" + "Possible commands:~%" + "~%")) + (map nil (lambda (hlp) + (destructuring-bind (name short long) hlp + (declare (ignore long)) + (format t " ~vA ~A~%" *max-command-name-length* + name short))) *help-messages*) + (format t "~%~A~%" + (concatenate 'string "Use `clark help ' to get more " + "information on a command."))) + (defun insert-bookmark (url name description) "Insert URL, NAME and DESCRIPTION into the bookmark table." (execute-non-query *db* "INSERT INTO bookmark VALUES (?, ?, ?, ?)" @@ -135,6 +151,10 @@ BM should be a list containing the url and name of the bookmark." (defcommand add "Add a new bookmark." + "Usage: clark add [ ...] + +Add URL with NAME, DESCRIPTION and TAGS to the database. TAGS may be +omitted or any number of tag names." (with-transaction *db* (destructuring-bind (url name description &rest tags) args (insert-bookmark url name description) @@ -142,6 +162,10 @@ BM should be a list containing the url and name of the bookmark." (defcommand exists "Check if a bookmark exists in the database." + "Usage: clark exists + +Check if URL exists in the database. Prints `yes' when found and `no' +otherwise." (if (execute-single *db* "SELECT rowid FROM bookmark WHERE url = ?" (car args)) (progn @@ -153,22 +177,24 @@ BM should be a list containing the url and name of the bookmark." (defcommand help "Show help message." - (declare (ignore args)) - (format t (concatenate - 'string - "Usage: clark [ [ ...]]~%" - " clark add [ ...]~%" - "~%" - "Possible commands:~%" - "~%")) - (map nil (lambda (hlp) - (destructuring-bind (name short long) hlp - (declare (ignore long)) - (format t " ~vA ~A~%" *max-command-name-length* - name short))) *help-messages*)) + help-message + (if (> (length args) 0) + (let ((ldoc + (nth 2 (car (member + (car args) *help-messages* + :test #'(lambda (x y) (equal x (car y)))))))) + (cond + ((null ldoc) (format t "Unkown command: ~A~%" (car args))) + ((and (symbolp ldoc) (fboundp ldoc)) (funcall ldoc)) + (t (format t "~A~%" ldoc)))) + (help-command '("help")))) (defcommand search "Search through bookmarks." + "Usage: clark search + +Search the database for STR. Matches are made for substrings of a +bookmark's name or an exact match for a tag." (map nil (lambda (bm) (destructuring-bind (url name description) bm @@ -186,6 +212,9 @@ BM should be a list containing the url and name of the bookmark." (defcommand version "Show version." + "Usage: clark version + +Print the version number and exit." (declare (ignore args)) (format t "clark version ~A~%" *version*))