QCaml/common/lib/command_line.mli

108 lines
2.8 KiB
OCaml

(** This module is a wrapper around the ~Getopt~ library and helps to
define command-line arguments.
Here is an example of how to use this module.
First, define the specification:
#+begin_src ocaml :tangle no
let open Command_line in
begin
set_header_doc (Sys.argv.(0) ^ " - One-line description");
set_description_doc "Long description of the command.";
set_specs
[ { short='c'; long="check"; opt=Optional;
doc="Checks the input data";
arg=Without_arg; };
{ short='b' ; long="basis" ; opt=Mandatory;
arg=With_arg "<string>";
doc="Name of the file containing the basis set"; } ;
{ short='m' ; long="multiplicity" ; opt=Optional;
arg=With_arg "<int>";
doc="Spin multiplicity (2S+1). Default is singlet"; } ;
]
end;
#+end_src
Then, define what to do with the arguments:
#+begin_src ocaml :tangle no
let c =
Command_line.get_bool "check"
in
let basis =
match Command_line.get "basis" with
| Some x -> x
| None -> assert false
in
let multiplicity =
match Command_line.get "multiplicity" with
| None -> 1
| Some n -> int_of_string n
in
#+end_src
*)
(** Types *)
type short_opt = char
(** In the command line, a dash with a single character (ex: =ls -l=) *)
type long_opt = string
(** In the command line, two dashes with a word (ex: =ls --directory=) *)
type optional = Mandatory | Optional
(** Command-line options can be ~Mandatory~ or ~Optional~ *)
type documentation = string
(** Documentation of the option is used in the help function *)
type argument = | With_arg of string
| Without_arg
| With_opt_arg of string
(** Some options require an argument (~ls --ignore="*.ml"~ ), some
don't (~ls -l~) and for some arguments the argument is optional
(~git --log[=<n>]~) *)
type description = {
short: short_opt ;
long : long_opt ;
opt : optional ;
doc : documentation ;
arg : argument ;
}
(** Functions to set the header, footer and main description of the
documentation provided by the ~help~ function: *)
val set_header_doc : string -> unit
val set_description_doc : string -> unit
val set_footer_doc : string -> unit
val anonymous : long_opt -> optional -> documentation -> description
(** Function to create an anonymous argument. *)
(** Query functions *)
val get : long_opt -> string option
(** Returns the argument associated with a long option *)
val get_bool : long_opt -> bool
(** True if the ~Optional~ argument is present in the command-line *)
val anon_args : unit -> string list
(** Returns the list of anonymous arguments *)
(** Specification *)
val set_specs : description list -> unit
(** Sets the specifications of the current program from a list of
~descrption~ variables. *)