Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
e55dc7556d | |||
14d25abc35 | |||
10ba753ee3 | |||
0b185aa3b9 |
@@ -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.2")
|
(load "https://veitheller.de/git/carpentry/cli@0.0.5")
|
||||||
|
|
||||||
(defn main []
|
(defn main []
|
||||||
(let [p (=> (CLI.new @"My super cool tool!")
|
(let [p (=> (CLI.new @"My super cool tool!")
|
||||||
@@ -18,7 +18,7 @@ A simple CLI library for Carp.
|
|||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
```clojure
|
```clojure
|
||||||
(load "https://veitheller.de/git/carpentry/cli@0.0.2")
|
(load "https://veitheller.de/git/carpentry/cli@0.0.5")
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
@@ -43,8 +43,8 @@ manually.
|
|||||||
Once you’re done building your flag structure, you can run `CLI.parse`. It
|
Once you’re done building your flag structure, you can run `CLI.parse`. It
|
||||||
will not abort the program on error, instead it will tell you what went wrong
|
will not abort the program on error, instead it will tell you what went wrong
|
||||||
in a `Result.Error`. If it succeeds, the `Result.Success` contains a `Map` from
|
in a `Result.Error`. If it succeeds, the `Result.Success` contains a `Map` from
|
||||||
the long flag name to the value (the values are `Maybe`s, since they might be
|
the long flag name to the value. The values are not in the map if they are
|
||||||
optional arguments.
|
unset.
|
||||||
|
|
||||||
<hr/>
|
<hr/>
|
||||||
|
|
||||||
|
48
cli.carp
48
cli.carp
@@ -6,7 +6,7 @@
|
|||||||
(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.2\")
|
(load \"https://veitheller.de/git/carpentry/cli@0.0.5\")
|
||||||
|
|
||||||
(defn main []
|
(defn main []
|
||||||
(let [p (=> (CLI.new @\"My super cool tool!\")
|
(let [p (=> (CLI.new @\"My super cool tool!\")
|
||||||
@@ -21,7 +21,7 @@
|
|||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
```clojure
|
```clojure
|
||||||
(load \"https://veitheller.de/git/carpentry/cli@0.0.2\")
|
(load \"https://veitheller.de/git/carpentry/cli@0.0.5\")
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
@@ -46,8 +46,8 @@ manually.
|
|||||||
Once you’re done building your flag structure, you can run `CLI.parse`. It
|
Once you’re done building your flag structure, you can run `CLI.parse`. It
|
||||||
will not abort the program on error, instead it will tell you what went wrong
|
will not abort the program on error, instead it will tell you what went wrong
|
||||||
in a `Result.Error`. If it succeeds, the `Result.Success` contains a `Map` from
|
in a `Result.Error`. If it succeeds, the `Result.Success` contains a `Map` from
|
||||||
the long flag name to the value (the values are `Maybe`s, since they might be
|
the long flag name to the value. The values are not in the map if they are
|
||||||
optional arguments.")
|
unset.")
|
||||||
(defmodule CLI
|
(defmodule CLI
|
||||||
(hidden Type)
|
(hidden Type)
|
||||||
(private Type)
|
(private Type)
|
||||||
@@ -55,11 +55,16 @@ optional arguments.")
|
|||||||
(Integer [Long])
|
(Integer [Long])
|
||||||
(Floating [Double])
|
(Floating [Double])
|
||||||
(Str [String])
|
(Str [String])
|
||||||
|
(None [])
|
||||||
)
|
)
|
||||||
|
|
||||||
(defmodule Type
|
(defmodule Type
|
||||||
(defn = [a b]
|
(defn = [a b]
|
||||||
(match @a
|
(match @a
|
||||||
|
(None)
|
||||||
|
(match @b
|
||||||
|
(None) true
|
||||||
|
_ false)
|
||||||
(Integer i)
|
(Integer i)
|
||||||
(match @b
|
(match @b
|
||||||
(Integer j) (= i j)
|
(Integer j) (= i j)
|
||||||
@@ -83,7 +88,35 @@ optional arguments.")
|
|||||||
(match @t
|
(match @t
|
||||||
(Integer i) (str i)
|
(Integer i) (str i)
|
||||||
(Floating f) (str f)
|
(Floating f) (str f)
|
||||||
(Str s) (str s)))
|
(Str s) (str s)
|
||||||
|
(None) @"none"))
|
||||||
|
|
||||||
|
(defn to-int [x]
|
||||||
|
(match x
|
||||||
|
(Integer l) (Long.to-int l)
|
||||||
|
_ 0))
|
||||||
|
|
||||||
|
(defn to-long [x]
|
||||||
|
(match x
|
||||||
|
(Integer l) l
|
||||||
|
_ 0l))
|
||||||
|
|
||||||
|
(defn to-str [x]
|
||||||
|
(match x
|
||||||
|
(Str s) s
|
||||||
|
_ @""))
|
||||||
|
|
||||||
|
(defn to-float [x]
|
||||||
|
(match x
|
||||||
|
(Floating d) (Double.to-float d)
|
||||||
|
_ 0.0f))
|
||||||
|
|
||||||
|
(defn to-double [x]
|
||||||
|
(match x
|
||||||
|
(Floating d) d
|
||||||
|
_ 0.0))
|
||||||
|
|
||||||
|
(defn zero [] (None))
|
||||||
)
|
)
|
||||||
|
|
||||||
(hidden Tag)
|
(hidden Tag)
|
||||||
@@ -203,7 +236,10 @@ optional arguments.")
|
|||||||
|
|
||||||
(defn to-map [m]
|
(defn to-map [m]
|
||||||
(Array.reduce
|
(Array.reduce
|
||||||
&(fn [a v] (Map.put a (Pair.a (Pair.a v)) (Pair.b (Pair.b v))))
|
&(fn [a v]
|
||||||
|
(match @(Pair.b (Pair.b v))
|
||||||
|
(Maybe.Just e) (Map.put a (Pair.a (Pair.a v)) &e)
|
||||||
|
(Maybe.Nothing) a))
|
||||||
{}
|
{}
|
||||||
(values m)))
|
(values m)))
|
||||||
)
|
)
|
||||||
|
@@ -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 "https://veitheller.de/git/carpentry/cli@0.0.2")
|
<pre><code class="language-clojure">(load "https://veitheller.de/git/carpentry/cli@0.0.5")
|
||||||
|
|
||||||
(defn main []
|
(defn main []
|
||||||
(let [p (=> (CLI.new @"My super cool tool!")
|
(let [p (=> (CLI.new @"My super cool tool!")
|
||||||
@@ -42,7 +42,7 @@
|
|||||||
(Result.Error msg) (do (IO.errorln &msg) (CLI.usage &p)))))
|
(Result.Error msg) (do (IO.errorln &msg) (CLI.usage &p)))))
|
||||||
</code></pre>
|
</code></pre>
|
||||||
<h2>Installation</h2>
|
<h2>Installation</h2>
|
||||||
<pre><code class="language-clojure">(load "https://veitheller.de/git/carpentry/cli@0.0.2")
|
<pre><code class="language-clojure">(load "https://veitheller.de/git/carpentry/cli@0.0.5")
|
||||||
</code></pre>
|
</code></pre>
|
||||||
<h2>Usage</h2>
|
<h2>Usage</h2>
|
||||||
<p><code>CLI</code> should be built using combinators, as in the example above. It has, as of
|
<p><code>CLI</code> should be built using combinators, as in the example above. It has, as of
|
||||||
@@ -61,8 +61,8 @@ manually.</p>
|
|||||||
<p>Once you’re done building your flag structure, you can run <code>CLI.parse</code>. It
|
<p>Once you’re done building your flag structure, you can run <code>CLI.parse</code>. It
|
||||||
will not abort the program on error, instead it will tell you what went wrong
|
will not abort the program on error, instead it will tell you what went wrong
|
||||||
in a <code>Result.Error</code>. If it succeeds, the <code>Result.Success</code> contains a <code>Map</code> from
|
in a <code>Result.Error</code>. If it succeeds, the <code>Result.Success</code> contains a <code>Map</code> from
|
||||||
the long flag name to the value (the values are <code>Maybe</code>s, since they might be
|
the long flag name to the value. The values are not in the map if they are
|
||||||
optional arguments.</p>
|
unset.</p>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
<div class="binder">
|
<div class="binder">
|
||||||
@@ -197,7 +197,7 @@ optional arguments.</p>
|
|||||||
defn
|
defn
|
||||||
</div>
|
</div>
|
||||||
<p class="sig">
|
<p class="sig">
|
||||||
(λ [(Ref Parser)] (Result (Map String (Maybe Type)) String))
|
(λ [(Ref Parser)] (Result (Map String Type) String))
|
||||||
</p>
|
</p>
|
||||||
<pre class="args">
|
<pre class="args">
|
||||||
(parse p)
|
(parse p)
|
||||||
|
@@ -3,8 +3,11 @@
|
|||||||
(defn main []
|
(defn main []
|
||||||
(let [p (=> (CLI.new @"My super cool tool!")
|
(let [p (=> (CLI.new @"My super cool tool!")
|
||||||
(CLI.add &(CLI.int "flag" "f" "my flag" true))
|
(CLI.add &(CLI.int "flag" "f" "my flag" true))
|
||||||
(CLI.add &(CLI.str "thing" "t" "my thing" false @"hi" &[@"a" @"b" @"hi"])))]
|
(CLI.add &(CLI.str "thing" "t" "my thing" false @"hi" &[@"a" @"b" @"hi"]))
|
||||||
|
(CLI.add &(CLI.str "other" "o" "my thing" false)))]
|
||||||
(match (CLI.parse &p)
|
(match (CLI.parse &p)
|
||||||
(Result.Success flags)
|
(Result.Success flags)
|
||||||
(println* &(str &(Map.get &flags "flag")) " " &(str &(Map.get &flags "thing")))
|
(println*
|
||||||
|
&(str &(Map.get &flags "flag")) " " &(str &(Map.get &flags "thing"))
|
||||||
|
" " &(str &(Map.get &flags "other")))
|
||||||
(Result.Error msg) (do (IO.errorln &msg) (CLI.usage &p)))))
|
(Result.Error msg) (do (IO.errorln &msg) (CLI.usage &p)))))
|
||||||
|
Reference in New Issue
Block a user