10
1
mirror of https://gitlab.com/scemama/QCaml.git synced 2024-06-02 03:15:19 +02:00
QCaml/common/command_line.org
2020-12-27 16:36:25 +01:00

10 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

  • 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 or Optional
  • 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>])

#+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 ;
}

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.

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

Function to create an anonymous argument:

val anonymous : long_opt -> optional -> documentation -> description

Query functions

anon_args

Returns the list of anonymous arguments

val anon_args : unit -> string list

help

Prints the documentation of the program.

get

Returns the argument associated with a long option.

val get : long_opt -> string option

get_bool

True if the Optional argument is present in the command-line

val get_bool : long_opt -> bool

Specification

Gives the specifications of the current program as a list of descrption variables.

val set_specs : description list -> unit