# HG changeset patch # User Richard Westhaver # Date 1726092509 14400 # Node ID 95fd920af398014206f8782e56d437ac8419b7b8 # Parent 3e6a17fb5712551535a1406b5294135fc4603190 error handling methods for clap diff -r 3e6a17fb5712 -r 95fd920af398 lisp/lib/cli/clap/cmd.lisp --- a/lisp/lib/cli/clap/cmd.lisp Wed Sep 11 17:24:07 2024 -0400 +++ b/lisp/lib/cli/clap/cmd.lisp Wed Sep 11 18:08:29 2024 -0400 @@ -65,6 +65,16 @@ (defmethod pop-opt ((self cli-opt)) (vector-pop (cli-opts self))) +(defmethod handle-unknown-opt ((self cli-cmd) (opt string)) + (with-opt-restart-case opt + (clap-unknown-argument opt 'cli-opt))) + +(defmethod handle-invalid-opt ((self cli-cmd) (opt string) &optional reason) + (clap-invalid-argument opt :kind 'cli-opt :reason reason)) + +(defmethod handle-missing-opt ((self cli-cmd) (opt string)) + (clap-missing-argument opt 'cli-opt)) + (defmethod cli-equal ((a cli-cmd) (b cli-cmd)) (with-slots (name opts cmds) a (with-slots ((bn name) (bo opts) (bc cmds)) b diff -r 3e6a17fb5712 -r 95fd920af398 lisp/lib/cli/clap/pkg.lisp --- a/lisp/lib/cli/clap/pkg.lisp Wed Sep 11 17:24:07 2024 -0400 +++ b/lisp/lib/cli/clap/pkg.lisp Wed Sep 11 18:08:29 2024 -0400 @@ -33,7 +33,15 @@ :clap-simple-error :clap-simple-warning :clap-warning - :clap-unknown-argument)) + :clap-unknown-argument + :handle-unknown-opt + :handle-missing-opt + :handle-invalid-opt + :handle-unknown-arg + :handle-missing-arg + :handle-invalid-arg + :clap-missing-argument + :clap-invalid-argument)) (defpackage :cli/clap/ast (:use :cl :std :log :dat/sxp) diff -r 3e6a17fb5712 -r 95fd920af398 lisp/lib/cli/clap/proto.lisp --- a/lisp/lib/cli/clap/proto.lisp Wed Sep 11 17:24:07 2024 -0400 +++ b/lisp/lib/cli/clap/proto.lisp Wed Sep 11 18:08:29 2024 -0400 @@ -11,10 +11,19 @@ (defwarning clap-warning (clap-condition) ()) (deferror clap-simple-error (simple-error clap-error) () (:auto t)) (deferror clap-unknown-argument (clap-error unknown-argument) ()) + (deferror clap-missing-argument (clap-error missing-argument) + ((kind :initarg :kind :initform nil))) + (deferror clap-invalid-argument (clap-error invalid-argument) ()) (defwarning clap-simple-warning (simple-warning clap-warning) () (:auto t))) -(defun clap-unknown-argument (opt) - (error 'clap-unknown-argument :name opt :kind 'cli-opt)) +(defun clap-unknown-argument (arg &optional kind) + (error 'clap-unknown-argument :name arg :kind kind)) + +(defun clap-missing-argument (arg &optional kind) + (error 'clap-missing-argument :item arg :kind kind)) + +(defun clap-invalid-argument (arg &key reason kind) + (error 'clap-invalid-argument :name arg :kind kind :reason reason)) (defgeneric push-cmd (cmd place)) @@ -54,13 +63,22 @@ (defgeneric print-usage (self &optional stream) (:documentation "Format cli SELF as a useful string.")) -(defgeneric handle-unknown-argument (self arg) - (:documentation "Handle an unknown argument.")) +(defgeneric handle-unknown-opt (self opt) + (:documentation "Handle an unknown cli-opt.")) + +(defgeneric handle-missing-opt (self opt) + (:documentation "Handle a missing cli-opt.")) + +(defgeneric handle-invalid-opt (self opt &optional reason) + (:documentation "Handle an invalid cli-opt.")) -(defgeneric handle-missing-argument (self arg) - (:documentation "Handle a missing argument.")) +(defgeneric handle-unknown-arg (self arg) + (:documentation "Handle an unknown cli-arg.")) -(defgeneric handle-invalid-argument (self arg) - (:documentation "Handle an invalid argument.")) +(defgeneric handle-missing-arg (self arg) + (:documentation "Handle a missing cli-arg.")) + +(defgeneric handle-invalid-arg (self arg &optional reason) + (:documentation "Handle an invalid cli-arg.")) (defgeneric cli-equal (a b)) diff -r 3e6a17fb5712 -r 95fd920af398 lisp/std/condition.lisp --- a/lisp/std/condition.lisp Wed Sep 11 17:24:07 2024 -0400 +++ b/lisp/std/condition.lisp Wed Sep 11 18:08:29 2024 -0400 @@ -172,15 +172,11 @@ ((item :initarg :item :initform (error "Must specify argument item") - :reader missing-argument-item) - (command - :initarg :command - :initform (error "Must specify command") - :reader missing-argument-command)) - (:report (lambda (condition stream) - (declare (ignore condition)) - (format stream "Missing argument"))) - (:documentation "A condition which is signalled when an option expects an argument, but none was provided")) + :reader missing-argument-item)) + (:report (lambda (condition stream) + (declare (ignore condition)) + (format stream "Missing argument"))) + (:documentation "A condition which is signalled when an option expects an argument, but none was provided")) (defun missing-argument-p (value) (typep value 'missing-argument))