Up

module Argparse

: sig

Parsing command-line arguments

#
exception Usage_error of int
#
type raw_option = string * string list

The option name and its arguments. e.g. ("--wrapper"; "echo")

#
class type ['b] option_reader =
#
method read : string -> string option -> string Stream.t -> completion:int option -> string list

read opt_name first_arg stream completion. Extract as many elements from the stream as this option needs. completion indicates when we're doing completion (so don't raise an error if the arguments are malformed unless this is None). When Some 0, the next item in the stream is being completed, etc.

#
method get_arg_types : int -> 'b list

Takes a stream of arguments and takes as many as are needed for the option. e.g. ["--version"; "--help"] takes ["--version"] and leaves ["--help"] while ["--version"; "1.0"] takes ["--version"; "1.0"] and leaves [].

#
class type ['a, 'b] option_parser =
#
method get_reader : 'b option_reader
#
method parse : string list -> 'a
#
type ('a, 'b) opt_spec = string list * int * string * ('a, 'b) option_parser
#
type ('a, 'b) argparse_spec = {
# options_spec
: ('a, 'b) opt_spec list;
# no_more_options
: string list -> bool;
}
#
type 'b complete =
# | CompleteNothing
(*There are no possible completions*)
# | CompleteOptionName of string
(*Complete this partial option name*)
# | CompleteOption of (string * 'b option_reader * string list * int)
# | CompleteArg of int
# | CompleteLiteral of string
(*This is the single possible completion*)
#
val read_args : ?cword:int -> ('a, 'b) argparse_spec -> string list -> raw_option list * string list * 'b complete

cword is the index in input_args that we are trying to complete, or None if we're not completing.

#
type 'a parsed_options
#
val parse_options : ('a, 'b) opt_spec list -> raw_option list -> 'a parsed_options
#
val iter_options : 'a parsed_options -> ('a -> unit) -> unit

Invoke the callback on each option. If it raises Safe_exception, add the name of the option to the error message.

Handy wrappers for option handlers

#
val no_arg_reader : 'b option_reader

The trivial reader for options which take no arguments.

#
class ['a, 'b] no_arg : 'a -> ['a, 'b] option_parser

Always returns a constant value (e.g. --help becomes ShowHelp) .

#
class ['a, 'b] one_arg : 'b -> string -> 'a -> ['a, 'b] option_parser
#
class ['a, 'b] two_arg : 'b -> 'b -> string -> string -> 'a -> ['a, 'b] option_parser
#
val format_options : Common.system -> ('b -> string) -> ('a, 'b) opt_spec list -> unit

Print out these options in a formatted list.

end