View source with raw comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2002-2017, University of Amsterdam
    7                              VU University Amsterdam
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(prolog_main,
   37          [ main/0,
   38            argv_options/3                      % +Argv, -RestArgv, -Options
   39          ]).

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).
See also
- library(optparse) for comprehensive option parsing.
- library(prolog_stack) to force backtraces in case of an uncaught exception.
-
XPCE users should have a look at library(pce_main), which starts the GUI and processes events until all windows have gone. */
   71:- module_transparent
   72    main/0.
 main
Call main/1 using the passed command-line arguments. Before calling main/1 this predicate installs a signal handler for SIGINT (Control-C) that terminates the process with status 1.
   80main :-
   81    context_module(M),
   82    set_signals,
   83    current_prolog_flag(argv, Av),
   84    catch_with_backtrace(M:main(Av), Error, throw(Error)).
   85
   86set_signals :-
   87    on_signal(int, _, interrupt).
 interrupt(+Signal)
We received an interrupt. This handler is installed using on_signal/3.
   94interrupt(_Sig) :-
   95    halt(1).
 argv_options(+Argv, -RestArgv, -Options) is det
Generic transformation of long commandline arguments to options. Each --Name=Value is mapped to Name(Value). Each plain name is mapped to Name(true), unless Name starts with no-, in which case the option is mapped to Name(false). Numeric option values are mapped to Prolog numbers.
See also
- library(optparse) provides a more involved option library, providing both short and long options, help and error handling. This predicate is more for quick-and-dirty scripts.
  109argv_options([], [], []).
  110argv_options([H0|T0], R, [H|T]) :-
  111    sub_atom(H0, 0, _, _, --),
  112    !,
  113    (   sub_atom(H0, B, _, A, =)
  114    ->  B2 is B-2,
  115        sub_atom(H0, 2, B2, _, Name),
  116        sub_string(H0, _, A,  0, Value0),
  117        convert_option(Name, Value0, Value)
  118    ;   sub_atom(H0, 2, _, 0, Name0),
  119        (   sub_atom(Name0, 0, _, _, 'no-')
  120        ->  sub_atom(Name0, 3, _, 0, Name),
  121            Value = false
  122        ;   Name = Name0,
  123            Value = true
  124        )
  125    ),
  126    canonical_name(Name, PlName),
  127    H =.. [PlName,Value],
  128    argv_options(T0, R, T).
  129argv_options([H|T0], [H|R], T) :-
  130    argv_options(T0, R, T).
  131
  132convert_option(password, String, String) :- !.
  133convert_option(_, String, Number) :-
  134    number_string(Number, String),
  135    !.
  136convert_option(_, String, Atom) :-
  137    atom_string(Atom, String).
  138
  139canonical_name(Name, PlName) :-
  140    split_string(Name, "-_", "", Parts),
  141    atomic_list_concat(Parts, '_', PlName).
  142
  143:- multifile
  144    prolog:called_by/2.  145
  146prolog:called_by(main, [main(_)])