1(*
2 * @TAG(OTHER_PRINCETON_OSS)
3 *)
4(* getopt-sig.sml
5 *
6 * COPYRIGHT (c) 1998 Bell Labs, Lucent Technologies.
7 *
8 * A SML port of GNU's getopt library.
9 *
10 * This port is derived from Sven Panne's
11 * <Sven.Panne@informatik.uni-muenchen.de>
12 * implementation of the getopt library in Haskell <http://www.haskell.org>
13 *
14 * The following comments are lifted from Sven's code:
15 *
16 *   Two rather obscure features are missing: The Bash 2.0 non-option hack (if
17 *   you don't already know it, you probably don't want to hear about it...)
18 *   and the recognition of long options with a single dash (e.g. '-help' is
19 *   recognised as '--help', as long as there is no short option 'h').
20 *
21 *   Other differences between GNU's getopt and this implementation:
22 *     * To enforce a coherent description of options and arguments, there are
23 *       explanation fields in the option/argument descriptor.
24 *     * Error messages are now more informative, but no longer POSIX
25 *       compliant... :-(
26 *
27 *
28 *
29 * A difference with Sven's port: errors now invoke an error callback, rather
30 * than returning error strings while continuing processing options.
31 * The full generality of the latter does not seem justified.
32 *)
33
34
35signature GetOpt =
36  sig
37
38      datatype 'a arg_order
39        = RequireOrder
40        | Permute
41        | ReturnInOrder of string -> 'a
42      (* What to do with options following non-options:
43       * RequireOrder: no option processing after first non-option
44       * Permute: freely intersperse options and non-options
45       * ReturnInOrder: wrap non-options into options
46       *)
47
48      datatype 'a arg_descr
49        = NoArg of unit -> 'a
50        | ReqArg of (string -> 'a) * string
51        | OptArg of (string option -> 'a) * string
52      (* Description of an argument option:
53       * NoArg: no argument required
54       * ReqArg: option requires an argument; the string is the argument name
55       * OptArg: optional argument; the string is the argument name
56       *)
57
58      type 'a opt_descr = {
59          short : string,
60          long : string list,
61          desc : 'a arg_descr,
62          help : string
63        }
64      (* Description of a single option *)
65
66      val usageInfo : {
67          header : string,
68          options : 'a opt_descr list
69        } -> string
70      (* takes a header string and a list of option descriptions and
71       * returns a string explaining the usage information.  A newline will
72       * be added following the header, so it should not be newline terminated.
73       *)
74
75      val getOpt : {
76          argOrder : 'a arg_order,
77          options : 'a opt_descr list,
78          errFn : string -> unit
79        } -> string list -> ('a list * string list)
80      (* takes as argument an arg_order to specify the non-options
81       * handling, a list of option descriptions, an error callback,
82       * and a command line containing the options and arguments,
83       * and returns a list of (options, non-options)
84       *)
85
86  end
87