Path: csiph.com!aioe.org!P703Hxu1m1uplaQVJzdzug.user.46.165.242.75.POSTED!not-for-mail From: news@zzo38computer.org.invalid Newsgroups: comp.lang.postscript Subject: getopt.ps Date: Sun, 01 Aug 2021 14:05:14 -0700 Organization: Aioe.org NNTP Server Message-ID: <1627849972.bystand@zzo38computer.org> Mime-Version: 1.0 Injection-Info: gioia.aioe.org; logging-data="62132"; posting-host="P703Hxu1m1uplaQVJzdzug.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org"; User-Agent: bystand/1.2.0 X-Notice: Filtered by postfilter v. 0.9.2 Xref: csiph.com comp.lang.postscript:3662 I wrote a PostScript program for parsing command-line switches similar to how the getopt function of UNIX is doing. I might use it myself in some of my own programs, but it might be useful for other programmers to use, too. You can also tell me if you have a suggestion for improvement, too. Long option parameters without = are not implemented, and it will always use the POSIXLY_CORRECT kind of parsing. ***BEGIN*** % PostScript code for parsing command-line arguments % (public domain) << /V [0 0] /L ARGUMENTS length >> begin /D exch def /G {//D 1 index known {//D exch get} {clear //D /Error get exec stop} ifelse} def /T << /N { % No value exch pop //null exch exec } bind /O { % Optional value //V 1 get 3 -1 roll 1 add 1 index length 1 index sub getinterval dup () eq {pop //null} if exch exec exit } bind /R { % Required value //V 1 get 3 -1 roll 1 add 1 index length 1 index sub getinterval dup () eq { pop //V 0 get 1 add //V 0 2 index put //ARGUMENTS length 1 index eq {//G 5 get exec} if //ARGUMENTS exch get } if exch exec exit } bind >> def { { //V 0 get //L ge {exit} if //ARGUMENTS //V 0 get get dup length 2 lt {pop exit} if dup (--) eq {pop //V 0 //V 0 get 1 add put exit} if dup 0 get 45 ne {pop exit} if dup 1 get 45 eq { % Long option (=) search { exch pop //G exec exec } { //G exec //null exch exec } ifelse } { % Short option //V 1 2 index put 1 exch 1 exch length 1 sub { //V 1 get 1 index 1 getinterval //G exec dup 1 get dup type /nametype eq //G if exch 0 get //T exch get exec } for } ifelse //V 0 //V 0 get 1 add put } loop //userdict /ARGUMENTS //ARGUMENTS //V 0 get //L 1 index sub getinterval put } end bind exec ***END*** -- Don't laugh at the moon when it is day time in France.