Skip to content

Commit

Permalink
Some prelimenary work for the cli
Browse files Browse the repository at this point in the history
  • Loading branch information
xvw committed Nov 19, 2024
1 parent 976658f commit 7e37f99
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 1 deletion.
41 changes: 41 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
*.annot
*.cmo
*.cma
*.cmi
*.a
*.o
*.cmx
*.cmxs
*.cmxa

# ocamlbuild working directory
_build/
www/
_www/

# ocamlbuild targets
*.byte
*.native
*.exe

# oasis generated files
setup.data
setup.log

# Merlin configuring file for Vim and Emacs
.merlin

# Dune generated files
*.install

# Local OPAM switch
_opam/

# Node js
node_modules/

# Krita and emacs temp files
*~

# Do not upload the cache
cache
25 changes: 25 additions & 0 deletions bin/arg.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
let docs = Cmdliner.Manpage.s_common_options

let target ?(default = Yocaml.Path.rel [ "_www" ]) () =
let doc = "Target directory" in
let arg = Cmdliner.Arg.info ~doc ~docs [ "target"; "output" ] in
Cmdliner.Arg.(value @@ opt Conv.path default arg)
;;

let source ?(default = Yocaml.Path.rel []) () =
let doc = "Source directory" in
let arg = Cmdliner.Arg.info ~doc ~docs [ "source"; "input" ] in
Cmdliner.Arg.(value @@ opt Conv.path default arg)
;;

let port ?(default = 8888) () =
let doc = "The development server's listening port" in
let arg = Cmdliner.Arg.info ~doc ~docs [ "port"; "P" ] in
Cmdliner.Arg.(value @@ opt Conv.port default arg)
;;

let log_level ?(default = `App) () =
let doc = "The log-level of the application running" in
let arg = Cmdliner.Arg.info ~doc ~docs [ "log-level"; "level"; "log" ] in
Cmdliner.Arg.(value @@ opt Conv.log_level default arg)
;;
8 changes: 8 additions & 0 deletions bin/arg.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
val target : ?default:Yocaml.Path.t -> unit -> Yocaml.Path.t Cmdliner.Term.t
val source : ?default:Yocaml.Path.t -> unit -> Yocaml.Path.t Cmdliner.Term.t
val port : ?default:int -> unit -> int Cmdliner.Term.t

val log_level
: ?default:([ `App | `Info | `Error | `Warning | `Debug ] as 'loglevel)
-> unit
-> 'loglevel Cmdliner.Term.t
3 changes: 3 additions & 0 deletions bin/capsule.ml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
module C = Conv
module A = Arg

let program () = Yocaml.Eff.log ~level:`Debug "Hello World"
let () = Yocaml_eio.run ~level:`Debug program
42 changes: 42 additions & 0 deletions bin/conv.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
let path =
let docv = "PATH"
and validate str = str |> Yocaml.Path.from_string |> Result.ok
and pp = Yocaml.Path.pp in
Cmdliner.Arg.conv ~docv (validate, pp)
;;

let port =
let docv = "PORT"
and validate str =
match int_of_string_opt str with
| None -> Result.error (str ^ " is not a valid port")
| Some x when x < 0 -> Result.error (str ^ " must be positive")
| Some x when x > 9999 -> Result.error (str ^ " must be lower than 9999")
| Some x -> Result.ok x
and pp ppf = Format.fprintf ppf "%04d" in
Cmdliner.Arg.conv' ~docv (validate, pp)
;;

let log_level =
let docv = "LOG_LEVEL"
and validate str =
match str |> String.trim |> String.lowercase_ascii with
| "a" | "app" -> Result.ok `App
| "i" | "info" -> Result.ok `Info
| "e" | "err" | "error" -> Result.ok `Error
| "w" | "warn" | "warning" -> Result.ok `Warning
| "d" | "debug" -> Result.ok `Debug
| _ -> Result.error "Invalid log-level"
and pp pf s =
Format.fprintf
pf
"%s"
(match s with
| `App -> "app"
| `Info -> "info"
| `Error -> "error"
| `Warning -> "warning"
| `Debug -> "debug")
in
Cmdliner.Arg.conv' ~docv (validate, pp)
;;
3 changes: 3 additions & 0 deletions bin/conv.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
val path : Yocaml.Path.t Cmdliner.Arg.conv
val port : int Cmdliner.Arg.conv
val log_level : [ `App | `Info | `Error | `Warning | `Debug ] Cmdliner.Arg.conv
2 changes: 1 addition & 1 deletion bin/dune
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(executable
(name capsule)
(public_name capsule)
(libraries yocaml yocaml_eio))
(libraries yocaml yocaml_eio static cmdliner))
1 change: 1 addition & 0 deletions capsule.opam
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ bug-reports: "https://github.com/xvw/capsule/issues"
depends: [
"ocaml" {>= "5.2.0"}
"dune" {>= "3.16" & >= "3.16.0"}
"cmdliner"
"yocaml"
"yocaml_eio"
"yocaml_cmarkit"
Expand Down
3 changes: 3 additions & 0 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
(ocaml (>= 5.2.0))
(dune (>= 3.16.0))

;; Binary dependencies
cmdliner

;; Yocaml dependencies
yocaml
yocaml_eio
Expand Down

0 comments on commit 7e37f99

Please sign in to comment.