Fork me on GitHub
#beginners
<
2024-05-16
>
Felix Dorner06:05:55

I'm puzzled about clj -X (and -M ). The documentation suggests that you can pass a map of function arguments. What if my function expects a single argument, e.g. a number? Can I not call it via -X without adapting the signature to accept a map?

Felix Dorner06:05:50

The https://clojure.org/guides/deps_and_cli would maybe benefit from adding an example that actually uses opts/arguments, e.g. print "Hello <name>" instead of "Hello World" if name is given a) as a single argument (if even possible?) b) as a map entry

plus_one 1
hiredman06:05:13

-X mandates a particular calling convention, it builds a map out of the list of arguments (calling read-string on each) and then passes it to the function

phronmophobic06:05:14

Can I not call it via -X without adapting the signature to accept a map?Unless there's some voodoo I'm unaware of, you cannot. There are several other good alternatives: • create a new function for adapting command line args • use -M • The syntax for specifying the map at the command line tries to help so using :num 42 might not be so bad. • There's also tricks you can do with aliases (see https://github.com/phronmophobic/clirun as an example) • There are also some libraries that can help. see https://github.com/babashka/cli