type t = { noclobber : bool; pipefail : bool; errexit : bool; no_path_expansion : bool; no_unset : bool; all_export : bool; async : bool; } let jsont = let make noclobber pipefail errexit no_path_expansion no_unset all_export async = { noclobber; pipefail; errexit; no_path_expansion; no_unset; all_export; async; } in Jsont.Object.map ~kind:"T" make |> Jsont.Object.mem "noclobber" Jsont.bool ~enc:(fun t -> t.noclobber) |> Jsont.Object.mem "pipefail" Jsont.bool ~enc:(fun t -> t.pipefail) |> Jsont.Object.mem "errexit" Jsont.bool ~enc:(fun t -> t.errexit) |> Jsont.Object.mem "no_path_expansion" Jsont.bool ~enc:(fun t -> t.no_path_expansion) |> Jsont.Object.mem "no_unset" Jsont.bool ~enc:(fun t -> t.no_unset) |> Jsont.Object.mem "all_export" Jsont.bool ~enc:(fun t -> t.all_export) |> Jsont.Object.mem "async" Jsont.bool ~enc:(fun t -> t.async) |> Jsont.Object.finish let default = { noclobber = false; pipefail = false; errexit = false; no_path_expansion = false; no_unset = false; all_export = false; async = false; } let with_options ?noclobber ?pipefail ?errexit ?async ?no_path_expansion ?no_unset ?all_export t = { noclobber = Option.value ~default:t.noclobber noclobber; pipefail = Option.value ~default:t.pipefail pipefail; errexit = Option.value ~default:t.errexit errexit; async = Option.value ~default:t.async async; no_path_expansion = Option.value ~default:t.no_path_expansion no_path_expansion; all_export = Option.value ~default:t.all_export all_export; no_unset = Option.value ~default:t.no_unset no_unset; } type posix = [ `Noclobber | `Pipefail | `Noglob | `Nounset | `Errexit | `Allexport ] type merry = [ `Async ] type option = [ posix | merry ] let posix_to_letter = function | `Noclobber -> "C" | `Noglob -> "f" | `Nounset -> "u" | `Errexit -> "e" | `Allexport -> "-a" | _ -> "" let get (o : option) t = match o with | `Pipefail -> t.pipefail | `Noclobber -> t.noclobber | `Noglob -> t.no_path_expansion | `Nounset -> t.no_unset | `Errexit -> t.errexit | `Allexport -> t.all_export | `Async -> t.async let set (o : option) b t = match o with | `Pipefail -> { t with pipefail = b } | `Noclobber -> { t with noclobber = b } | `Noglob -> { t with no_path_expansion = b } | `Nounset -> { t with no_unset = b } | `Errexit -> { t with errexit = b } | `Allexport -> { t with all_export = b } | `Async -> { t with async = b } let to_letters t = let nc = if t.noclobber then posix_to_letter `Noclobber else "" in nc ^ "" let update t options = List.fold_left (fun d -> function | `Pipefail, pipefail -> with_options ~pipefail d | `Noclobber, noclobber -> with_options ~noclobber d | `Noglob, no_path_expansion -> with_options ~no_path_expansion d | `Errexit, errexit -> with_options ~errexit d | `Nounset, no_unset -> with_options ~no_unset d | `Allexport, all_export -> with_options ~all_export d | `Async, async -> with_options ~async d) t options let pp ppf opt = let pp_option ppf (name, value) = Fmt.pf ppf "%-12s %s\n" name (if value then "on" else "off") in let opts = let { noclobber; pipefail; async; no_path_expansion; no_unset; errexit; all_export; } = opt in [ ("pipefail", pipefail); ("errexit", errexit); ("noclobber", noclobber); ("noglob", no_path_expansion); ("nounset", no_unset); ("async", async); ("allexport", all_export); ] in Fmt.pf ppf "@[%a@]" Fmt.(list ~sep:nop pp_option) opts