Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /smlnj-lib/trunk/Util/getopt-sig.sml
ViewVC logotype

Annotation of /smlnj-lib/trunk/Util/getopt-sig.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4244 - (view) (download)

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

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0