1 (* GetOpt.def 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 DEFINITION MODULE GetOpt ;
29 FROM SYSTEM IMPORT ADDRESS ;
30 FROM DynamicStrings IMPORT String ;
34 required_argument = 1 ;
35 optional_argument = 2 ;
39 PtrToInteger = POINTER TO INTEGER ;
42 GetOpt - call C getopt and fill in the parameters:
43 optarg, optind, opterr and optopt.
46 PROCEDURE GetOpt (argc: INTEGER; argv: ADDRESS; optstring: String;
48 VAR optind, opterr, optopt: INTEGER) : CHAR ;
52 InitLongOptions - creates and returns a LongOptions empty array.
55 PROCEDURE InitLongOptions () : LongOptions ;
59 AddLongOption - appends long option {name, has_arg, flag, val} to the
60 array of options and new long options array is
62 The old array, lo, should no longer be used.
65 The meanings of the different fields are:
67 name is the name of the long option.
70 is: no_argument (or 0) if the option does not take an
71 argument; required_argument (or 1) if the option
72 requires an argument; or optional_argument (or 2) if
73 the option takes an optional argument.
75 flag specifies how results are returned for a long option.
76 If flag is NULL, then getopt_long() returns val.
77 (For example, the calling program may set val to the
78 equivalent short option character). Otherwise,
79 getopt_long() returns 0, and flag points to a
80 variable which is set to val if the option is found,
81 but left unchanged if the option is not found.
83 val is the value to return, or to load into the variable
86 The last element of the array must be filled with zeros.
89 PROCEDURE AddLongOption (lo: LongOptions; index: CARDINAL;
90 name: String; has_arg: INTEGER;
91 VAR flag: INTEGER; val: INTEGER) : LongOptions ;
95 KillLongOptions - returns NIL and also frees up memory
99 PROCEDURE KillLongOptions (lo: LongOptions) : LongOptions ;
103 GetOptLong - works like GetOpt but will accept long options (using
104 two dashes). If the program only accepts long options
105 then optstring should be an empty string, not NIL.
108 PROCEDURE GetOptLong (argc: INTEGER; argv: ADDRESS;
109 optstring: String; longopts: LongOptions;
110 VAR longindex: INTEGER) : INTEGER ;
114 GetOptLongOnly - works like GetOptLong except that a single dash
115 can be used for a long option.
118 PROCEDURE GetOptLongOnly (argc: INTEGER; argv: ADDRESS;
119 optstring: String; longopts: LongOptions;
120 VAR longindex: INTEGER) : INTEGER ;