Re: Questionable example in the documentation for command line arguments

Bernd Paysan <[email protected]> Tue, 14 Apr 2026 10:19:51 +0200
Newsgroups gmane.comp.lang.forth.gforth
Message-ID <3689624.dWV9SEqChM@wukong>
--nextPart5779021.rdbgypaU67
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset="utf-8"; protected-headers="v1"
From: Bernd Paysan <[email protected]>
To: "Shawn K. Quinn" <skquinn-ka0BGeLORx5Wk0Htik3J/[email protected]>, gforth-mXXj517/[email protected]
Cc: gforth-mXXj517/[email protected], anton-8NJIiSa5LzB3ZGU3T6kWFyEcasqEdqbRs2V0tQ3P5b8@public.gmane.org
Date: Tue, 14 Apr 2026 10:19:51 +0200
Message-ID: <3689624.dWV9SEqChM@wukong>
In-Reply-To: <ad0M7MNwJ6E8JFKj-eMTcDWn+JWX/RkxMZTRrOJdqbFPxfnh/@public.gmane.org>
MIME-Version: 1.0

Am Montag, 13. April 2026, 17:34:04 Mitteleurop=C3=A4ische Sommerzeit schri=
eb Anton=20
Ertl:
> Instead, plug your word into "'COLD".  Best read the whole
> Section "Modifying the Startup Sequence".

The documentation there is a bit short. The right way to deal with special=
=20
treatment of command line options in your user programm is to either hook=20
PROCESS-OPTIONS (this is a recognizer, so it has to follow the recognizer=20
API), or add words to the vocabulary OPTIONS.  E.g. if you have a command t=
hat=20
needs to change to a directory, you can add an option:

get-current also options definitions

: --directory ( -- )
  next-arg 2dup d0<> if
      set-dir throw
  else
      2drop \ maybe also print out a warning
  then ;
synonym -d --directory

previous set-current

Then you can start your program with

myprogram -d $HOME/.local/share/myprogram

or wherever the files are kept.

Or, if you want to match more complex patterns (e.g. all directories starti=
ng=20
with /, ./, or ../ passed are cd'd to), you can create a recognizer for tha=
t=20
and add it to the recognizer sequence PROCESS-VOC-OPTION.

If your way of dealing with arguments doesn't fit into the way PROCESS-ARGS=
=20
does it (i.e. recognize each argument), then you need to stick it in 'COLD=
=20
before, but this only works if you save your program as image.  If you load=
=20
your program as script, just perform your argument parsing yourself, becaus=
e=20
you are already inside the PROCESS-ARGS loop.  There, the best thing is=20
something like

script? [IF]
    next-arg 2dup d<> [IF]  set-dir throw  [ELSE]  2drop  [THEN]
[THEN]

before you start reading your files.  Then you can start it with

gforth myprogram.fs $HOME/.local/share/myprogram

BTW: ARGC is a variable, so you might want to check ARGC @ instead of ARGC.=
 =20
ARGC is 1 if no argument is passed, because the program name is always pass=
ed=20
as argument 0.  Checking NEXT-ARG for 0 0 also works.

=2D-=20
Bernd Paysan
"If you want it done right, you have to do it yourself"
net2o id: kQusJzA;7*?t=3Duy@X}1GWr!+0qqp_Cn176t4(dQ*
https://net2o.de/

--nextPart5779021.rdbgypaU67
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: This is a digitally signed message part.
Content-Transfer-Encoding: 7Bit

-----BEGIN PGP SIGNATURE-----

iQJPBAABCAA5FiEERJ1NDqPQRwYnwBjr9y2Uk5MtoGcFAmnd+KcbFIAAAAAABAAO
bWFudTIsMi41KzEuMTIsMiwyAAoJEPctlJOTLaBn+/EP/1ClTUY9U4tcnQwC3A3i
EE0TbiEyiSNLMpKV0H62mIKR+FytyCjK9OdnnDUh3+E56MURg9rO+2rzSLPGKc0k
ZsS9gzHDzGIF9kDqV3NeSP9f35vQbJa7ogMBU6/xaZ7dduMVnTMW9USY+XU32IY/
Pia86CReSW4SWxi4llBZs7o/u5m9UN3yOabIhaj7ySZFLmGM2vNzNVF2gqyMkanS
4lKLpKh7fyShsPLlmrjZeFvyjuXT+R5UiwiEN8PpdwcS/rQjn+CYgFss1Juic1YJ
0+QfbyOfic4tOY9dKNvaWzU4urF01gKv6h68Pcb2MhikrsslhG615HI0Vux1SwAv
xNbKoY2eU7WXafx5UdJcQltxlCkrgW950oukmJVH0pNEYrtbaw2cmeaIkAGHaljc
UdKUGL5g4Cs6GqZar1uFeelaeb8Q3AH46jIi9MDBcnYzJKH/L67qa/KPbvt8AyI9
q+CRjUsZttNbJb20nY7wwauG8aRVOKffZQtXETxjvf2/ppbl3lCqXU4aov556dgl
kPOAq7f8D/N2l+TDem7jA07tNaKzS8UXamTI87MIZV9mwr5uwEQ5Bp2xsHrxagzi
CqEFda+EjsjwNdejNYu1LIEbl5AjbJev3Oefn/pZDLR0S68toQvMNwRGai6QexoT
vvl0uKBKnjj16GiCQX3qbVGk
=TReu
-----END PGP SIGNATURE-----

--nextPart5779021.rdbgypaU67--