mirror of
https://gitlab.com/scemama/QCaml.git
synced 2024-11-06 22:23:42 +01:00
9.8 KiB
9.8 KiB
Command line
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:
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;
Then, define what to do with the arguments:
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
Type
#+NAME:type
type short_opt = char
type long_opt = string
type optional = Mandatory | Optional
type documentation = string
type argument = With_arg of string | Without_arg | With_opt_arg of string
type description = {
short: short_opt ;
long : long_opt ;
opt : optional ;
doc : documentation ;
arg : argument ;
}
- Short option: in the command line, a dash with a single character
(ex:
ls -l
) - Long option: in the command line, two dashes with a word
(ex:
ls --directory
) - Command-line options can be
Mandatory
orOptional
- Documentation of the option is used in the help function
- Some options require an argument (
ls --ignore="*.ml"
), some don't (ls -l
) and for some arguments the argument is optional (git --log[=<n>]
)
Mutable attributes
All the options are stored in the hash table dict
where the key
is the long option and the value is a value of type description
.
val set_header_doc : string -> unit
val set_description_doc : string -> unit
val set_footer_doc : string -> unit
Functions to set the header, footer and main description of the
documentation provided by the help
function:
val anonymous : long_opt -> optional -> documentation -> description
Function to create an anonymous argument.
Query functions
val get : long_opt -> string option
val get_bool : long_opt -> bool
val anon_args : unit -> string list
get |
Returns the argument associated with a long option |
get_bool |
True if the Optional argument is present in the command-line |
anon_args |
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.