Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cli demonstrate arg type.map #3387

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
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
43 changes: 43 additions & 0 deletions book/command-line-parsing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,49 @@ For usage information, run
[1]
```

One downside of the above approach is that we've lost some of the
benefits of `Filename.arg_type`, in particular, its support for
autocomplete. We can preserve this by instead using `Arg_type.map` to
add the extra check for a regular file, without losing the
autocompletion support.

```ocaml file=examples/correct/md5_with_better_custom_arg/md5.ml,part=1
let regular_file =
Command.Arg_type.map Filename.arg_type ~f:(fun filename ->
match Sys.is_file filename with
| `Yes -> filename
| `No -> failwith "Not a regular file"
| `Unknown -> failwith "Could not determine if this was a regular file")
```

(BUT! The following shows a result that's different in terms of the
error message. We no longer get):

```
failed to parse FILENAME value "/dev/null"
```

Why?


```sh dir=examples/correct/md5_with_better_custom_arg
$ dune exec -- ./md5.exe md5.ml
ppx md5.pp.ml (exit 127)
(cd _build/default && .ppx/57ef275c515ec1fe105f6ff0979f5a61/ppx.exe -o md5.pp.ml --impl md5.ml -corrected-suffix .ppx-corrected -diff-cmd - -dump-ast)
[1]
$ dune exec -- ./md5.exe /dev/null
Error parsing command line:

(Failure "Not a regular file")

For usage information, run

md5.exe -help

[1]
```


### Optional and Default Arguments

A more realistic `md5` binary could also read from the standard input if a
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(packages core)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(executable
(name md5)
(libraries core)
(preprocess (pps ppx_jane)))
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(lang dune 2.6)
(name rwo-example)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
open Core

let do_hash file =
Md5.digest_file_blocking file
|> Md5.to_hex
|> print_endline

[@@@part "1"];;
let regular_file =
Command.Arg_type.map Filename.arg_type ~f:(fun filename ->
match Sys.is_file filename with
| `Yes -> filename
| `No -> failwith "Not a regular file"
| `Unknown -> failwith "Could not determine if this was a regular file")

[@@@part "2"];;
let command =
Command.basic ~summary:"Generate an MD5 hash of the input data"
~readme:(fun () -> "More detailed information")
Command.Let_syntax.(
let%map_open filename = anon ("filename" %: regular_file) in
fun () -> do_hash filename)

let () = Command.run ~version:"1.0" ~build_info:"RWO" command
12 changes: 12 additions & 0 deletions book/command-line-parsing/examples/dune.inc
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@
(name runtest)
(deps (alias md5_succinct)))

(rule
(alias md5_with_better_custom_arg)
(deps
(source_tree ./correct/md5_with_better_custom_arg)
(package core))
(action
(system "dune build @all @runtest --root ./correct/md5_with_better_custom_arg")))

(alias
(name runtest)
(deps (alias md5_with_better_custom_arg)))

(rule
(alias md5_with_custom_arg)
(deps
Expand Down