The arg parser module implements a command line arguments parser. Due to the fact that the ANS standard does not specify words for arguments this module has a environmental dependency.
Supported option formats:
-v short switch option
-f a.txt short option with parameter
-vq multiple short switch options
--file=a.txt long option with parameter
--verbose long switch option
-- stop parsing arguments
include ffl/arg.fs
\ Test if the argument parser is implemented for the current forth Engine
[DEFINED] a32.version [IF]
\ Create an argument parser on the heap
s" argparser" \ program name
s" [OPTION] .. [FILES]" \ program usage
s" v1.0" \ program version
s" Report bugs to bugs@bugs.com" \ program extra info
arg-new value arg1
\ Add the default help and version options
arg1 arg-add-help-option
arg1 arg-add-version-option
\ Variable for the verbose switch
variable verbose verbose off
\ Add the -v/--verbose option switch
char v \ Short option name
s" verbose" \ Long option name
s" activate verbose mode" \ Description
true \ Switch -> true
4 \ Option id
arg1 arg-add-option
\ Add the -f/--file=FILE option
char f \ Short option name
s" file=FILE" \ Long option name
s" set input file, any input file is allowed, as long as the description is multicolumn" \ Description
false \ Parameter -> false
5 \ Option id
arg1 arg-add-option
: parse-options ( -- )
BEGIN
arg1 arg-parse \ parse the next argument
dup arg.done <> over arg.error <> AND \ stop parsing when ready or after an error
WHILE
CASE
arg.help-option OF arg1 arg-print-help ENDOF \ print default help info
arg.version-option OF arg1 arg-print-version ENDOF \ print default version info
arg.non-option OF ." Non option found:" type cr ENDOF \ non option parameter, parameter on stack
4 OF verbose on ." Verbose is on" cr ENDOF \ switch, no extra stack parameters
5 OF ." File parameter:" type cr ENDOF \ parameter switch, parameter on stack
ENDCASE
REPEAT
arg.done = IF
." All options okee." cr
ELSE
." Error in one of the options." cr
THEN
;
\ Parse the command line arguments
parse-options
\ Free the argument parser from the heap
arg1 arg-free
[THEN]