caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] [ANN] release of minicli-1.0.0
@ 2017-08-18  3:05 Francois BERENGER
  0 siblings, 0 replies; only message in thread
From: Francois BERENGER @ 2017-08-18  3:05 UTC (permalink / raw)
  To: caml-list

Dear OCaml users,

I am pleased to announce the first release of minicli.
minicli is a minimalist library for command line parsing.

The code is here:
https://github.com/UnixJunkie/minicli

It should be available in opam soon.

A small example being better than a long discourse, here is an example client program:

---
open Printf

let main () =
  let argc, args = CLI.init () in
  if argc = 1 then
    (printf "usage:\n\
             %s {-i|--input} <file> {-o|--output} <file> -n <int> -x <float> \
             [-v] [--hi <string>]\n" Sys.argv.(0);
     exit 1);
  let input_fn = CLI.get_string ["-i";"--input"] args in
  let output_fn = CLI.get_string ["-o"] args in
  let n = CLI.get_int ["-n"] args in
  let x = CLI.get_float ["-x"] args in
  let verbose = CLI.get_set_bool ["-v"] args in
  let maybe_say_hi = CLI.get_string_opt ["--hi"] args in
  printf "i: %s o: %s n: %d x: %f v: %s\n"
    input_fn output_fn n x (string_of_bool verbose);
  match maybe_say_hi with
  | None -> ()
  | Some name -> printf "Hi %s!\n" name

let () = main ()
---


Here is an example session of a user playing with this program:
---
# ./test
usage:
./test {-i|--input} <file> {-o|--output} <file> -n <int> -x <float> [-v] [--hi <string>]

# ./test -i
Fatal error: exception CLI.No_param_for_option("-i")

# ./test -i input.txt
Fatal error: exception CLI.Option_is_mandatory("-o")

# ./test -i input.txt -o output.txt
Fatal error: exception CLI.Option_is_mandatory("-n")

# ./test -i input.txt -o output.txt -n /dev/null
Fatal error: exception CLI.Not_an_int("/dev/null")

# ./test -i input.txt -o output.txt -n 123
Fatal error: exception CLI.Option_is_mandatory("-x")

# ./test -i input.txt -o output.txt -n 123 -x /dev/null
Fatal error: exception CLI.Not_a_float("/dev/null")

# ./test -i input.txt -o output.txt -n 123 -x 0.123
i: input.txt o: output.txt n: 123 x: 0.123000 v: false

# ./test -i input.txt -o output.txt -n 123 -x 0.123 -v
i: input.txt o: output.txt n: 123 x: 0.123000 v: true

# ./test -i input.txt -o output.txt -n 123 -x 0.123 -v -i input.bin
Fatal error: exception CLI.More_than_once("-i, --input")
---

minicli doesn't generate any kind of documentation automatically.
It is up to the programmer to generate a useful and up to date usage message or
to handle {-h|--help}.

More complete solutions to command line parsing are the Arg
module from the stdlib or the cmdliner library.

Best regards,
Francois.

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2017-08-18  3:05 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-18  3:05 [Caml-list] [ANN] release of minicli-1.0.0 Francois BERENGER

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).