Debian Codegraph @

gcc/testsuite/gm2/pimlib/run/pass/testgetopt.mod

raw
1 MODULE testgetopt ;
2
3 FROM libc IMPORT printf, exit ;
4 FROM GetOpt IMPORT InitLongOptions, KillLongOptions, AddLongOption,
5 GetOptLong, PtrToInteger, LongOptions ;
6 FROM DynamicStrings IMPORT String, InitString, string ;
7 IMPORT UnixArgs ;
8 FROM Storage IMPORT ALLOCATE ;
9 FROM SYSTEM IMPORT ADR ;
10
11
12 (*
13 Assert -
14 *)
15
16 PROCEDURE Assert (condition: BOOLEAN) ;
17 BEGIN
18 IF NOT condition
19 THEN
20 printf ("assert failed, condition is false\n") ;
21 exit (1)
22 END
23 END Assert ;
24
25
26
27 (*
28 test -
29 *)
30
31 PROCEDURE test ;
32 VAR
33 result : INTEGER ;
34 optstring: String ;
35 i, val : INTEGER ;
36 ch : CHAR ;
37 BEGIN
38 longopts := AddLongOption (longopts, 0, InitString ('help'), 0, result, 0) ;
39 longopts := AddLongOption (longopts, 1, InitString ('dir'), 1, result, 0) ;
40 longopts := AddLongOption (longopts, 2, NIL, 0, result, 0) ;
41 optstring := InitString ('hd:') ;
42 i := 1 ;
43 REPEAT
44 val := GetOptLong (UnixArgs.GetArgC (), UnixArgs.GetArgV (),
45 optstring, longopts, i) ;
46 IF val = 0
47 THEN
48 printf ("long option detected, result = %d, val = %d, index i = %d, optstring = %s\n",
49 result, val, i, string (optstring))
50 ELSIF val > 0
51 THEN
52 ch := VAL (CHAR, val) ;
53 CASE ch OF
54
55 'h': printf ("short option 'h' seen\n")
56
57 ELSE
58 printf ("unknown short option '%c' seen\n", ch)
59 END
60 ELSE
61 printf ("unknown long option\n")
62 END ;
63 INC (i)
64 UNTIL val <= 0
65 END test ;
66
67
68 VAR
69 longopts: LongOptions ;
70 BEGIN
71 longopts := InitLongOptions () ;
72 test ;
73 longopts := KillLongOptions (longopts)
74 END testgetopt.