Skip to content

Commit

Permalink
Merge pull request #138 from tionis/argparse
Browse files Browse the repository at this point in the history
fixed docstring of argparse and added collection of unparsed arguments
  • Loading branch information
bakpakin authored Jul 13, 2023
2 parents 08eb46e + 8aa5a3b commit 213842c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
16 changes: 10 additions & 6 deletions spork/argparse.janet
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
* `:help` - Help text for the option, explaining what it is.
* `:default` - Default value for the option.
* `:required` - Whether or not an option is required.
* `:short-circuit` - Whether or not to stop parsing and fail if this option is hit.
* `:short-circuit` - Whether or not to stop parsing if this option is hit.
Result will also contain the rest of the arguments under the :rest key.
* `:action` - A function that will be invoked when the option is parsed.
* `:map` - A function that is applied to the value of the option to transform it
Expand All @@ -46,7 +47,7 @@
Once parsed, values are accessible in the returned table by the name
of the option. For example `(result "verbose")` will check if the verbose
flag is enabled.
You may also use a custom args array when specified via the special option `:args`
You may also use a custom args array when specified via the special option `:args`.
```
[description &keys options]

Expand All @@ -69,7 +70,7 @@
(def res @{:order @[]})
(def args (if-let [args (options :args)] (do (put options :args nil) args) (dyn :args)))
(def arglen (length args))
(var scanning true)
(var parsing true)
(var bad false)
(var i 1)
(var process-options? true)
Expand All @@ -80,7 +81,7 @@
# Only show usage once.
(if bad (break))
(set bad true)
(set scanning false)
(set parsing false)
(unless (empty? msg)
(print "usage error: " ;msg))
(def flags @"")
Expand Down Expand Up @@ -159,11 +160,11 @@

# Early exit for things like help
(when (handler :short-circuit)
(set scanning false)))
(set parsing false)))

# Iterate command line arguments and parse them
# into the run table.
(while (and scanning (< i arglen))
(while (and parsing (< i arglen))
(def arg (get args i))
(cond
# `--` turns off option processing so that
Expand Down Expand Up @@ -197,6 +198,9 @@
(handle-option :default handler)
(usage "could not handle option " arg))))

(when (and (not parsing) (not bad))
(put res :rest (slice args (dec i) -1)))

# Handle defaults, required options
(loop [[name handler] :pairs options]
(when (nil? (res name))
Expand Down
28 changes: 28 additions & 0 deletions test/suite0005.janet
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,32 @@
(assert (= ["echo" "-n" "ok"] (tuple ;cmd-args))
"unnamed arguments after `--` were not parsed correctly."))

(def argparse-params-with-shortcircuit
["A simple CLI tool. An example to show the capabilities of argparse."
"debug" {:kind :flag
:short "d"
:help "Set debug mode."}
"verbose" {:kind :multi
:short "v"
:help "Print debug information to stdout."}
"key" {:kind :option
:short "k"
:help "An API key for getting stuff from a server."
:required true}
"expr" {:kind :accumulate
:short "e"
:help "Search for all patterns given."}
"thing" {:kind :option
:help "Some option?"
:default "123"}
:default {:kind :accumulate
:short-circuit true}])

(with-dyns [:args @["testcase.janet" "-k" "100" "test" "--fake"]]
(def res (suppress-stdout (argparse/argparse ;argparse-params-with-shortcircuit)))
(assert (deep= res
@{"key" "100" "thing" "123" :default @["test"] :order @["key" :default] :rest ["test" "--fake"]})
"argparse param :default {:short-circuit true} should not fail but return with res and :rest of arguments"))


(end-suite)

0 comments on commit 213842c

Please sign in to comment.