1 (* GetOpt.mod allows users to manage long options to getopt.
3 Copyright (C) 2017-2024 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
6 This file is part of GNU Modula-2.
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. *)
27 IMPLEMENTATION MODULE GetOpt ; (*!m2pim+gm2*)
29 FROM DynamicStrings IMPORT string, InitStringCharStar ;
30 FROM Storage IMPORT ALLOCATE, REALLOCATE, DEALLOCATE ;
31 FROM MemUtils IMPORT MemCopy ;
37 LongOptions = POINTER TO RECORD
43 GetOpt - call C getopt and fill in the parameters:
44 optarg, optind, opterr and optop.
47 PROCEDURE GetOpt (argc: INTEGER; argv: ADDRESS; optstring: String;
49 VAR optind, opterr, optopt: INTEGER) : CHAR ;
53 r := cgetopt.getopt (argc, argv, string (optstring)) ;
54 optarg := InitStringCharStar (cgetopt.optarg) ;
55 opterr := cgetopt.opterr ;
56 optopt := cgetopt.optopt ;
62 InitLongOptions - creates and returns a LongOptions empty array.
65 PROCEDURE InitLongOptions () : LongOptions ;
71 cptr := cgetopt.InitOptions ()
78 AddLongOption - appends long option {name, has_arg, flag, val} to the
79 array of options and new long options array is returned.
80 The old array, lo, should no longer be used.
83 The meanings of the different fields are:
85 name is the name of the long option.
88 is: no_argument (or 0) if the option does not take an argument; required_argument
89 (or 1) if the option requires an argument; or optional_argument (or 2) if the
90 option takes an optional argument.
92 flag specifies how results are returned for a long option. If flag is NULL, then
93 getopt_long() returns val. (For example, the calling program may set val to the
94 equivalent short option character.) Otherwise, getopt_long() returns 0, and flag
95 points to a variable which is set to val if the option is found, but left unchanged
96 if the option is not found.
98 val is the value to return, or to load into the variable pointed to by flag.
100 The last element of the array must be filled with zeros.
103 PROCEDURE AddLongOption (lo: LongOptions; index: CARDINAL;
104 name: String; has_arg: INTEGER;
105 VAR flag: INTEGER; val: INTEGER) : LongOptions ;
107 cgetopt.SetOption (lo^.cptr, index, name, has_arg, flag, val) ;
113 KillLongOptions - returns NIL and also frees up memory associated with, lo.
116 PROCEDURE KillLongOptions (lo: LongOptions) : LongOptions ;
118 lo^.cptr := cgetopt.KillOptions (lo^.cptr) ;
121 END KillLongOptions ;
125 GetOptLong - works like GetOpt but will accept long options (using two dashes).
126 If the program only accepts long options then optstring should be
127 an empty string, not NIL.
130 PROCEDURE GetOptLong (argc: INTEGER; argv: ADDRESS; optstring: String;
131 longopts: LongOptions; VAR longindex: INTEGER) : INTEGER ;
133 RETURN cgetopt.getopt_long (argc, argv, string (optstring),
134 cgetopt.GetLongOptionArray (longopts^.cptr), longindex)
139 GetOptLongOnly - works like GetOptLong except that a single dash can be used
143 PROCEDURE GetOptLongOnly (argc: INTEGER; argv: ADDRESS; optstring: String;
144 longopts: LongOptions; VAR longindex: INTEGER) : INTEGER ;
146 RETURN cgetopt.getopt_long_only (argc, argv, string (optstring),
147 cgetopt.GetLongOptionArray (longopts^.cptr), longindex)