1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
(* Command line handling for OCaml tools in libguestfs.
* Copyright (C) 2016 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*)
type spec =
| Unit of (unit -> unit)
(** Simple option with no argument; call the function. *)
| Set of bool ref
(** Simple option with no argument; set the reference to true. *)
| Clear of bool ref
(** Simple option with no argument; set the reference to false. *)
| String of string * (string -> unit)
(** Option requiring an argument; the first element in the tuple
is the documentation string of the argument, and the second
is the function to call. *)
| Set_string of string * string ref
(** Option requiring an argument; the first element in the tuple
is the documentation string of the argument, and the second
is the reference to be set. *)
| Int of string * (int -> unit)
(** Option requiring an integer value as argument; the first
element in the tuple is the documentation string of the
argument, and the second is the function to call. *)
| Set_int of string * int ref
(** Option requiring an integer value as argument; the first
element in the tuple is the documentation string of the
argument, and the second is the reference to be set. *)
| Symbol of string * string list * (string -> unit)
(** Option requiring an argument among a fixed set; the first
element in the tuple is the documentation string of the
argument, the second is the list of allowed strings,
and the third is the function to call. *)
| OptString of string * (string option -> unit)
(** Option with an optional argument; the first element in the
tuple is the documentation string of the argument, and the
second is the function to call. *)
module OptionName : sig
type option_name =
| S of char (** short option like -a *)
| L of string (** long option like --add *)
| M of string
(** [M] should only be used in virt-v2v. Handle options like [-os].
This works exactly like [L] except for changing the output of
[--help], and that [M] options appear in [--short-options] and
[--long-options]. *)
end
type keys = OptionName.option_name list
type doc = string
type usage_msg = string
type anon_fun = (string -> unit)
type speclist = (keys * spec * doc) list
val hidden_option_description : string
val compare_command_line_args : OptionName.option_name -> OptionName.option_name -> int
(** Compare command line arguments for equality, ignoring any leading [-]s. *)
val string_of_option_name : OptionName.option_name -> string
(** Convert an {!OptionName.option_name} to a string for [--help] output
and man pages. For instance [L"foo"] is converted to ["--foo"]. *)
type t
(** The abstract data type. *)
val create : speclist -> ?anon_fun:anon_fun -> usage_msg -> t
(** [Getopt.create speclist ?anon_fun usage_msg] creates a new parses
for command line arguments.
[speclist] is a list of triples [(keys, spec, doc)]: [keys] is a
list of options, [spec] is the associated action, and [doc] is
the help text.
If [doc] is [Getopt.hidden_option_description], then the option
is considered internal, and it is not shown in the help text.
[anon_fun] is an optional function to handle non-option arguments;
not specifying one means that only options are allowed, and
non-options will cause an error.
[usage_msg] is the string which is printed before the list of
options as help text.
*)
val parse_argv : t -> string array -> unit
(** [Getopt.parse handle args] parses the specified arguments.
[handle] is the [Getopt.t] type with the configuration of the
command line arguments.
[args] is the array with command line arguments, with the first
element representing the application name/path.
In case of errors, like non-integer value for [Int] or [Set_int],
an error message is printed, together with a pointer to use
[--help], and then the program exists with a non-zero exit
value. *)
val parse : t -> unit
(** Call {!Getopt.parse_argv} on [Sys.argv]. *)
|