caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Francois BERENGER <berenger@bioreg.kyushu-u.ac.jp>
To: caml-list@inria.fr
Subject: [Caml-list] [ANN] release of minicli-1.0.0
Date: Fri, 18 Aug 2017 12:05:17 +0900	[thread overview]
Message-ID: <0d430eea-d929-3d7a-a1b0-138daabd1a12@bioreg.kyushu-u.ac.jp> (raw)

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.

                 reply	other threads:[~2017-08-18  3:05 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0d430eea-d929-3d7a-a1b0-138daabd1a12@bioreg.kyushu-u.ac.jp \
    --to=berenger@bioreg.kyushu-u.ac.jp \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).