Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 47 additions & 42 deletions cli.carp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
(None) (match @b (None) true _ false)
(Integer i) (match @b (Integer j) (= i j) _ false)
(Floating f) (match @b (Floating g) (= f g) _ false)
(Str s) (match @b (Str t) (= s t) _ false)))
(Str s) (match @b (Str t) (= s t) _ false)
(Boolean p) (match @b (Boolean q) (= p q) _ false)))

(defn format [s t]
(match @t
(Integer i) (Long.format s i)
(Floating f) (Double.format s f)
(Str s2) (format s &s2)))
(Str s2) (format s &s2)
(Boolean b) (String.format s &(Bool.str b))
(None) (String.format s "none")))

(defn str [t]
(match @t
Expand Down Expand Up @@ -59,8 +62,8 @@
(defmodule Tag
(defn to-type [t s]
(match t
(Integer) (CLI.Type.Integer (Maybe.from 0l (Long.from-string s)))
(Floating) (CLI.Type.Floating (Maybe.from 0.0 (Double.from-string s)))
(Integer) (CLI.Type.Integer (Maybe.from (Long.from-string s) 0l))
(Floating) (CLI.Type.Floating (Maybe.from (Double.from-string s) 0.0))
(Str) (CLI.Type.Str @s)
(Boolean) (CLI.Type.Boolean (/= s "false"))))

Expand Down Expand Up @@ -145,21 +148,20 @@
k (Pair.a e)
v (Pair.b (Pair.b e))]
(when-do (or (= (Pair.a k) s) (= (Pair.b k) s))
(set! res (Maybe.unsafe-from @v))
(match @v (Maybe.Just val) (set! res val) (Maybe.Nothing) ())
(break))))
res))

(defn in? [m s vs]
(defn in? [m s allowed]
(let-do [found true
vals (values m)]
(for [i 0 (length vals)]
(let [e (unsafe-nth vals i)
k (Pair.a e)
v (Pair.b (Pair.b e))]
k (Pair.a e)]
(when (or (= (Pair.a k) s) (= (Pair.b k) s))
(match @v
(match @(Pair.b (Pair.b e))
(Maybe.Just value)
(do (set! found (contains? vs &value)) (break))
(do (set! found (contains? allowed &value)) (break))
(Maybe.Nothing) (break)))))
found))

Expand Down Expand Up @@ -208,35 +210,36 @@
(hidden option-)
(private option-)
(defndynamic option- [t long short description required default-options]
(if (= (length default-options) 0)
(list 'CLI.Option.init
(list t)
(list 'copy long)
(list 'copy short)
(list 'copy description)
required
'(Maybe.Nothing)
'(Maybe.Nothing))
(if (= (length default-options) 1)
(cond
(= (length default-options) 0)
(list 'CLI.Option.init
(list t)
(list 'copy long)
(list 'copy short)
(list 'copy description)
required
(list 'Maybe.Just (list 'to-cli-type (car default-options)))
'(Maybe.Nothing)
'(Maybe.Nothing))
(= (length default-options) 1)
(list 'CLI.Option.init
(list t)
(list 'copy long)
(list 'copy short)
(list 'copy description)
required
(list 'Maybe.Just (list 'to-cli-type (car default-options)))
(list 'Maybe.Just
(list 'Array.copy-map
'(ref (fn [e] (to-cli-type @e)))
(cadr default-options)))))))
'(Maybe.Nothing))
(list 'CLI.Option.init
(list t)
(list 'copy long)
(list 'copy short)
(list 'copy description)
required
(list 'Maybe.Just (list 'to-cli-type (car default-options)))
(list 'Maybe.Just
(list 'Array.copy-map
'(ref (fn [e] (to-cli-type @e)))
(cadr default-options))))))

(doc bool "creates a boolean option.")
(defmacro bool [long short description]
Expand All @@ -246,7 +249,7 @@
(defmacro str [long short description required :rest default-options]
(CLI.option- 'CLI.Tag.Str long short description required default-options))

(doc int "creates a integer option. The actual type is a `Long`.")
(doc int "creates an integer option. The actual type is a `Long`.")
(defmacro int [long short description required :rest default-options]
(CLI.option- 'CLI.Tag.Integer
long
Expand All @@ -255,7 +258,7 @@
required
default-options))

(doc float "creates a integer option. The actual type is a `Double`.")
(doc float "creates a floating point option. The actual type is a `Double`.")
(defmacro float [long short description required :rest default-options]
(CLI.option- 'CLI.Tag.Floating
long
Expand Down Expand Up @@ -285,10 +288,10 @@ Options:" (System.get-arg 0) &(options-str p) (Parser.description p))) (for [i
(Option.short arg)
(Option.description arg)))
(when @(Option.required? arg) (IO.print " REQUIRED"))
(when (Maybe.just? (Option.default arg))
(IO.print
&(fmt " (default: %s)"
&(str &(Maybe.unsafe-from @(Option.default arg))))))
(match @(Option.default arg)
(Maybe.Just default-val)
(IO.print &(fmt " (default: %s)" &(str &default-val)))
(Maybe.Nothing) ())
(match @(Option.options arg)
(Maybe.Just o)
(IO.print &(fmt " (options: %s)" &(join ", " &(copy-map &str &o))))
Expand All @@ -303,7 +306,7 @@ the long arguments to their values. Because values can be optional, they are
returned as `Maybe`.

Otherwise it will return an `Error` containing an error message. If that error
mesage is empty, `--help` was requested. If you don’t want to provide a
message is empty, `--help` was requested. If you don’t want to provide a
`--help` feature, you can override that flag.")
(defn parse [p]
(let-do [values (Parser.values p)
Expand Down Expand Up @@ -354,16 +357,18 @@ mesage is empty, `--help` was requested. If you don’t want to provide a
(fmt "Required option missing: --%s" (Option.long o))))
(break))
(Maybe.just? (Option.options o))
(let-do [opts (Maybe.unsafe-from @(Option.options o))]
(unless-do (CmdMap.in? &values (Option.long o) &opts)
(set! res
(Result.Error
(fmt
"Option %s received an invalid option %s (Options are %s)"
(Option.long o)
&(CmdMap.get &values (Option.long o))
&(join ", " &(copy-map &str &opts)))))
(break)))
(match @(Option.options o)
(Maybe.Just opts)
(unless-do (CmdMap.in? &values (Option.long o) &opts)
(set! res
(Result.Error
(fmt
"Option %s received an invalid option %s (Options are %s)"
(Option.long o)
&(CmdMap.get &values (Option.long o))
&(join ", " &(copy-map &str &opts)))))
(break))
(Maybe.Nothing) ())
()))))
(match res
(Result.Success _) (Result.Success (CmdMap.to-map &values))
Expand Down
Loading