diff options
author | Benjamin Pierce <bcpierce00@users.noreply.github.com> | 2018-02-09 12:53:54 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-09 12:53:54 -0500 |
commit | f8f30c3543183b0c72c046bf10e8f3376781e9ae (patch) | |
tree | c4a8f907811645675ad9fd500a5b10b3c4a37244 | |
parent | 5faea1e4c75e1a9c2c2d51e720de216ef28c0e32 (diff) | |
parent | 4eb205bfd066904b620a9c5210cf2f137ab42fee (diff) | |
download | unison-f8f30c3543183b0c72c046bf10e8f3376781e9ae.zip unison-f8f30c3543183b0c72c046bf10e8f3376781e9ae.tar.gz unison-f8f30c3543183b0c72c046bf10e8f3376781e9ae.tar.bz2 |
Merge pull request #148 from g-raud/uarg-fix-parse-cli-options
Uarg: fix the parsing of command line options' arguments
-rw-r--r-- | src/ubase/prefs.ml | 19 | ||||
-rw-r--r-- | src/ubase/uarg.ml | 13 | ||||
-rw-r--r-- | src/ubase/util.ml | 8 | ||||
-rw-r--r-- | src/ubase/util.mli | 1 |
4 files changed, 26 insertions, 15 deletions
diff --git a/src/ubase/prefs.ml b/src/ubase/prefs.ml index 44df052..7049719 100644 --- a/src/ubase/prefs.ml +++ b/src/ubase/prefs.ml @@ -392,16 +392,15 @@ and parseLines filename lines = | _ -> raise (Util.Fatal(Printf.sprintf "File \"%s\", line %d:\nGarbled 'include' directive: %s" filename lineNum theLine)) - else try - let pos = String.index theLine '=' in - let varName = Util.trimWhitespace (String.sub theLine 0 pos) in - let theResult = - Util.trimWhitespace (String.sub theLine (pos+1) - (String.length theLine - pos - 1)) in - loop rest (lineNum+1) ((filename, lineNum, varName, theResult)::res) - with Not_found -> (* theLine does not contain '=' *) - raise(Util.Fatal(Printf.sprintf - "File \"%s\", line %d:\nGarbled line (no '='):\n%s" filename lineNum theLine)) in + else + let l = Util.splitAtFirstChar theLine '=' in + match Safelist.map Util.trimWhitespace l with + [varName;theResult] -> + loop rest (lineNum+1) ((filename, lineNum, varName, theResult)::res) + | _ -> (* theLine does not contain '=' *) + raise (Util.Fatal(Printf.sprintf + "File \"%s\", line %d:\nGarbled line (no '='):\n%s" + filename lineNum theLine)) in loop lines 1 [] let processLines lines = diff --git a/src/ubase/uarg.ml b/src/ubase/uarg.ml index 5ffd924..2d9e55e 100644 --- a/src/ubase/uarg.ml +++ b/src/ubase/uarg.ml @@ -69,7 +69,7 @@ let parse speclist anonfun errmsg = while !current < l do let ss = argv.(!current) in if String.length ss >= 1 & String.get ss 0 = '-' then begin - let args = Util.splitIntoWords ss '=' in + let args = Util.splitAtFirstChar ss '=' in let s = Safelist.nth args 0 in let arg conv mesg = match args with @@ -79,10 +79,13 @@ let parse speclist anonfun errmsg = incr current; (try conv a with Failure _ -> stop (Wrong (s, a, mesg))) | [_;a] -> (try conv a with Failure _ -> stop (Wrong (s, a, mesg))) - | _ -> stop (Message (sprintf "Garbled argument %s" s)) in + | _ -> assert false in let action = try assoc3 s speclist with Not_found -> stop (Unknown s) + and catch f a = + try f a + with Invalid_argument s -> raise (Failure s) in begin try match action with @@ -92,11 +95,11 @@ let parse speclist anonfun errmsg = | Bool f -> begin match args with [_] -> f true - | _ -> f (arg bool_of_string "a boolean") + | _ -> f (arg (catch bool_of_string) "a boolean") end | String f -> f (arg (fun s-> s) "") - | Int f -> f (arg int_of_string "an integer") - | Float f -> f (arg float_of_string "a float") + | Int f -> f (arg (catch int_of_string) "an integer") + | Float f -> f (arg (catch float_of_string) "a float") | Rest f -> while !current < l-1 do f argv.(!current+1); diff --git a/src/ubase/util.ml b/src/ubase/util.ml index 6b9c760..9634a85 100644 --- a/src/ubase/util.ml +++ b/src/ubase/util.ml @@ -401,6 +401,14 @@ let trimWhitespace s = in loop 0 (l-1) +let splitAtFirstChar (s:string) (c:char) = + try + let i = String.index s c + and l= String.length s in + (* rest is possibly the empty string *) + [String.sub s 0 i; String.sub s (i+1) (l-i-1)] + with Not_found -> [s] + let splitIntoWords ?esc:(e='\\') (s:string) (c:char) = let rec inword acc eacc start pos = if pos >= String.length s || s.[pos] = c then diff --git a/src/ubase/util.mli b/src/ubase/util.mli index bd8d804..24bdb9d 100644 --- a/src/ubase/util.mli +++ b/src/ubase/util.mli @@ -55,6 +55,7 @@ val replacesubstrings : string -> (string * string) list -> string val concatmap : string -> ('a -> string) -> 'a list -> string val removeTrailingCR : string -> string val trimWhitespace : string -> string +val splitAtFirstChar : string -> char -> string list val splitIntoWords : ?esc:char -> string -> char -> string list val splitIntoWordsByString : string -> string -> string list val padto : int -> string -> string |