fa72b7f
From 350eab7918f0adf385e01411ae6fed3e0579145f Mon Sep 17 00:00:00 2001
0db310c
From: "Richard W.M. Jones" <rjones@redhat.com>
0db310c
Date: Tue, 1 Apr 2014 11:21:40 +0100
5b56684
Subject: [PATCH 12/18] arg: Allow flags such as --flag=arg as well as --flag
0db310c
 arg.
0db310c
0db310c
Allow flags to be followed directly by their argument, separated by an '='
0db310c
sign.  This is consistent with what GNU getopt_long and many other
0db310c
command line parsing libraries allow.
0db310c
0db310c
Fix for the following issue:
0db310c
http://caml.inria.fr/mantis/view.php?id=5197
0db310c
---
e4cc04a
 stdlib/arg.ml  | 30 ++++++++++++++++++++++++------
0db310c
 stdlib/arg.mli |  3 ++-
e4cc04a
 2 files changed, 26 insertions(+), 7 deletions(-)
0db310c
0db310c
diff --git a/stdlib/arg.ml b/stdlib/arg.ml
fa72b7f
index a8f3964..f2b6f13 100644
0db310c
--- a/stdlib/arg.ml
0db310c
+++ b/stdlib/arg.ml
0db310c
@@ -55,6 +55,12 @@ let rec assoc3 x l =
0db310c
   | _ :: t -> assoc3 x t
0db310c
 ;;
0db310c
 
0db310c
+let split s =
0db310c
+  let i = String.index s '=' in
0db310c
+  let len = String.length s in
0db310c
+  String.sub s 0 i, String.sub s (i+1) (len-(i+1))
0db310c
+;;
0db310c
+
0db310c
 let make_symlist prefix sep suffix l =
0db310c
   match l with
0db310c
   | [] -> "<none>"
e4cc04a
@@ -130,14 +136,26 @@ let parse_argv_dynamic ?(current=current) argv speclist anonfun errmsg =
0db310c
   while !current < l do
0db310c
     let s = argv.(!current) in
7335bbf
     if String.length s >= 1 && s.[0] = '-' then begin
0db310c
-      let action =
0db310c
-        try assoc3 s !speclist
0db310c
-        with Not_found -> stop (Unknown s)
0db310c
+      let action, follow =
0db310c
+        try assoc3 s !speclist, None
0db310c
+        with Not_found ->
0db310c
+          try
0db310c
+            let keyword, arg = split s in
0db310c
+            assoc3 keyword !speclist, Some arg
0db310c
+          with Not_found -> stop (Unknown s)
0db310c
       in
0db310c
-      let no_arg () = () in
0db310c
+      let no_arg () =
0db310c
+        match follow with
0db310c
+        | None -> ()
0db310c
+        | Some arg -> stop (Wrong (s, arg, "no argument")) in
0db310c
       let get_arg () =
0db310c
-        if !current + 1 < l then argv.(!current + 1)
0db310c
-        else stop (Missing s)
0db310c
+        match follow with
0db310c
+        | None ->
0db310c
+          if !current + 1 < l then argv.(!current + 1)
0db310c
+          else stop (Missing s)
e4cc04a
+        | Some arg ->
e4cc04a
+          decr current;
e4cc04a
+          arg
0db310c
       in
0db310c
       begin try
0db310c
         let rec treat_action = function
0db310c
diff --git a/stdlib/arg.mli b/stdlib/arg.mli
fa72b7f
index 0999edf..71af638 100644
0db310c
--- a/stdlib/arg.mli
0db310c
+++ b/stdlib/arg.mli
0db310c
@@ -25,7 +25,8 @@
0db310c
     [Unit], [Set] and [Clear] keywords take no argument. A [Rest]
0db310c
     keyword takes the remaining of the command line as arguments.
0db310c
     Every other keyword takes the following word on the command line
0db310c
-    as argument.
0db310c
+    as argument.  For compatibility with GNU getopt_long, [keyword=arg]
0db310c
+    is also allowed.
0db310c
     Arguments not preceded by a keyword are called anonymous arguments.
0db310c
 
0db310c
    Examples ([cmd] is assumed to be the command name):
0db310c
-- 
23620d4
2.3.1
0db310c