Source

ocaml-pkgbuild / bashparams.ml

juster 09947d5 


























module Bashparams =
  struct
    type t = PString of string | PArray of string list

    (** Create a parameter defined simply. ex: FOO=bar *)
    let param_of_string rawstr = PString(rawstr)

    (** Create a parameter defined as an array. ex: FOO=(bar baz) *)
    let param_of_list rawstrlist = PArray(rawstrlist)

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

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

    (** This is equivalent to expanding $FOO in bash. *)
    let string_of_param param =
      match param with
        PString(str) -> str
      | PArray (_)   -> string_of_array_elem param 0
  end