pax_global_header00006660000000000000000000000064143733675720014533gustar00rootroot0000000000000052 comment=8855ac892c57920c9c537109caca9bb579c0b061 ocaml-getopt-20230213/000077500000000000000000000000001437336757200143645ustar00rootroot00000000000000ocaml-getopt-20230213/.gitignore000066400000000000000000000000111437336757200163440ustar00rootroot00000000000000/_build/ ocaml-getopt-20230213/COPYING000066400000000000000000000020521437336757200154160ustar00rootroot00000000000000Copyright (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-20230213/Changes000066400000000000000000000004121437336757200156540ustar00rootroot000000000000002000-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 COPYING 2012-01-18 Oasis packaging 2023-02-13 OCaml 5.0 compatibility + dune packaging ocaml-getopt-20230213/README.md000066400000000000000000000015441437336757200156470ustar00rootroot00000000000000# Getopt: command line parsing à la GNU getopt Copyright (C) 2000-2004 Alain Frisch distributed under an MIT-like license (see the COPYING file) -------------------------------------------------------------------------- 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 test/getopt_test.ml ocaml-getopt-20230213/dune-project000066400000000000000000000010631437336757200167060ustar00rootroot00000000000000(lang dune 2.0) (name getopt) (generate_opam_files true) (source (github scemama/ocaml-getopt)) (authors "Alain Frisch") (maintainers "Anthony Scemama") (license MIT) (package (name getopt) (synopsis "Parsing of command line arguments (similar to GNU GetOpt) for OCaml") (description "General command line syntax of GNU getopt and getopt_long, but is close to the spirit of the Arg module.") (depends ocaml dune) (tags (topics "command-line"))) ; See the complete stanza docs at https://dune.readthedocs.io/en/stable/dune-files.html#dune-project ocaml-getopt-20230213/getopt.opam000066400000000000000000000014451437336757200165500ustar00rootroot00000000000000# This file is generated by dune, edit dune-project instead opam-version: "2.0" synopsis: "Parsing of command line arguments (similar to GNU GetOpt) for OCaml" description: "General command line syntax of GNU getopt and getopt_long, but is close to the spirit of the Arg module." maintainer: ["Anthony Scemama"] authors: ["Alain Frisch"] license: "MIT" tags: ["topics" "command-line"] homepage: "https://github.com/scemama/ocaml-getopt" bug-reports: "https://github.com/scemama/ocaml-getopt/issues" depends: [ "ocaml" {>= "4.07"} "dune" {>= "3.6"} "odoc" {with-doc} ] build: [ ["dune" "subst"] {dev} [ "dune" "build" "-p" name "-j" jobs "@install" "@runtest" {with-test} "@doc" {with-doc} ] ] dev-repo: "git+https://github.com/scemama/ocaml-getopt.git" ocaml-getopt-20230213/lib/000077500000000000000000000000001437336757200151325ustar00rootroot00000000000000ocaml-getopt-20230213/lib/.merlin000066400000000000000000000002561437336757200164240ustar00rootroot00000000000000EXCLUDE_QUERY_DIR B ../_build/default/lib/.getopt.objs/byte S . FLG -w @1..3@5..28@30..39@43@46..47@49..57@61..62-40 -strict-sequence -strict-formats -short-paths -keep-locs ocaml-getopt-20230213/lib/dune000066400000000000000000000000571437336757200160120ustar00rootroot00000000000000(library (public_name getopt) (name getopt)) ocaml-getopt-20230213/lib/getopt.ml000066400000000000000000000061031437336757200167660ustar00rootroot00000000000000(* 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 () -> Stdlib.incr var) let atmost_once var exc = Some (fun x -> if !var="" then var := x else raise exc) ocaml-getopt-20230213/lib/getopt.mli000066400000000000000000000103201437336757200171330ustar00rootroot00000000000000(** 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-20230213/test/000077500000000000000000000000001437336757200153435ustar00rootroot00000000000000ocaml-getopt-20230213/test/.merlin000066400000000000000000000004121437336757200166270ustar00rootroot00000000000000EXCLUDE_QUERY_DIR B /usr/lib/ocaml B ../_build/default/lib/.getopt.objs/byte B ../_build/default/test/.getopt_test.eobjs/byte S /usr/lib/ocaml S ../lib S . FLG -w @1..3@5..28@30..39@43@46..47@49..57@61..62-40 -strict-sequence -strict-formats -short-paths -keep-locs ocaml-getopt-20230213/test/dune000066400000000000000000000000651437336757200162220ustar00rootroot00000000000000(test (name getopt_test) (libraries getopt unix) ) ocaml-getopt-20230213/test/getopt_test.ml000066400000000000000000000016651437336757200202460ustar00rootroot00000000000000(* 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;;