package cmdlang

  1. Overview
  2. Docs

Module Command.ParamSource

Refer to the Parameters terminology.

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

Parsing parameters of type 'a from the command line.

type 'a print := 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.

Sourcetype 'a t

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

Sourceval 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.

Sourceval string : string t
Sourceval int : int t
Sourceval float : float t
Sourceval bool : bool t
Sourceval 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.

Sourceval 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.

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

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

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

Parser for a list of values separated by commas.

OCaml

Innovation. Community. Security.