Source

ocaml-pkgbuild / bashparam.ml

type bash_param = Param of string | ParamArray of string list

(** This is equivalent to expanding ${FOO[0]} in bash. *)
let string_of_param_elem param idx =
  match param with
    Param(str) -> if idx > 0 then "" else str
  | ParamArray (strlist) -> try List.nth strlist idx with Failure(_) -> ""

(** This is equivalent to expanding ${FOO[@]} in bash. *)
let string_of_array_param param =
  match param with
    Param(str) -> str | ParamArray (strlist) -> String.concat " " strlist

(** This is equivalent to expanding $FOO in bash. *)
let string_of_param param =
  match param with
    Param(str) -> str | ParamArray (_) -> string_of_param_elem param 0

let make_lookup expander name plist =
  let param = try List.assoc name plist
  with Not_found -> Param("")
  in expander param

(** Lookup the value of a parameter in string context.
    This is like $FOO in bash. *)
let assoc_str = make_lookup string_of_param

(** Lookup the value of a parameter in array context.
    This is like ${FOO[@]} in bash. *)
let assoc_arr = make_lookup string_of_array_param