Skip to content

Commit

Permalink
Exit with error message if conditional arguments are missing
Browse files Browse the repository at this point in the history
  • Loading branch information
plexus committed May 27, 2024
1 parent 684b90d commit 3b81e73
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
8 changes: 3 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
# Unreleased

## Added

## Fixed

## Changed

- Exit with error message if conditional arguments are missing

# 0.8.39 (2024-05-24 / e967e9c)

## Added
Expand Down Expand Up @@ -94,4 +92,4 @@ approaching the envisioned scope for this library.

- subcommand handling
- rudimentary flag handling
- help text generation
- help text generation
10 changes: 7 additions & 3 deletions src/lambdaisland/cli.clj
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@
(defn long? [f]
(re-find #"^--.*" f))

(defn print-help [cmd-name doc command-pairs flagpairs]
(defn print-help [cmd-name doc command-pairs argnames flagpairs]
(let [desc #(str (first (str/split (:doc % "") #"\R"))
(when-let [d (:default %)] (str " (default " (pr-str d) ")")))]
(println (str "Usage: " cmd-name
(when (seq argnames)
(str/join (for [n argnames]
(str " <" (name n) ">"))))
(str/join (for [[_ {:keys [flags argdoc]}] flagpairs]
(str " [" (str/join " | " flags) argdoc "]")))
(when (seq command-pairs)
Expand Down Expand Up @@ -334,8 +337,8 @@

(cond
command
(if (:help opts)
(print-help program-name doc [] flagpairs)
(if (or (:help opts) (< (count pos-args) (count argnames)))
(print-help program-name doc [] argnames flagpairs)
(binding [*opts* (-> opts
(dissoc ::middleware)
(update ::argv (fnil into []) pos-args)
Expand Down Expand Up @@ -374,6 +377,7 @@
[k (if (:commands v)
(update v :commands prepare-cmdpairs)
v)])
argnames
flagpairs)

:else
Expand Down

0 comments on commit 3b81e73

Please sign in to comment.