l3nz / cli-matic

Compact, hands-free [sub]command line parsing library for Clojure.
Eclipse Public License 2.0
365 stars 29 forks source link

[Ftr] Add ability to provide custom validations and coercions #97

Open taxonomic-blackfish opened 4 years ago

taxonomic-blackfish commented 4 years ago

Is your feature request related to a problem? Please describe. I want to be able to provide a custom type where a coercion is applied. In my case, I have a set that is too large to be worth printing to the screen.

Describe the solution you'd like I would like an API where I can define functions that allows me to validate data, coerce data, and report errors to the user about what went wrong. If this exists, I did not see it in the docs.

For example:

{:type {;; validate returns `nil` on success and `str` on failure
        :validate (fn [x] (if (= (% x 2) 0) "No even values"))
        ;; coerce must always succeed (no error/exception)
        :coerce some-func-here}}

Or combine them into something (perhaps?) simpler.

{:type (fn [x] (if (= (% x 2) 0)
                 (throw (Exception. "No even values"))
                 (some-func-here x))}

Describe alternatives you've considered I can do all this boilerplate in my code, but if it's a global option, I have to do it with all subcommands. I would rather define these functions in one place and know that the option that's being passed to my function has the correct type/value.

I also attempted using :type (fn [x] x) to test if it accepts functions, and it does not (though this is clear after inspecting the code).

bartkl commented 2 years ago

I would love this functionality too.

I actually missed this issue when creating my own one, which is probably a duplicate, but I'll leave that up to the maintainer(s): #157