Fork me on GitHub
#babashka
<
2022-11-10
>
cap10morgan17:11:10

In 1.0.165, it seems like this doesn't work (from the Babashka book): "Invoking a main function can be done by providing a fully qualified symbol" and then the example shows just a namespace without the main fn (specifically {:tasks {foo-bar foo.bar}}). but when I do that I get Could not resolve symbol: my.ns errors. but if I suffix it with /-main it does work. is that the intended behavior? am I misreading the book?

cap10morgan18:11:43

Is it possible to support subcommands w/ exec? Seems like the cli auto-coercion drops them before they get to my main fn.

borkdude18:11:52

Perhaps. Do you have an example that you would like to see?

cap10morgan19:11:11

if I have a task that wraps its invoked fn in exec and I invoke it like bb mytask sub --foo bar currently I get args like this: ({:foo "bar"}) but if I could get something like this instead I think I'd be good to go: ("sub" {:foo "bar"})

borkdude19:11:27

@cap10morgan The sub is likely on the metadata of the input map

cap10morgan19:11:07

hmm... not seeing it anywhere in the metadata of the args I get (neither the list nor the map inside it)

borkdude19:11:25

why do you get a list? the exec function should be a 1 arg function

borkdude19:11:40

did you make it varargs?

cap10morgan19:11:55

ah, yeah. I always do w/ -main

borkdude19:11:30

-main is typically called with string arguments, varargs. exec function has 1 argument, a map

borkdude19:11:39

this map has metadata with commands as well

cap10morgan19:11:01

ok. I looked at that metadata but didn't see it. I'll try it w/o varargs

borkdude19:11:09

if you want to do something else, you could just use babashka.cli programmatically

cap10morgan19:11:50

(meta args) still returns nil w/o varargs

borkdude19:11:25

that surprises me

borkdude19:11:38

what does your function look like

cap10morgan19:11:50

(defn -main [args]
  (println "coerced args:" (pr-str args))
  (println "args metadata:" (-> args meta pr-str)))

cap10morgan19:11:13

and bb.edn:

{:paths ["src"]
 :deps  {local/deps {:local/root "."}}
 :pods  {com.fluree/db {:path "../pod-fluree-db/pod-fluree-db"}}
 :tasks {fdb (exec 'fluree.db.cli/-main)}}

borkdude19:11:35

while with babashka.cli.exec the metadata is preserved since the return value of parse-opts is returned: https://github.com/babashka/cli/blob/23f817f83046ff59a1d4ffc30c43eafec9b30bd4/src/babashka/cli/exec.clj#L59

borkdude19:11:17

we should probably change that "exec-fn-snippet" to match babashka.cli.exec to gain that metadata back

borkdude19:11:43

issue and/or PR welcome

borkdude19:11:55

the crux is that parse-opts returns metadata on the parsed options

borkdude18:11:21

@cap10morgan I wondered about this myself too recently: I expected -main not to be necessary. Perhaps something broke, not sure. Can you try with an older version of bb?

cap10morgan19:11:29

doesn't seem to work (i.e. requires /-main) on 0.9.160, 0.8.0, nor 0.6.0

borkdude19:11:10

then the docs are probably wrong ;)

cap10morgan19:11:55

ok, yeah, that seems right. I think it's not a big deal to require /-main as long as the docs are updated to reflect that

borkdude19:11:20

issue or PR welcome

👍 1