From f5712070281949a94a0a90360b767c0ee1ffc3b3 Mon Sep 17 00:00:00 2001 From: "Yaron M. Minsky" Date: Sun, 11 Oct 2020 11:11:16 -0400 Subject: [PATCH 1/4] demonstrate Arg_type.map --- book/command-line-parsing/README.md | 16 ++++++++++++++ .../md5_with_better_custom_arg/.rwo-example | 1 + .../correct/md5_with_better_custom_arg/dune | 4 ++++ .../md5_with_better_custom_arg/dune-project | 2 ++ .../md5_with_better_custom_arg/dune-workspace | 0 .../correct/md5_with_better_custom_arg/md5.ml | 22 +++++++++++++++++++ book/command-line-parsing/examples/dune.inc | 12 ++++++++++ 7 files changed, 57 insertions(+) create mode 100644 book/command-line-parsing/examples/correct/md5_with_better_custom_arg/.rwo-example create mode 100644 book/command-line-parsing/examples/correct/md5_with_better_custom_arg/dune create mode 100644 book/command-line-parsing/examples/correct/md5_with_better_custom_arg/dune-project create mode 100644 book/command-line-parsing/examples/correct/md5_with_better_custom_arg/dune-workspace create mode 100644 book/command-line-parsing/examples/correct/md5_with_better_custom_arg/md5.ml diff --git a/book/command-line-parsing/README.md b/book/command-line-parsing/README.md index eedf11b7d..579574750 100644 --- a/book/command-line-parsing/README.md +++ b/book/command-line-parsing/README.md @@ -417,6 +417,22 @@ For usage information, run [1] ``` +```sh dir=examples/correct/md5_with_better_custom_arg +$ dune exec -- ./md5.exe md5.ml +b5d69e668b55cc32eb683ae818cd5938 +$ 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 diff --git a/book/command-line-parsing/examples/correct/md5_with_better_custom_arg/.rwo-example b/book/command-line-parsing/examples/correct/md5_with_better_custom_arg/.rwo-example new file mode 100644 index 000000000..d156db9eb --- /dev/null +++ b/book/command-line-parsing/examples/correct/md5_with_better_custom_arg/.rwo-example @@ -0,0 +1 @@ +(packages core) diff --git a/book/command-line-parsing/examples/correct/md5_with_better_custom_arg/dune b/book/command-line-parsing/examples/correct/md5_with_better_custom_arg/dune new file mode 100644 index 000000000..19c5052f1 --- /dev/null +++ b/book/command-line-parsing/examples/correct/md5_with_better_custom_arg/dune @@ -0,0 +1,4 @@ +(executable + (name md5) + (libraries core) + (preprocess (pps ppx_jane))) diff --git a/book/command-line-parsing/examples/correct/md5_with_better_custom_arg/dune-project b/book/command-line-parsing/examples/correct/md5_with_better_custom_arg/dune-project new file mode 100644 index 000000000..7c716d575 --- /dev/null +++ b/book/command-line-parsing/examples/correct/md5_with_better_custom_arg/dune-project @@ -0,0 +1,2 @@ +(lang dune 2.6) +(name rwo-example) diff --git a/book/command-line-parsing/examples/correct/md5_with_better_custom_arg/dune-workspace b/book/command-line-parsing/examples/correct/md5_with_better_custom_arg/dune-workspace new file mode 100644 index 000000000..e69de29bb diff --git a/book/command-line-parsing/examples/correct/md5_with_better_custom_arg/md5.ml b/book/command-line-parsing/examples/correct/md5_with_better_custom_arg/md5.ml new file mode 100644 index 000000000..33869d937 --- /dev/null +++ b/book/command-line-parsing/examples/correct/md5_with_better_custom_arg/md5.ml @@ -0,0 +1,22 @@ +open Core + +let do_hash file = + Md5.digest_file_blocking file + |> Md5.to_hex + |> print_endline + +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") + +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 diff --git a/book/command-line-parsing/examples/dune.inc b/book/command-line-parsing/examples/dune.inc index 1bcf3e08d..bca72257a 100644 --- a/book/command-line-parsing/examples/dune.inc +++ b/book/command-line-parsing/examples/dune.inc @@ -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 + (run 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 From bab73b75d3ff7631264a5e084934ee6359ac8dbe Mon Sep 17 00:00:00 2001 From: "Yaron M. Minsky" Date: Sun, 11 Oct 2020 11:17:29 -0400 Subject: [PATCH 2/4] Added some explanation to this broken example --- book/command-line-parsing/README.md | 27 ++++++++++++++++++- .../correct/md5_with_better_custom_arg/md5.ml | 2 ++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/book/command-line-parsing/README.md b/book/command-line-parsing/README.md index 579574750..d24734b7a 100644 --- a/book/command-line-parsing/README.md +++ b/book/command-line-parsing/README.md @@ -417,9 +417,34 @@ 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. Wen 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 -b5d69e668b55cc32eb683ae818cd5938 +12545f2cf40cf5c68a53198963831e70 $ dune exec -- ./md5.exe /dev/null Error parsing command line: diff --git a/book/command-line-parsing/examples/correct/md5_with_better_custom_arg/md5.ml b/book/command-line-parsing/examples/correct/md5_with_better_custom_arg/md5.ml index 33869d937..3f03e64cc 100644 --- a/book/command-line-parsing/examples/correct/md5_with_better_custom_arg/md5.ml +++ b/book/command-line-parsing/examples/correct/md5_with_better_custom_arg/md5.ml @@ -5,6 +5,7 @@ let do_hash 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 @@ -12,6 +13,7 @@ let regular_file = | `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") From aba3f81fa2aeb6b9ac9c3388148ed88a050b2362 Mon Sep 17 00:00:00 2001 From: "Yaron M. Minsky" Date: Sun, 29 Aug 2021 07:15:32 -0400 Subject: [PATCH 3/4] typo --- book/command-line-parsing/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/book/command-line-parsing/README.md b/book/command-line-parsing/README.md index fbd8d7dd5..031651797 100644 --- a/book/command-line-parsing/README.md +++ b/book/command-line-parsing/README.md @@ -419,8 +419,8 @@ For usage information, run 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. Wen can preserve this by instead using `Arg_type.map` -to add the extra check for a regular file, without losing the +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 From 6321753aab342141c259854b39bb4bafaf253657 Mon Sep 17 00:00:00 2001 From: "Yaron M. Minsky" Date: Sun, 29 Aug 2021 07:17:54 -0400 Subject: [PATCH 4/4] promotion --- book/command-line-parsing/README.md | 4 +++- book/command-line-parsing/examples/dune.inc | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/book/command-line-parsing/README.md b/book/command-line-parsing/README.md index 031651797..82f2dca96 100644 --- a/book/command-line-parsing/README.md +++ b/book/command-line-parsing/README.md @@ -444,7 +444,9 @@ Why? ```sh dir=examples/correct/md5_with_better_custom_arg $ dune exec -- ./md5.exe md5.ml -12545f2cf40cf5c68a53198963831e70 + 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: diff --git a/book/command-line-parsing/examples/dune.inc b/book/command-line-parsing/examples/dune.inc index 93d87a5e3..d3a474190 100644 --- a/book/command-line-parsing/examples/dune.inc +++ b/book/command-line-parsing/examples/dune.inc @@ -138,7 +138,7 @@ (source_tree ./correct/md5_with_better_custom_arg) (package core)) (action - (run dune build @all @runtest --root ./correct/md5_with_better_custom_arg))) + (system "dune build @all @runtest --root ./correct/md5_with_better_custom_arg"))) (alias (name runtest)