(* Copyright (C) 1990, Digital Equipment Corporation. *) (* All rights reserved. *) (* See the file COPYRIGHT for a full description. *) (* Last modified on Sat Apr 7 00:16:00 1990 by muller *) (* modified on Fri Sep 29 11:37:25 1989 by kalsow *) (* modified on Fri Mar 4 22:59:47 1988 by stolfi *) (* Tests the ParseParams package *) (* This program implements the command ParseParamsTest -width [ -landscape ] [ -pages ] ... [ file ... ] where the switches can be given in any order, and a file may be just "-". If the parameters are correct, the command just prints them out. If they are not, the command prints an error message. *) MODULE TestParseParams EXPORTS Main; IMPORT ParseParams, Wr, Text, Fmt, Scan, OS; FROM Stdio IMPORT stderr; TYPE Orientation = {Portrait, Landscape}; CONST maxRanges = 1000; maxFiles = 1000; VAR orient: Orientation; width: INTEGER; weight: REAL; nRanges, i: INTEGER; ini, fin: ARRAY [0..maxRanges - 1] OF INTEGER; nFiles: INTEGER; file: ARRAY [0..maxFiles - 1] OF Text.T; command: Text.T; oriens: ARRAY Orientation OF Text.T; BEGIN oriens[Orientation.Landscape] := "Landscape"; oriens[Orientation.Portrait] := "Portrait"; TRY Wr.PutText (stderr, "\nHi there!\n\n"); ParseParams.BeginParsing (stderr); FOR z := 0 TO ParseParams.NumParameters - 1 DO i := z; Wr.PutText (stderr, "Parameter[" & Fmt.Int (i) & "] = " & ParseParams.GetParameter (i) & "\n"); END; command := ParseParams.GetParameter (0); Wr.PutText (stderr, "\nCommand = " & command & "\n"); ParseParams.GetKeyword ("-width"); width := ParseParams.GetNextInt (1, 100); Wr.PutText (stderr, "Width = " & Fmt.Int (width) & "\n"); IF ParseParams.KeywordPresent ("-landscape") THEN orient := Orientation.Landscape ELSE orient := Orientation.Portrait END; Wr.PutText (stderr, "Orientation = " & oriens[orient] & "\n"); IF ParseParams.KeywordPresent ("-weight") THEN weight := ParseParams.GetNextReal (0.0000000e+00, 1.0000000e+00) ELSE weight := 5.5000001e-01 END; Wr.PutText (stderr, "Weight = " & Fmt.Real (weight, 6, Fmt.Style.Flo) & "\n"); Wr.PutText (stderr, "Pages:\n"); nRanges := 0; WHILE ParseParams.KeywordPresent ("-pages") DO ini[nRanges] := ParseParams.GetNextInt (1, 10000); fin[nRanges] := ParseParams.GetNextInt (ini[nRanges], 10000); Wr.PutText (stderr, " " & Fmt.Int (ini[nRanges]) & " -- " & Fmt.Int (fin[nRanges]) & "\n"); nRanges := nRanges + 1; END; Wr.PutText (stderr, "Files:\n"); ParseParams.UnparsedTail (); nFiles := 0; WHILE ParseParams.NextParameter < ParseParams.NumParameters DO IF ParseParams.TestNext ("-") THEN file[nFiles] := "**stdin**" ELSE file[nFiles] := ParseParams.GetNext () END; Wr.PutText (stderr, " " & file[nFiles] & "\n"); nFiles := nFiles + 1 END; ParseParams.EndParsing (); EXCEPT Scan.BadFormat => Wr.PutText (stderr, "\nOops, syntax error...\n\n"); Wr.PutText (stderr, "Usage: " & command & " [ -pages n1 n2 ]... "); Wr.PutText (stderr, "-width n3 [ -landscape ] "); Wr.PutText (stderr, "[ -weight n4 ] [ file ... ]\n"); OS.Exit (1) END; Wr.PutText (stderr, "\nParameters seem ok to me...\n"); Wr.PutText (stderr, "Thank you very much for your request.\n"); Wr.PutText (stderr, "I will think about it one of these days.\n"); Wr.PutText (stderr, "Good bye.\n\n") END ParseParamsTest.