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