2 Commits

Author SHA1 Message Date
ba0fa7dc5b invalid c 2021-01-19 14:05:40 +01:00
4116771c98 fix typo 2020-02-12 16:33:19 +01:00
3 changed files with 37 additions and 29 deletions

View File

@@ -3,7 +3,7 @@
A simple CLI library for Carp. A simple CLI library for Carp.
```clojure ```clojure
(load "https://veitheller.de/git/carpentry/cli@0.0.6") (load "https://veitheller.de/git/carpentry/cli@0.0.7")
(defn main [] (defn main []
(let [p (=> (CLI.new @"My super cool tool!") (let [p (=> (CLI.new @"My super cool tool!")

View File

@@ -34,12 +34,14 @@
(match @b (match @b
(Str t) (= s t) (Str t) (= s t)
_ false))) _ false)))
(implements = CLI.Type.=)
(defn format [s t] (defn format [s t]
(match @t (match @t
(Integer i) (Long.format s i) (Integer i) (Long.format s i)
(Floating f) (Double.format s f) (Floating f) (Double.format s f)
(Str s2) (format s &s2))) (Str s2) (format s &s2)))
(implements format CLI.Type.format)
(defn str [t] (defn str [t]
(match @t (match @t
@@ -48,6 +50,7 @@
(Str s) (str s) (Str s) (str s)
(Boolean b) (str b) (Boolean b) (str b)
(None) @"none")) (None) @"none"))
(implements str CLI.Type.str)
(defn to-int [x] (defn to-int [x]
(match x (match x
@@ -80,6 +83,7 @@
_ 0.0)) _ 0.0))
(defn zero [] (None)) (defn zero [] (None))
(implements zero CLI.Type.zero)
) )
(hidden Tag) (hidden Tag)
@@ -94,8 +98,8 @@
(defmodule Tag (defmodule Tag
(defn to-type [t s] (defn to-type [t s]
(match t (match t
(Integer) (CLI.Type.Integer (Long.from-string s)) (Integer) (CLI.Type.Integer (Maybe.unsafe-from (from-string s)))
(Floating) (CLI.Type.Floating (Double.from-string s)) (Floating) (CLI.Type.Floating (Maybe.unsafe-from (from-string s)))
(Str) (CLI.Type.Str @s) (Str) (CLI.Type.Str @s)
(Boolean) (CLI.Type.Boolean (/= s "false")))) (Boolean) (CLI.Type.Boolean (/= s "false"))))
@@ -117,25 +121,26 @@
(match @b (match @b
(Boolean) true (Boolean) true
_ false))) _ false)))
(implements = CLI.Tag.=)
) )
(doc Option "is the option type. To construct an `Option`, please use (doc Option "is the option type. To construct an `Option`, please use
[`int`](#int), [`float`](#float), or [`str`](#str).") [`int`](#int), [`float`](#float), or [`str`](#str).")
(deftype Option [ (deftype Option [
type- Tag type- CLI.Tag
long String long String
short String short String
description String description String
required? Bool required? Bool
default (Maybe Type) default (Maybe CLI.Type)
options (Maybe (Array Type)) options (Maybe (Array CLI.Type))
]) ])
(doc Parser "is the parser type. To construct a `Parser`, please use (doc Parser "is the parser type. To construct a `Parser`, please use
[`new`](#new).") [`new`](#new).")
(deftype Parser [ (deftype Parser [
description String description String
options (Array Option) options (Array CLI.Option)
]) ])
; this is pretty brutal. Its a (Pair (Pair <long> <short>) (<tag> <value>)) ; this is pretty brutal. Its a (Pair (Pair <long> <short>) (<tag> <value>))
@@ -145,11 +150,10 @@
(hidden CmdMap) (hidden CmdMap)
(deftype CmdMap [ (deftype CmdMap [
values (Array (Pair (Pair String String) (Pair Tag (Maybe Type)))) values (Array (Pair (Pair String String) (Pair CLI.Tag (Maybe CLI.Type))))
]) ])
(defmodule CmdMap (defmodule CmdMap
(use Array)
(defn new [] (init [])) (defn new [] (init []))
(defn put [m o v] (defn put [m o v]
@@ -265,21 +269,20 @@
(Parser.update-options p &(fn [options] (push-back options @opt)))) (Parser.update-options p &(fn [options] (push-back options @opt))))
(hidden option-) (hidden option-)
(private option-)
(defndynamic option- [t long short description required default-options] (defndynamic option- [t long short description required default-options]
(if (= (length default-options) 0) (if (= (length default-options) 0)
(list 'CLI.Option.init (list t) `(CLI.Option.init (%t)
(list 'copy long) (list 'copy short) (list 'copy description) (copy %long) (copy %short) (copy %description)
required '(Maybe.Nothing) '(Maybe.Nothing)) %required (Maybe.Nothing) (Maybe.Nothing))
(if (= (length default-options) 1) (if (= (length default-options) 1)
(list 'CLI.Option.init (list t) `(CLI.Option.init (%t)
(list 'copy long) (list 'copy short) (list 'copy description) (copy %long) (copy %short) (copy %description)
required (list 'Maybe.Just (list 'to-cli-type (car default-options))) '(Maybe.Nothing)) %required (Maybe.Just (to-cli-type %(car default-options))) (Maybe.Nothing))
(list 'CLI.Option.init (list t) `(CLI.Option.init (%t)
(list 'copy long) (list 'copy short) (list 'copy description) (copy %long) (copy %short) (copy %description)
required (list 'Maybe.Just (list 'to-cli-type (car default-options))) %required (Maybe.Just (to-cli-type %(car default-options)))
(list 'Maybe.Just (Maybe.Just
(list 'Array.copy-map '(ref (fn [e] (to-cli-type @e))) (cadr default-options))))))) (Array.copy-map &(fn [e] (to-cli-type @e)) %(cadr default-options)))))))
(doc bool "creates a boolean option.") (doc bool "creates a boolean option.")
(defmacro bool [long short description] (defmacro bool [long short description]
@@ -311,7 +314,8 @@
(do (do
(IO.println (IO.println
&(fmt "usage: %s %s\n%s\nOptions:" &(fmt "usage: %s %s\n%s\nOptions:"
(System.get-arg 0) &(options-str p) (Parser.description p))) (StaticArray.unsafe-nth &System.args 0)
&(options-str p) (Parser.description p)))
(for [i 0 (length (Parser.options p))] (for [i 0 (length (Parser.options p))]
(let [arg (unsafe-nth (Parser.options p) i)] (let [arg (unsafe-nth (Parser.options p) i)]
(do (do
@@ -340,16 +344,17 @@ mesage is empty, `--help` was requested. If you dont want to provide a
(defn parse [p] (defn parse [p]
(let-do [values (Parser.values p) (let-do [values (Parser.values p)
res (Result.Success @p) res (Result.Success @p)
options (Parser.options p)] options (Parser.options p)
(for [i 1 (System.get-args-len)] len (StaticArray.length &System.args)]
(let [x (System.get-arg i)] (for [i 1 len]
(let [x (StaticArray.unsafe-nth &System.args i)]
(if (or (starts-with? x "--") (starts-with? x "-")) (if (or (starts-with? x "--") (starts-with? x "-"))
(let [flag (Pattern.substitute #"^\-\-?" x "" 1) (let [flag (Pattern.substitute #"^\-\-?" x "" 1)
splt (split-by &flag &[\=]) splt (split-by &flag &[\=])
k (if (> (length &splt) 1) (unsafe-nth &splt 0) &flag) k (if (> (length &splt) 1) (unsafe-nth &splt 0) &flag)
v (cond (> (length &splt) 1) (nth &splt 1) v (cond (> (length &splt) 1) (nth &splt 1)
(< i (Int.dec (System.get-args-len))) (< i (Int.dec len))
(do (set! i (Int.inc i)) (Maybe.Just @(System.get-arg i))) (do (set! i (Int.inc i)) (Maybe.Just @(StaticArray.unsafe-nth &System.args i)))
(Maybe.Nothing))] (Maybe.Nothing))]
(cond (cond
(CmdMap.contains? &values k) (CmdMap.contains? &values k)
@@ -405,20 +410,23 @@ mesage is empty, `--help` was requested. If you dont want to provide a
(defmodule String (defmodule String
(defn to-cli-type [s] (CLI.Type.Str s)) (defn to-cli-type [s] (CLI.Type.Str s))
(implements to-cli-type String.to-cli-type)
) )
(defmodule Double (defmodule Double
(defn to-cli-type [f] (CLI.Type.Floating f)) (defn to-cli-type [f] (CLI.Type.Floating f))
(implements to-cli-type Double.to-cli-type)
) )
(defmodule Long (defmodule Long
(defn to-cli-type [l] (CLI.Type.Integer l)) (defn to-cli-type [l] (CLI.Type.Integer l))
(implements to-cli-type Long.to-cli-type)
) )
(doc CLI "is a simple CLI library for Carp. (doc CLI "is a simple CLI library for Carp.
```clojure ```clojure
(load \"https://veitheller.de/git/carpentry/cli@0.0.6\") (load \"https://veitheller.de/git/carpentry/cli@0.0.7\")
(defn main [] (defn main []
(let [p (=> (CLI.new @\"My super cool tool!\") (let [p (=> (CLI.new @\"My super cool tool!\")

View File

@@ -30,7 +30,7 @@
</h1> </h1>
<div class="module-description"> <div class="module-description">
<p>is a simple CLI library for Carp.</p> <p>is a simple CLI library for Carp.</p>
<pre><code class="language-clojure">(load &quot;https://veitheller.de/git/carpentry/cli@0.0.6&quot;) <pre><code class="language-clojure">(load &quot;https://veitheller.de/git/carpentry/cli@0.0.7&quot;)
(defn main [] (defn main []
(let [p (=&gt; (CLI.new @&quot;My super cool tool!&quot;) (let [p (=&gt; (CLI.new @&quot;My super cool tool!&quot;)