Fork me on GitHub
#babashka
<
2022-07-27
>
borkdude10:07:51

Taking #babashka-cli to the next step by integrating it with babashka. Proposal: https://github.com/babashka/babashka/issues/1324

bmo 1
1
lread13:07:26

Cool! Are you considering argument validation support?

borkdude13:07:53

@UE21H2HHD Do you have any ideas on what that might look like?

borkdude13:07:00

Pseudo-code is welcome

borkdude13:07:19

Like:

{:validate {:foo #{:foo :bar}}
?

lread14:07:48

Sorry, had not given it much thought, but I do seem to like a cli that validates args and provides usage help.

borkdude14:07:42

The philosophy behind babashka CLI is that you would turn a function into a CLI. If you validate args when doing a CLI invocation, why should this validate and not when you invoke the function from a REPL?

borkdude14:07:03

Isn't CLI just a special case of a more general fn invocation?

borkdude14:07:16

You can use :pre in the function to support both

borkdude14:07:49

I'm just echoing one argument here, please change my mind, I'm curious to hear your thoughts

lread14:07:54

Ya, that's certainly a reasonable argument. I do like a consistent way of validating args and presenting usage help. I currently often use docopt these days, it is not perfect (I sometimes spin my wheels figuring out how to express things) but it helps with these goals.

lread14:07:45

Maybe I'm focusing on classic command line usage/validation/help. This might not be something babashka-cli wants to concern itself with.

borkdude14:07:23

yeah, I think it's what people expect and adding a :validate thing seems reasonable

emccue15:07:18

Maybe provide a helper to format malli schema validation errors in a "CLI appropriate form"?

emccue15:07:06

separate the parsing of flags to a map with displaying errors if that map aint good

borkdude15:07:26

@U3JH98J4R bb CLI has no dependency on malli / spec / schema

borkdude15:07:07

But just throwing an error with a configurable message upon the first failure seems reasonable

borkdude15:07:35

Maybe look at how tools.cli does this

borkdude15:07:06

:validate [#(< 0 % 0x10000) "Must be a number between 0 and 65536"]]
:validate [#(< 0 % 0x10000) #(str % " is not a number between 0 and 65536")]

borkdude15:07:14

instead of a tuple, we could have :validate be a function which receives a map

:validate {:fn #{:foo :bar} :error-fn (fn [{:keys [opt]}] {:message (str "O no!!!! opt")}) 
or so

borkdude15:07:35

But it seems you could do this fairly easily yourself after the options have been passed to the function

borkdude15:07:54

I don't understand why you would only throw on CLI invocation but give fn invocation from the REPL a free pass

borkdude15:07:24

But I'm still open to feedback, haven't made up my mind on this 100%

borkdude15:07:58

I guess you could say something similar for :closed and :required

borkdude15:07:34

I guess parse-args can return :errors thing, like tools.cli does: If the validation-fn returns a falsey value, the validation-msg is added to the errors vector.

lread19:07:24

I thinking it might be a matter of usage perspective. If as a user you know you are using babashka-cli as a way to invoke a Clojure function from the command line, you might be just fine with a stack trace as an error, and then having to look at a docstring and/or source for proper usage. If as a user you see yourself as using a command line tool, you expectations will likely be different. The Clojure fn being called is an implementation detail. You likely expect some --help or help option to be available, and likely expect textual error and/or usage help when you specify invalid input. When I write babashka tasks, I see myself as writing command line tools, so I (usually) try to make them follow the general conventions of command line tools.

emccue20:07:22

I agree, just thinking of what library would make it easy to do that

emccue20:07:11

So it's a disjoint task, I don't think babashka needs to support it so much as "map->CLI conventions" is a task people will want to do

borkdude20:07:39

I'm ok with adding some validation, ideas welcome of course

borkdude20:07:28

the bb -x foo/bar --dude case should then also handle the :errors if there are some, print those and exit with 1

borkdude21:07:24

I'm open to a PR for this

hlship21:07:13

I'm using babashka-cli, and just note that I expect the returned options map to use the long key (i.e. :pretty) rather than the short alias key (e.g., :p). Maybe I'm late to the discussion, but this is also more in line with clojure.tools.cli does.

borkdude21:07:37

@U04VDKC4G Let's go to #babashka-cli

borkdude21:07:40

I replied over there