David Krentzlin committed a57b1f1 Draft

stabelized commands api

Comments (0)

Files changed (3)


   (:documentation "Apply all processors to the messages and generate answers if apropriate")
   (:method ((kernel <kernel>) messages stop-processing)
     (labels ((skip (answers)
-	       (format t "Skipping messages ~%")
 	       (log5:log-for (debug mira.core.logging::kernel) "Skipping following messages")
 	       (when answers
 		 (dolist (answer (ensure-list answers))


 (defun cmd-answer (env &rest arguments)
   (let ((answer-fn (command-environment-answer-fn env)))
-    (format t "Answerfn is ~A ~S~%" answer-fn arguments)
     (apply answer-fn arguments)))
 (defun answer (message result &key (channels) (sender-id) (receiver (sender message))
 (defgeneric apply-command (command argument-string command-env)
   (:documentation "Applies the command to the given input")
   (:method ((command <command>) arguments command-env)
-    (let ((argument-list (argument-string->argument-list arguments))
-          (cmd-closure   (body command)))
-      (if (equal (string-downcase (car argument-list)) "help")
+    (let* ((argument-list (argument-string->argument-list arguments))
+	   (cmd-closure   (body command))
+	   (first-arg     (car argument-list)))
+      (if (and first-arg (stringp first-arg) (equal (string-downcase (car argument-list)) "help"))
           (cmd-answer command-env (help command))
-	  (apply cmd-closure (cons command-env argument-list))
-          ;; (handler-case 
-	  ;;     (progn
-	  ;; 	(format t "Applying command to ~A~%" argument-list)
-	  ;; 	(apply cmd-closure argument-list))
-          ;;   (error ()
-	  ;;     (format t "Have an error ~A ~%" (usage command))
-          ;;     (answer (usage command))))
+	 ;; (apply cmd-closure (cons command-env argument-list))
+	  (handler-case 
+	      (progn
+	   	(apply cmd-closure (cons command-env argument-list)))
+	    (error ()
+	      (cmd-answer command-env (usage command))))
 (defmethod print-object ((object <processor>) stream)
 	(values (string-trim prefix stripped) ""))))
 ;; foo bar baz -> (list "foo" "bar" "baz")
 ;; foo :bar 0  -> (list "foo" :bar 0)
 (defun argument-string->argument-list (argument-string)
 	(options-start "Options: ")
 	(options-strings (format-options options)))
     (if options
-	(list help-string usage-string options-start options-strings)
+	(append  (list help-string usage-string options-start) options-strings)
 	(list help-string usage-string))))
 (defun format-options (options)


 (defmethod ensure-list (data)
   "If data is a list it returns just data, otherwise it returns a list with data as its sole argument"
   (if (listp data) data (list data)))
+(defun list->string (ls)
+  (if (stringp ls) ls  (format nil "~{~A~^ ~}" ls)))