|Did you know ...||Search Documentation:|
|main.pl -- Provide entry point for scripts|
This library is intended for supporting PrologScript on Unix using the
#! magic sequence for scripts using commandline options. The entry
point main/0 calls the user-supplied predicate main/1 passing a list of
commandline options. Below is a simle
echo implementation in Prolog.
#!/usr/bin/env swipl :- initialization(main, main). main(Argv) :- echo(Argv). echo() :- nl. echo([Last]) :- !, write(Last), nl. echo([H|T]) :- write(H), write(' '), echo(T).
SIGINT(Control-C) that terminates the process with status 1.
When main/0 is called interactively it simply calls main/1 with the arguments. This allows for debugging scripts as follows:
$ swipl -l script.pl -- arg ... ?- gspy(suspect/1). % setup debugging ?- main. % run program
When guided, three predicates are called in the calling module. opt_type/3 must be defined, the others need not. Note that these three predicates may be defined as multifile to allow multiple modules contributing to the provided commandline options. Defining them as discontiguous allows for creating blocks that describe a group of related options.
-. A single character implies a short option, multiple a long option. Long options use
_as word separator, user options may use either
-. Type is one of:
nonneg|boolean, for an option
http(3000). With an optional boolean an option is considered boolean if it is the last or the next argument starts with a hyphen (
--opt=valuenotation. This explicit value specification converts
1and the obvious false equivalents to Prolog
false. If the option is specified, Default is used. If
--nooptis used, the inverse of Default is used.
integer. Requires value >= 0.
integer. Requires value >= 1.
float, else convert as
integer. Then check the range.
atom, but requires the value to be a member of List (enum type).
file, and check access using access_file/2. A value
-is not checked for access, assuming the application handles this as standard input or output.
term, but passes Options to term_string/3. If the option
variable_names(Bindings)is given the option value is set to the pair
--help are bound to help. If
opt_type(Opt, help, boolean) is true for some Opt, the default
help binding and help message are disabled and the normal user
rules apply. In particular, the user should also provide a rule for
halt(Code), exit with Code. Other goals are currently not supported.
true), stop parsing after the first positional argument, returning options that follow this argument as positional arguments. E.g,
-x file -yresults in positional arguments
debug. Other meaningful options are
warning. The help page consists of four sections, two of which are optional:
opt_help(help(header), String). It is optional.
Usage: <command>is by default
[options]and can be overruled using
opt_help(help(footer), String). It is optional.
The help provided by
either a simple string or a list of elements as defined by
print_message_lines/3. In the latter case, the construct
can be used to call a DCG rule in the module from which the user
calls argv_options/3. For example, we can add a bold title using
opt_help(help(header), [ansi(bold, '~w', ['My title'])]).
This predicate may be called from main/1 to enter the Prolog toplevel rather than terminating the application after main/1 completes.