Skip to content

Commit 4f86a6e

Browse files
committed
Call parse of default value, as documented
Also when no `:handler` for a flag is specified Fixes #7
1 parent d627942 commit 4f86a6e

3 files changed

Lines changed: 24 additions & 21 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# Unreleased
22

3-
## Added
4-
53
## Fixed
64

7-
## Changed
5+
- Call `parse` of default value, as documented, also when no `:handler` for a
6+
flag is specified. Fixes #9
87

98
# 0.19.78 (2024-12-23 / a1f884e)
109

README.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,22 +158,26 @@ influence global behavior, like `--verbose`, `--silent`, or `--dry-run`.
158158

159159
At this point a few things are worth calling out.
160160

161-
- We support both `--flag ARG` and `--flag=<arg>` format in the flag
162-
specification, and accept both the version with and without a `=` when
163-
invoking, regardless of which version is used to define the flag.
161+
- To specifiy arguments to flags or subcommands we support both the all-caps
162+
version or the angle brackets, version, so `--flag ARG` or `--flag <arg>`.
163+
- You can optionally add an equal sign in between when specifying the argument,
164+
e.g. `--flag=<arg>` .
165+
- When invoking the command, both the version with or without `=` are
166+
understood, regardless of how the flag was specified.
164167
- If a flag is not predefined then only the `--flag=<arg>` version is able to
165-
pass along an argument, with `--flag arg` we assume that arg is a plain
166-
positional argument.
168+
pass along an argument, with `--flag arg` we assume that flag does not take
169+
any arguments, and arg is instead treated as a positional argument to the
170+
(sub-)command.
167171
- For flags without arguments, the default behavior is to count the number of
168172
flags. This is useful for things like `--verbose` that can be specified
169173
multiple times. For other cases you can simply treat it as a boolean.
170-
- The key that is used in the options map is based on the long-form
174+
- The key that is used in the options map is based on the long-form.
171175
(double-dash) form. So `-v, --verbose` means you'll get a `:verbose` key, for
172-
`-v` or `--verbose`
176+
`-v` or `--verbose`.
173177
- You can use the `--[no-]foo` syntax for adding both a `--foo` and a `--no-foo`
174178
flag, in this case the resulting value in the opts map will be `:foo true` or
175-
`:foo false`
176-
- You can add a `:default` to a flag, like `["--port PORT" {:default 8080}]`
179+
`:foo false`.
180+
- You can add a `:default` to a flag, like `["--port PORT" {:default 8080}]`.
177181
- You can set a `:parse` function which will be used to parse/coerce the
178182
argument. The default will parse numbers (basic longs and doubles, no special
179183
formats), and nothing else.
@@ -182,7 +186,7 @@ At this point a few things are worth calling out.
182186
the default to a string or a number, this will look better in the help text,
183187
where we show the default.
184188
- A single dash (`-`) is considered a positional argument, conventially
185-
indicating stdin/stdout
189+
indicating stdin/stdout.
186190
- To pass a positional argument that starts with a dash, prefix it with a
187191
backslash. lambdaisland/cli will remove the backslash, and treat the remainder
188192
as a positional argument rather than a flag. Note that the shell does its own

src/lambdaisland/cli.clj

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -291,14 +291,14 @@
291291
(defn add-defaults [init flagpairs]
292292
(reduce (fn [opts flagspec]
293293
(if-let [d (:default flagspec)]
294-
(if-let [h (:handler flagspec)]
295-
(binding [*opts* opts]
296-
(h opts (if (and (string? d) (:parse flagspec))
297-
((:parse flagspec default-parse) d)
298-
d)))
299-
(-> opts
300-
(assoc (:key flagspec) d)
301-
(assoc-in [::sources (:key flagspec)] (str (:flagstr flagspec) " (default value)"))))
294+
(let [d (if (and (string? d) (:parse flagspec))
295+
((:parse flagspec default-parse) d)
296+
d)]
297+
(if-let [h (:handler flagspec)]
298+
(binding [*opts* opts] (h opts d))
299+
(-> opts
300+
(assoc (:key flagspec) d)
301+
(assoc-in [::sources (:key flagspec)] (str (:flagstr flagspec) " (default value)")))))
302302
opts))
303303
init
304304
(map second flagpairs)))

0 commit comments

Comments
 (0)