diff --git a/book/command-line-parsing/README.md b/book/command-line-parsing/README.md index 7e8a409e2..82f2dca96 100644 --- a/book/command-line-parsing/README.md +++ b/book/command-line-parsing/README.md @@ -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 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..3f03e64cc --- /dev/null +++ b/book/command-line-parsing/examples/correct/md5_with_better_custom_arg/md5.ml @@ -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 diff --git a/book/command-line-parsing/examples/dune.inc b/book/command-line-parsing/examples/dune.inc index 2f8fdba25..d3a474190 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 + (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