juster avatar juster committed a4622fb

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

Comments (0)

Files changed (5)

-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)
+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
+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

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

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
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.