ocaml-getopt-0.0.20040811/0000755000175000017500000000000010106403276015151 5ustar furrmfurrm00000000000000ocaml-getopt-0.0.20040811/getopt.mli0000644000175000017500000001032010041222102017133 0ustar furrmfurrm00000000000000(** Module [Getopt]: parsing of command line arguments. Copyright (C) 2000-2004 Alain Frisch. Distributed under the terms of the MIT license. email: {{:Alain.Frisch\@ens.fr}Alain Frisch\@ens.fr} web: {{:http://www.eleves.ens.fr/home/frisch}http://www.eleves.ens.fr/home/frisch} This module provides a general mechanism for extracting options and arguments from the command line to the program. It is an alternative to the module [Arg] from the standard OCaml distribution. The syntax is close to GNU getopt and getop_long ([man 3 getopt]). *) (** {1 Layout of the command line} There are two types of argument on the command line: options and anonymous arguments. Options may have two forms: a short one introduced by a single dash character (-) and a long one introduced by a double dash (--). Options may have an argument attached. For the long form, the syntax is "--option=argument". For the short form, there are two possible syntaxes: "-o argument" (argument doesn't start with a dash) and "-oargument" Short options that refuse arguments may be concatenated, as in "-opq". The special argument -- interrupts the parsing of options: all the remaining arguments are arguments even they start with a dash. {1 Command line specification} A specification lists the possible options and describe what to do when they are found; it also gives the action for anonymous arguments and for the special option - (a single dash alone). *) type opt = char * string * ((unit -> unit) option) * ((string -> unit) option) (** The specification for a single option is a tuple [(short_form, long_form, action, handler)] where: - [short_form] is the character for the short form of the option without the leading - (or [noshort='\000'] if the option does not have a short form) - [long_form] is the string for the long form of the option without the leading -- (or [nolong=""] if no long form) - [(action : (unit -> unit) option)] gives the action to be executed when the option is found without an argument - [(handler : (string -> unit) option)] specifies how to handle the argument when the option is found with the argument According to the pair [(action, handler)], the corresponding option may, must or mustn't have an argument : - [(Some _, Some _)] : the option may have an argument; the short form can't be concatenated with other options (even if the user does not want to provide an argument). The behaviour (handler/action) is determined by the presence of the argument. - [(Some _, None)] : the option must not have an argument; the short form, if it exists, may be concatenated - [(None, Some _)] : the option must have an argument; the short form can't be concatenated - [(None, None)] : not allowed *) (** [noshort='\000'] can be used when an option has no short form *) val noshort : char (** [nolong=""] can be used when an option has no long form *) val nolong : string (** Signals error on the command line *) exception Error of string (** {1 Parsing the command line} *) val parse : opt list -> (string -> unit) -> string array -> int -> int -> unit (** [parse opts others args first last] parse the arguments [args.(first)], [arg.(first+1)] ... [args.(last)]. [others] is called on anonymous arguments (and the special - argument); [opts] is a list of option specifications (there must be no ambiguities). @raise Error : Unknown options, unexpected argument, ... *) val parse_cmdline : opt list -> (string -> unit) -> unit (** Parse the command line in [Sys.argv] using [parse]. *) (** {1 Useful actions and handlers} *) val set : 'a ref -> 'a -> ((unit -> unit) option) (** @return an action that gives a reference a given value *) val incr : int ref -> ((unit -> unit) option) (** @return an action that increments an [int] reference *) val append : string list ref -> ((string -> unit) option) (** @return an handler that appends the argument to the end of a [string list] reference *) val atmost_once : string ref -> exn -> ((string -> unit) option) (** @return an handler that stores the argument in a [string] reference if it is empty, raises an exception otherwise *) ocaml-getopt-0.0.20040811/getopt.ml0000644000175000017500000000610710041216363017006 0ustar furrmfurrm00000000000000(* Module [Getopt]: parsing of command line arguments *) (* Alain Frisch *) let noshort = '\000' let nolong = "" type opt = char * string * ((unit -> unit) option) * ((string -> unit) option) exception Error of string let index_option s c = try Some (String.index s c) with Not_found -> None let extract_arg_handle opt = function | (_,_,_,Some handle) -> handle | _ -> raise (Error (Printf.sprintf "Option %s does not accept argument" opt)) let extract_handle opt = function | (_,_,Some handle,_) -> handle | _ -> raise (Error (Printf.sprintf "Option %s must have an argument" opt)) let parse opts others args first last = let find_long opt = try List.find (fun (_,l,_,_) -> opt = l) opts with Not_found -> raise (Error (Printf.sprintf "Unknown option --%s" opt)) in let find_short opt = try List.find (fun (l,_,_,_) -> opt = l) opts with Not_found -> raise (Error (Printf.sprintf "Unknown option -%c" opt)) in (* Anonymous arguments after -- *) let rec skip no = if (no <= last) then (others args.(no); skip (succ no)) in let rec aux no = if (no <= last) then let s = args.(no) in let l = String.length s in if (l=0) then (others s; aux (succ no)) else if (s.[0] = '-') then if (l >= 2) && (s.[1] = '-') then if (l = 2) then skip (succ no) (* -- *) else match index_option s '=' with | Some i -> (* long option with argument *) let opt = String.sub s 2 (i-2) in let arg = String.sub s (i+1) (l-i-1) in let handle = extract_arg_handle ("--"^opt) (find_long opt) in handle arg; aux (succ no) | None -> (* long option with no argument *) let opt = String.sub s 2 (l-2) in let handle = extract_handle s (find_long opt) in handle (); aux (succ no) else if (l = 1) then (others s; aux (succ no)) (* - *) else (* short option *) let opt = s.[1] in match find_short opt with | (_,_,Some handle,None) -> (* no argument allowed; next chars are options *) handle (); for i = 2 to (l - 1) do match find_short s.[i] with | (_,_,Some handle,None) -> handle () | _ -> raise (Error (Printf.sprintf "Only non-argument short-options can be concatenated (error with option %c in %s)" s.[i] s)) done; aux (succ no) | (_,_,_,Some handle) as o -> (* argument allowed or mandatory *) if (l>2) then (* immediate argument *) (handle (String.sub s 2 (l-2)); aux (succ no)) else if (no+1 <= last) && (args.(no+1).[0] <> '-') then (* non-immediate argument *) (handle args.(no+1); aux (no+2)) else (* no argument *) let handle = extract_handle s o in (handle (); aux (succ no)) | _ -> failwith "Getopt.parse" else (others s; aux (succ no)) in aux first let parse_cmdline opts others = parse opts others Sys.argv 1 (Array.length Sys.argv - 1) (* useful actions and handlers *) let set var value = Some (fun () -> var := value) let append lst = Some (fun x -> lst := !lst@[x]) let incr var = Some (fun () -> Pervasives.incr var) let atmost_once var exc = Some (fun x -> if !var="" then var := x else raise exc) ocaml-getopt-0.0.20040811/COPYING0000644000175000017500000000205210106403137016177 0ustar furrmfurrm00000000000000Copyright (c) 2004 by Alain Frisch The package "getopt" is copyright by Alain Frisch. Permission is hereby granted, free of charge, to any person obtaining a copy of the "getopt" software (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. The Software is provided ``as is'', without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall Alain Frisch be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the Software or the use or other dealings in the software. ocaml-getopt-0.0.20040811/Changes0000644000175000017500000000026010106403203016430 0ustar furrmfurrm000000000000002000-07-15 Initial release 2000-08-04 Findlib installation 2003-03-01 Bug fix in Makefile 2004-04-20 Cleanup, ocamldoc'ed, MIT license 2004-08-11 Typos in COPYINGocaml-getopt-0.0.20040811/sample.ml0000644000175000017500000000166507134124507017000 0ustar furrmfurrm00000000000000(* Demonstration of the Getopt module *) open Getopt let archive = ref false and update = ref false and verbose = ref 0 and includ = ref [] and output = ref "" let bip () = Printf.printf "\007"; flush stdout let wait () = Unix.sleep 1 let specs = [ ( 'x', "execute", None, Some (fun x -> Printf.printf "execute %s\n" x)); ( 'I', "include", None, (append includ)); ( 'o', "output", None, (atmost_once output (Error "only one output"))); ( 'a', "archive", (set archive true), None); ( 'u', "update", (set update true), None); ( 'v', "verbose", (incr verbose), None); ( 'X', "", Some bip, None); ( 'w', "wait", Some wait, None) ] let _ = parse_cmdline specs print_endline; Printf.printf "archive = %b\n" !archive; Printf.printf "update = %b\n" !update; Printf.printf "verbose = %i\n" !verbose; Printf.printf "output = %s\n" !output; List.iter (fun x -> Printf.printf "include %s\n" x) !includ;; ocaml-getopt-0.0.20040811/README0000644000175000017500000000250210041222556016026 0ustar furrmfurrm00000000000000Getopt: command line parsing à la GNU getopt Copyright (C) 2000-2004 Alain Frisch distributed under an MIT-like license (see the COPYING file) email: Alain.Frisch@ens.fr web: http://www.eleves.ens.fr:8080/home/frisch -------------------------------------------------------------------------- Contents of the distribution Changes History of code changes COPYING MIT license README This file getopt.ml source of Getopt (implementation) getopt.mli source of Getopt (interface) sample.ml a sample program to demonstrate the use of the module Makefile Makefile to build the module and the sample program -------------------------------------------------------------------------- What is it ? The OCaml distribution comes with the module Arg specialized in command-line parsing. However, it doesn't support the well known features of GNU getopt and getopt_long. The module Getopt is an alternative; it supports GNU syntax, but from the programmer point of view, it is close to the spirit of Arg: the programmer gives to the general parsing function a list of possible options, together with the behaviour of these options. -------------------------------------------------------------------------- General usage information The documentation is included in the file getopt.mli. There is an example in sample.ml. ocaml-getopt-0.0.20040811/META0000644000175000017500000000014010041214655015614 0ustar furrmfurrm00000000000000requires = "" version = "20040420" archive(byte) = "getopt.cma" archive(native) = "getopt.cmxa" ocaml-getopt-0.0.20040811/Makefile0000644000175000017500000000142510041223674016613 0ustar furrmfurrm00000000000000all: doc getopt.cmi getopt.cma allopt: doc getopt.cmi getopt.cma getopt.cmxa getopt.cmo: getopt.cmi getopt.ml ocamlc -c getopt.ml getopt.cmi: getopt.mli ocamlc -c getopt.mli getopt.cma: getopt.cmo ocamlc -o getopt.cma -a getopt.cmo getopt.cmxa: getopt.cmx ocamlopt -o getopt.cmxa -a getopt.cmx getopt.cmx: getopt.cmi getopt.ml ocamlopt -c getopt.ml sample.cmo: getopt.cmi sample.ml ocamlc -c sample.ml sample: getopt.cma sample.cmo ocamlc -o sample unix.cma getopt.cma sample.cmo install: ocamlfind install getopt META getopt.cmi getopt.cma $(wildcard getopt.cmxa) $(wildcard getopt.o) $(wildcard getopt.a) uninstall: ocamlfind remove getopt .PHONY: doc doc: mkdir -p doc ocamldoc -d doc -html getopt.mli clean: rm -f *.cm[ioxa] *.cmxa *.a *.o sample *~ rm -Rf doc ocaml-getopt-0.0.20040811/.nfs0055423c000000020000644000175000017500000000205710041222026017442 0ustar furrmfurrm00000000000000Copyright (c) 2004 by Alain Frisch The package "getopt" is copyright by Alain Frisch. Permission is hereby granted, free of charge, to any person obtaining a copy of the "netclient" software (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. The Software is provided ``as is'', without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall Gerd Stolpmann be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the Software or the use or other dealings in the software.