`args->opts` does not handle positional args after `--`

Open
#241 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

  • #242 by @codebeige — closed without merging

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Domain
cli

Research direction

Start at parse-args in babashka/cli.cljc:908 and trace how -- separates positional arguments from the remaining args; compare that path with the existing :args->opts processing. Check the CLI tests for :args->opts and :restrict-args, then add coverage showing that post--- values are validated, coerced, and included in the option collection.

Written by the indexing model from the issue text.

Description

version

0.12.92

platform

macos/arm64

problem

Positional arguments following -- are not handled by :args->opts and therefore stay unprocessed in :args without being added to the option's collection.

repro

(babashka.cli/parse-args ["foo" "bar" "--" "baz"]
                         {:args->opts [:positional-args]
                          :spec [[:positional-args {:coerce []}]]})
; => {:args ["baz"], :opts {:positional-args ["foo" "bar"]}}

With :restrict-args enabled, the following error is expected given the above behavior, but I think it is still be worth mentioning:

 % bb -e '(babashka.cli/parse-args ["foo" "bar" "--" "baz"] {:args->opts [:positional-args] :restrict-args true :spec [[:positional-args {:coerce []}]]})'
----- Error --------------------------------------------------------------------
Type:     clojure.lang.ExceptionInfo
Message:  Unexpected argument: baz
Data:     {:spec [[:positional-args {:coerce []}]], :type :org.babashka/cli, :cause :restrict-args, :msg "Unexpected argument: baz", :value "baz", :opts {:positional-args ["foo" "bar"]}}
Location: NO_SOURCE_PATH:1:1

----- Context ------------------------------------------------------------------
1: (babashka.cli/parse-args ["foo" "bar" "--" "baz"] {:args->opts [:positional-args] :restrict-args true :spec [[:positional-args {:coerce []}]]})
   ^--- Unexpected argument: baz

----- Stack trace --------------------------------------------------------------
babashka.cli/->error-fn/fn--24916 - <built-in>
babashka.cli/->error-fn/fn--24918 - <built-in>
babashka.cli/validate-opts        - <built-in>
babashka.cli/parse-opts           - <built-in>
babashka.cli/parse-args           - babashka/cli.cljc:908:1
user                              - NO_SOURCE_PATH:1:1

expected behavior

Positional arguments following -- undergo the same processing (i.e. validation, coercion) as the ones before it, and everything is folded into the option's collection.

(babashka.cli/parse-args ["foo" "bar" "--" "baz"]
                         {:args->opts [:positional-args]
                          :spec [[:positional-args {:coerce []}]]})
; => {:opts {:positional-args ["foo" "bar" "baz"]}}
Dominant language
Clojure
Stars
305
Forks
24
Avg merge
1m
Merged PRs (30d)
1

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from babashka/cli

All issues in babashka/cli

Similar issues

More Clojure issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.