New commandline parser

The s(CASP) executable scasp processes zillions of commandline options that were programmed rather ad hoc. SWI-Prolog has two commandline option parsers, one in library(main) as argv_options/3 and one in library(optparse). The first is simple minded, only works with long options as --option, --no-option or --option=value. The advantage is that this option processing requires no declarations. The opt_parse/4 is rather complicated and slow. Even using only a small part of the s(CASP) defined options and a simple commandline, parsing took already 5ms.

I’ve added an extension to argv_options/3 to provide type and (optionally) help info that turns it into the current unguided parser if nothing is specified and turns it into a guided full featured parser by adding clauses for opt_type/3, e.g.

opt_type(f, file, file(read)).

Which specifies there is a commandline flag -f that takes a file argument for a file that needs read access and creates an option term file(Name).

As the specification is in clauses we are not faced with a giant specification term where it is too easy to make a hard-to-find syntax error. Using all the usual Prolog rules we can easily gather additional commandline specifications from plugins. Help is separated in clauses, e.g. opt_help(file, "File to process"), which we can as discontiguous predicates mix opt_type/3 or move to e.g. language files.

With little work we now get the scasp -h output as below. The parse time can barely be measured.

Comments are welcome. Fetch the current git and run ?- help(argv_options). for the docs.

4 Likes