changeset 646: |
95fd920af398 |
parent 645: |
3e6a17fb5712 |
child 647: |
74e563ed4537 |
author: |
Richard Westhaver <ellis@rwest.io> |
date: |
Wed, 11 Sep 2024 18:08:29 -0400 |
files: |
lisp/lib/cli/clap/cmd.lisp lisp/lib/cli/clap/pkg.lisp lisp/lib/cli/clap/proto.lisp lisp/std/condition.lisp |
description: |
error handling methods for clap |
1.1--- a/lisp/lib/cli/clap/cmd.lisp Wed Sep 11 17:24:07 2024 -0400
1.2+++ b/lisp/lib/cli/clap/cmd.lisp Wed Sep 11 18:08:29 2024 -0400
1.3@@ -65,6 +65,16 @@
1.4 (defmethod pop-opt ((self cli-opt))
1.5 (vector-pop (cli-opts self)))
1.6
1.7+(defmethod handle-unknown-opt ((self cli-cmd) (opt string))
1.8+ (with-opt-restart-case opt
1.9+ (clap-unknown-argument opt 'cli-opt)))
1.10+
1.11+(defmethod handle-invalid-opt ((self cli-cmd) (opt string) &optional reason)
1.12+ (clap-invalid-argument opt :kind 'cli-opt :reason reason))
1.13+
1.14+(defmethod handle-missing-opt ((self cli-cmd) (opt string))
1.15+ (clap-missing-argument opt 'cli-opt))
1.16+
1.17 (defmethod cli-equal ((a cli-cmd) (b cli-cmd))
1.18 (with-slots (name opts cmds) a
1.19 (with-slots ((bn name) (bo opts) (bc cmds)) b
2.1--- a/lisp/lib/cli/clap/pkg.lisp Wed Sep 11 17:24:07 2024 -0400
2.2+++ b/lisp/lib/cli/clap/pkg.lisp Wed Sep 11 18:08:29 2024 -0400
2.3@@ -33,7 +33,15 @@
2.4 :clap-simple-error
2.5 :clap-simple-warning
2.6 :clap-warning
2.7- :clap-unknown-argument))
2.8+ :clap-unknown-argument
2.9+ :handle-unknown-opt
2.10+ :handle-missing-opt
2.11+ :handle-invalid-opt
2.12+ :handle-unknown-arg
2.13+ :handle-missing-arg
2.14+ :handle-invalid-arg
2.15+ :clap-missing-argument
2.16+ :clap-invalid-argument))
2.17
2.18 (defpackage :cli/clap/ast
2.19 (:use :cl :std :log :dat/sxp)
3.1--- a/lisp/lib/cli/clap/proto.lisp Wed Sep 11 17:24:07 2024 -0400
3.2+++ b/lisp/lib/cli/clap/proto.lisp Wed Sep 11 18:08:29 2024 -0400
3.3@@ -11,10 +11,19 @@
3.4 (defwarning clap-warning (clap-condition) ())
3.5 (deferror clap-simple-error (simple-error clap-error) () (:auto t))
3.6 (deferror clap-unknown-argument (clap-error unknown-argument) ())
3.7+ (deferror clap-missing-argument (clap-error missing-argument)
3.8+ ((kind :initarg :kind :initform nil)))
3.9+ (deferror clap-invalid-argument (clap-error invalid-argument) ())
3.10 (defwarning clap-simple-warning (simple-warning clap-warning) () (:auto t)))
3.11
3.12-(defun clap-unknown-argument (opt)
3.13- (error 'clap-unknown-argument :name opt :kind 'cli-opt))
3.14+(defun clap-unknown-argument (arg &optional kind)
3.15+ (error 'clap-unknown-argument :name arg :kind kind))
3.16+
3.17+(defun clap-missing-argument (arg &optional kind)
3.18+ (error 'clap-missing-argument :item arg :kind kind))
3.19+
3.20+(defun clap-invalid-argument (arg &key reason kind)
3.21+ (error 'clap-invalid-argument :name arg :kind kind :reason reason))
3.22
3.23 (defgeneric push-cmd (cmd place))
3.24
3.25@@ -54,13 +63,22 @@
3.26 (defgeneric print-usage (self &optional stream)
3.27 (:documentation "Format cli SELF as a useful string."))
3.28
3.29-(defgeneric handle-unknown-argument (self arg)
3.30- (:documentation "Handle an unknown argument."))
3.31+(defgeneric handle-unknown-opt (self opt)
3.32+ (:documentation "Handle an unknown cli-opt."))
3.33+
3.34+(defgeneric handle-missing-opt (self opt)
3.35+ (:documentation "Handle a missing cli-opt."))
3.36+
3.37+(defgeneric handle-invalid-opt (self opt &optional reason)
3.38+ (:documentation "Handle an invalid cli-opt."))
3.39
3.40-(defgeneric handle-missing-argument (self arg)
3.41- (:documentation "Handle a missing argument."))
3.42+(defgeneric handle-unknown-arg (self arg)
3.43+ (:documentation "Handle an unknown cli-arg."))
3.44
3.45-(defgeneric handle-invalid-argument (self arg)
3.46- (:documentation "Handle an invalid argument."))
3.47+(defgeneric handle-missing-arg (self arg)
3.48+ (:documentation "Handle a missing cli-arg."))
3.49+
3.50+(defgeneric handle-invalid-arg (self arg &optional reason)
3.51+ (:documentation "Handle an invalid cli-arg."))
3.52
3.53 (defgeneric cli-equal (a b))
4.1--- a/lisp/std/condition.lisp Wed Sep 11 17:24:07 2024 -0400
4.2+++ b/lisp/std/condition.lisp Wed Sep 11 18:08:29 2024 -0400
4.3@@ -172,15 +172,11 @@
4.4 ((item
4.5 :initarg :item
4.6 :initform (error "Must specify argument item")
4.7- :reader missing-argument-item)
4.8- (command
4.9- :initarg :command
4.10- :initform (error "Must specify command")
4.11- :reader missing-argument-command))
4.12- (:report (lambda (condition stream)
4.13- (declare (ignore condition))
4.14- (format stream "Missing argument")))
4.15- (:documentation "A condition which is signalled when an option expects an argument, but none was provided"))
4.16+ :reader missing-argument-item))
4.17+ (:report (lambda (condition stream)
4.18+ (declare (ignore condition))
4.19+ (format stream "Missing argument")))
4.20+ (:documentation "A condition which is signalled when an option expects an argument, but none was provided"))
4.21
4.22 (defun missing-argument-p (value)
4.23 (typep value 'missing-argument))