libre-man / unix-opts

Unix-style command line options parser
MIT License
107 stars 13 forks source link

A shorter unix-opts:define-opts for "common-case" worth the code? #19

Open digikar99 opened 4 years ago

digikar99 commented 4 years ago

The following macro

(defmacro expand-opts (&rest descriptions)
  "Each description should be of the format (name-as-keyword description &optional parser)."
  `(opts:define-opts
     ,@(let* ((used-short-names (make-hash-set))
              (get-unique-short-name
               (lambda (name)
                 (iter (for ch in-string name)
                       (when (and (not (hs-memberp used-short-names ch))
                                  (alpha-char-p ch))
                         (hs-ninsert used-short-names ch)
                         (return ch))
                       (finally (error #?"Could not find a unique short name for ${name}"))))))
         (iter (for description in descriptions)
               (destructuring-bind (name description &optional parser short) description
                 (let ((name-string (string-downcase (symbol-name name))))
                   (collect `(:name ,name
                                    :description ,description
                                    :short ,(or short (funcall get-unique-short-name
                                                               name-string))
                                    :long ,name-string
                                    :arg-parser ,parser))))))))

lets me use something shorter instead of unix-opts:define-opts:

(expand-opts
 (:help "Show this help text.")
 (:port "Port number on which to run the server." #'parse-integer)
 (:swank-port "Port number at which to start swank [default: 8080]" #'parse-integer)
 (:debug "Run in debug mode if specified" #'identity))

This doesn't take into account the case for :meta-var and :required, which I'd hope isn't "common case" either, or may be :required is, and can be easily accomodated.

vindarel commented 4 years ago

that's something I wanted to do in a project of mine, thanks for sharing.

libre-man commented 4 years ago

Hi @digikar99,

I agree that this would be useful, is it OK if I use the specified macro as a starting point for something in the repo? This would mean you release the code as something MIT licensed.

digikar99 commented 4 years ago

Oh yes, sure! I've in fact added to a branch on my fork, along with a few other things - and can issue a PR.

libre-man commented 4 years ago

Oh please do! Sorry I haven't been active for some while, I'm trying to catch up now :)

digikar99 commented 4 years ago

Oh yes, that's alright; all of this is volunteer work, with no obligation for us to do it; it's okay to have other tasks that require our more immediate attention :D.

The PR is at #26. (For anyone who stumbles upon this thread in the future.)