Module Os.Exit

Program exit.

There are two ways for a program to exit: either by returning an exit code or by calling B0_std.Os.Cmd.execv. The type here allows to represent these two ways of exiting.

Program exits

type code = int

The type for exit codes.

type execv

The type for execv calls.

type t =
  1. | Code : code -> t
    (*

    exit with code.

    *)
  2. | Execv : execv -> t
    (*

    exit with execv

    *)

The type for specifying program exits.

val get_code : t -> code

get_code e is the exit code of e. Raises Invalid_argument if e is Exec.

val exit : ?on_error:t -> t -> 'a

exit ~on_error e exits according to e:

  • If e is Code c, Stdlib.exit c is called and the function never returns.
  • If e is Execv execv, execv is called. This can only return with an Error _. In that case the error is logged and exit is called again with on_error (and the default on_error)

on_error defaults to some_error. Except if an asynchronous exception is raised this function never returns.

Exiting with codes

Note. The constants here match those established by Cmdliner with another one useful in cli tools. But we don't want a Cmdliner dependency on it here.

val code : code -> t

code c is Code c.

val ok : t

ok is Code 0.

val no_such_name : t

no_such_name is Code 122, it indicates a named entity was not found.

val some_error : t

some_error is Code 123, it indicates an indiscriminate error reported on stdout.

val cli_error : t

cli_error is Code 124, it indicates a command line parsing error.

val internal_error : t

internal_error is Code 125, it indicates an unexpected internal error (bug).

Exit with results

val of_result : (unit, string) Stdlib.result -> t

of_result v exits with ok if v is Ok () and logs the Error and exits with some_error if v is Error _.

val of_result' : (t, string) Stdlib.result -> t

of_result v exits with e if v is Ok e and logs the error and exits with some_error if v is Error _.

Exit by execv

val execv : ?env:Env.assignments -> ?cwd:Fpath.t -> ?argv0:string -> Cmd.t -> t

exec ?env ?cwd ?argv0 cmd is an Exec _. That has a call to Os.Cmd.execv with the corresponding arguments.

val execv_env : execv -> Env.assignments option

execv_env exec is the environment of exec.

val execv_cwd : execv -> Fpath.t option

execv_env exec is the environment of exec.

val execv_argv0 : execv -> string option

execv_env exec is the environment of exec.

val execv_cmd : execv -> Cmd.t

execv_env exec is the command of exec.

Signal exit hooks

val on_sigint : hook:(unit -> unit) -> (unit -> 'a) -> 'a

on_sigint ~hook f calls f () and returns its value. If SIGINT is signalled during that time hook is called followed by exit 130 – that is the exit code a SIGINT would produce.

on_sigint replaces an existing signal handler for Sys.sigint during time of the function call. It is restored when the function returns.

Note. Since Stdlib.exit is called Stdlib.at_exit functions are called if a SIGINT occurs during the call to f. This is not the case on an unhandled SIGINT.