Comments (3)
Hey @dan-passaro ,
Could you please post some sample code that you have, so I can better understand what you are trying to accomplish?
Thanks!
from clingon.
Sure, here's an example:
(defpackage :clingon-issue
(:use :cl))
(in-package :clingon-issue)
(ql:quickload :clingon)
(ql:quickload :str)
(defclass 2d-coordinates (clingon:option)
()
(:default-initargs :parameter "INTEGER,INTEGER")
(:documentation "Parse an 'INTEGER,INTEGER' CLI option, producing a cons."))
(defmethod clingon:derive-option-value ((self 2d-coordinates) arg &key)
(handler-case
(let ((coords (mapcar 'parse-integer (str:split "," arg))))
(destructuring-bind (x y) coords
(cons x y)))
(error () (error 'clingon:option-derive-error :reason "whoops"))))
(defmethod clingon:make-option ((kind (eql :2d-coordinates)) &rest rest)
(apply 'make-instance '2d-coordinates rest))
(defun cli/options ()
(list
(clingon:make-option
:2d-coordinates
:long-name "spawn"
:key :spawn
:description "Set spawn point")))
(defun cli/handler (options)
(format t "Spawn point: ~S~%" (clingon:getopt options :spawn)))
(defun cli/command ()
(clingon:make-command
:name "program"
:options (cli/options)
:handler 'cli/handler))
(defun main ()
(clingon:run (cli/command)))
(main)
You can run using e.g. sbcl --load ~/quicklisp/setup.lisp --script clingon-issue.lisp
from clingon.
Hey @dan-passaro ,
An easy solution might be to provide more details about the error in the CLINGON:DERIVE-OPTION-VALUE
method, e.g.
(defmethod clingon:option-usage-details ((kind (eql :option-info)) (option clingon:option) &key)
(with-output-to-string (s)
(cond
;; Short and long names are defined
((and (clingon:option-short-name option) (clingon:option-long-name option))
(format s "-~A, --~A" (clingon:option-short-name option) (clingon:option-long-name option)))
;; We only have a short name defined
((clingon:option-short-name option)
(format s "-~A" (clingon:option-short-name option)))
;; Long name defined only
(t
(format s "--~A" (clingon:option-long-name option))))))
(defmethod make-option-derive-error ((option clingon:option) arg)
(let* ((info (clingon:option-usage-details :option-info option))
(reason (format nil "Invalid value for option ~A: ~A" info arg)))
(error 'clingon:option-derive-error :reason reason)))
(defmethod clingon:derive-option-value ((self 2d-coordinates) arg &key)
(handler-case
(let ((coords (mapcar 'parse-integer (str:split "," arg))))
(destructuring-bind (x y) coords
(cons x y)))
(error () (make-option-derive-error self arg))))
This would now print the following.
Invalid value for option --spawn: 3,4.1
Another way, which we can fix this (and I think that's the better approach) is to add another slot to CLINGON:OPTION-DERIVE-ERROR
condition, which carries an instance of the option which has failed.
Then, this option instance can be inspected by the CLINGON:RUN
method, and if we see such errors we can print the error there instead.
This however would mean that we need to have a breaking API change, as then CLINGON:OPTION-DERIVE-ERROR
will expect an instance of an option.
Let me know what do you think.
Thanks!
from clingon.
Related Issues (19)
- [Question] Having a subcommand that doesn't take any arguments or options HOT 2
- "non-consuming" options? That is options with optional values of specific types which shouldn't signal errors but rather not consume the argument. HOT 6
- Trying to handle parse errors inside run (to use unknown options as free args) HOT 2
- Proposal: running clingon:run and not exit-ing when in a dumb terminal (like Slime) HOT 7
- Desactivating quit/exit at the REPL if desired HOT 8
- About line breaks in the description HOT 3
- Question: Clingon and buildapp? HOT 4
- Support named positional arguments HOT 1
- How can I add a new option type like Python click's nargs? HOT 2
- completions.bash doesn't work. HOT 4
- Documentation request HOT 1
- Searching for options in parent commands HOT 7
- Documentation about extending and adding aliases to pre-existing commands? HOT 10
- [SBCL] --help and --version print SBCL's help and version HOT 4
- --version for subcommands HOT 2
- Tags and Quicklisp HOT 1
- Simple question about README HOT 2
- Feature enhancement: float options HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from clingon.