Skip to content
This repository
tree: 0091b784cf
Fetching contributors…

Cannot retrieve contributors at this time

file 100 lines (88 sloc) 3.899 kb
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
(*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA 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 Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
type simple_completion =
  | Nothing (** no completion possible *)
  | File of string (** completion matching the bash pattern *)
  | Dir (** any directory *)
  | Oneof of string list (** a finite set of possibilities *)

type completion = {params : simple_completion list; stop : bool}
    (**
The type of bash completions
The list represents the completion of consecutive arguments
The boolean is true when completion should stop here (after '--' for instance)
*)

type spec =
  | Unit of (unit -> unit)
  | Bool of (bool -> unit)
  | Set of bool ref
  | Clear of bool ref
  | String of (string -> unit)
  | Set_string of string ref
  | Int of (int -> unit)
  | Set_int of int ref
  | Float of (float -> unit)
  | Set_float of float ref
  | Tuple of spec list
  | Symbol of string list * (string -> unit)
  | Rest of (string -> unit)
  | Complete of spec * completion
      (** to be used when the spec does not allow automatic completion, but you can specify one
for example, with [("--impl", Arg.String _, "")], it can't be guessed that
it should be completed with a file, so you should say
[("--impl", Arg.Complete (Arg.String _, {params=[File "*"];stop=false}), "")] instead
*)

type key = string
type doc = string
type usage_msg = string
type anon_fun = string -> unit
exception Help of string
exception Bad of string
val parse : (key * spec * doc) list -> anon_fun -> usage_msg -> unit
val parse_argv : ?current:int ref -> string array ->
  (key * spec * doc) list -> anon_fun -> usage_msg -> unit
val usage : (key * spec * doc) list -> usage_msg -> unit

val align : (key * spec * doc) list -> (key * spec * doc) list
  (** beware, if you wish to call [add_bash_completion], you should do it before calling [align] *)
val current : int ref

val sort : (key * spec * doc) list -> (key * spec * doc) list
  (** sort the options by alphabetical order on the key.
if a key appear more than once in the spec list,
only the first occurrence is keeped, while any other
occurence is simply removed (no error) *)

val spec_of_assoc : 'a ref -> (string * 'a) list -> spec
val spec_opt_of_assoc : 'a option ref -> (string * 'a) list -> spec
val spec_of_opt_assoc : 'a ref -> 'a -> (string * 'a) list -> spec

val add_bash_completion :
  ?name:string ->
  ?names:string list ->
  ?default:simple_completion ->
  (string * spec * string) list ->
  (string * spec * string) list
    (**
adds a --bash-completion option to the command line that generates a
"bash_completion" for the given command line parser
Should be the last option added
@param name The name of the executable that completion will work on (eg qmlflat)
@param names Same as [name], but several names can be given (eg [\[qmlflat;qmlflat.native\]])
@param default The completion for anonymous argument
@param arg The arguments for which completion will happen
@return The same arguments with a new option --bash-completion
*)

(**
Often, options in command line can be given grouped.
Like : [-I "foo,bar,foobar"]
This function split a given argument into words. Char separators
are : [';' ; ',' ; ' ']
*)
val split : string -> string list
Something went wrong with that request. Please try again.