From af99e796d142efa9fc9717dcff3df55a4e82e6ca Mon Sep 17 00:00:00 2001 From: Richard W.M. Jones Date: Sep 09 2014 17:08:43 +0000 Subject: arg: Allow flags such as --flag=arg as well as --flag arg. Allow flags to be followed directly by their argument, separated by an '=' sign. This is consistent with what GNU getopt_long and many other command line parsing libraries allow. Fix for the following issue: http://caml.inria.fr/mantis/view.php?id=5197 --- diff --git a/stdlib/arg.ml b/stdlib/arg.ml index c8b3d44..50d6e46 100644 --- a/stdlib/arg.ml +++ b/stdlib/arg.ml @@ -55,6 +55,12 @@ let rec assoc3 x l = | _ :: t -> assoc3 x t ;; +let split s = + let i = String.index s '=' in + let len = String.length s in + String.sub s 0 i, String.sub s (i+1) (len-(i+1)) +;; + let make_symlist prefix sep suffix l = match l with | [] -> "" @@ -130,14 +136,26 @@ let parse_argv_dynamic ?(current=current) argv speclist anonfun errmsg = while !current < l do let s = argv.(!current) in if String.length s >= 1 && String.get s 0 = '-' then begin - let action = - try assoc3 s !speclist - with Not_found -> stop (Unknown s) + let action, follow = + try assoc3 s !speclist, None + with Not_found -> + try + let keyword, arg = split s in + assoc3 keyword !speclist, Some arg + with Not_found -> stop (Unknown s) in - let no_arg () = () in + let no_arg () = + match follow with + | None -> () + | Some arg -> stop (Wrong (s, arg, "no argument")) in let get_arg () = - if !current + 1 < l then argv.(!current + 1) - else stop (Missing s) + match follow with + | None -> + if !current + 1 < l then argv.(!current + 1) + else stop (Missing s) + | Some arg -> + decr current; + arg in begin try let rec treat_action = function diff --git a/stdlib/arg.mli b/stdlib/arg.mli index 869d030..b8c6f11 100644 --- a/stdlib/arg.mli +++ b/stdlib/arg.mli @@ -25,7 +25,8 @@ [Unit], [Set] and [Clear] keywords take no argument. A [Rest] keyword takes the remaining of the command line as arguments. Every other keyword takes the following word on the command line - as argument. + as argument. For compatibility with GNU getopt_long, [keyword=arg] + is also allowed. Arguments not preceded by a keyword are called anonymous arguments. Examples ([cmd] is assumed to be the command name):