Commits

juster  committed a4622fb

Reorganize and rename the Bashparams module. Simplify the interface for it.

  • Participants
  • Parent commits 632c202

Comments (0)

Files changed (5)

File bashexpand.ml

-open Bashparams
+open Bashparam
 open Printf
 
 module Bashexpand =
 
     let param_lookup expander name plist =
       let param = try List.assoc name plist
-        with Not_found -> Bashparams.param_of_string ""
+        with Not_found -> Param("")
       in expander param
 
     (** Lookup the value of a parameter in string context.
         This is like $FOO in bash. *)
-    let param_str_ctx = param_lookup Bashparams.string_of_param
+    let param_str_ctx = param_lookup Bashparam.string_of_param
 
     (** Lookup the value of a parameter in array context.
         This is like ${FOO[@]} in bash. *)
           match str.[idx] with
           | '$' ->
               begin
-                try
-                  match expand_name (idx+1) with (newidx, name) ->
-                    (newidx, param_str_ctx name params)
+                try match expand_name (idx+1) with (newidx, name) ->
+                  (newidx, param_str_ctx name params)
                 with Invalid_argument(_) -> raise (ExpandError idx)
               end
           | '{' ->
   end
 
 let _ =
-  let paramslist = [ ("FOO", Bashparams.param_of_string "BAR") ;
-                     ("BAR", Bashparams.param_of_string "Hello, World!") ] in
+  let paramslist = [ ("FOO", Param("BAR")) ;
+                     ("BAR", Param("Hello, World!")) ] in
   print_endline (Bashexpand.parameter_expand
                    "Substitutions${BAZ:- IS WORKING}!" paramslist)

File 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

File bashparam.mli

+type bash_param = Param of string | ParamArray of string list
+
+val string_of_param_elem  : bash_param -> int -> string
+val string_of_array_param : bash_param -> string
+val string_of_param       : bash_param -> string

File bashparams.ml

-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

File bashparams.mli

-module Bashparams :
-    sig
-      type t
-
-      val param_of_string      : string -> t
-      val param_of_list        : string list -> t
-
-      val string_of_array_elem : t -> int -> string
-      val string_of_array      : t -> string
-      val string_of_param      : t -> string
-    end