Giter Club home page Giter Club logo

Comments (3)

dnaeon avatar dnaeon commented on August 18, 2024

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.

dan-passaro avatar dan-passaro commented on August 18, 2024

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.

dnaeon avatar dnaeon commented on August 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.