package cmdlang

  1. Overview
  2. Docs

Refer to the Parameters terminology.

type 'a parse := string -> ('a, [ `Msg of string ]) Stdlib.result

Parsing parameters of type 'a from the command line.

type 'a print := Stdlib.Format.formatter -> 'a -> unit

Printing parameters of type 'a back to their expected command line syntax. Printing parameter is used for example when documenting default values in the help messages.

type 'a t

A type to hold the capability of parsing and printing a parameter of type 'a.

val create : docv:string -> parse:'a parse -> print:'a print -> 'a t

Basic types

The API supports basic types with default docv values. These defaults can be overridden for each argument. The default docv is used only if none is specified at the argument level. The actual syntax used by default depend on the targeted backend.

val string : string t
val int : int t
val float : float t
val bool : bool t
val file : string t

Helpers

Helpers for creating parameters for custom types. These helpers also come with default docv values, which can be overridden as needed.

val enumerated : ?docv:string -> (module Enumerated_stringable with type t = 'a) -> 'a t

Create a parameter for an enumerated type. The module must implement the Enumerated_stringable interface.

Example:

module Color = struct
  type t =
    | Red
    | Green
    | Blue

  let all = [ Red; Green; Blue ]

  let to_string = function
    | Red -> "red"
    | Green -> "green"
    | Blue -> "blue"
  ;;
end

let color_param = Param.enumerated ~docv:"COLOR" (module Color)

The usage message will show the supported values for COLOR.

val stringable : ?docv:string -> (module Stringable with type t = 'a) -> 'a t
val validated_string : ?docv:string -> (module Validated_string with type t = 'a) -> 'a t

To be used with custom types when the parsing may fail.

val comma_separated : 'a t -> 'a list t

Parser for a list of values separated by commas.

OCaml

Innovation. Community. Security.