View source with formatted 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          ]).   40
   41/** <module> Provide entry point for scripts
   42
   43This library is intended for supporting   PrologScript on Unix using the
   44=|#!|= magic sequence for scripts using   commandline options. The entry
   45point main/0 calls the user-supplied predicate  main/1 passing a list of
   46commandline options. Below is `echo' in Prolog (adjust /usr/bin/swipl to
   47where SWI-Prolog is installed)
   48
   49==
   50#!/usr/bin/env swipl
   51
   52:- initialization(main, main).
   53
   54main(Argv) :-
   55        echo(Argv).
   56
   57echo([]) :- nl.
   58echo([Last]) :- !,
   59        write(Last), nl.
   60echo([H|T]) :-
   61        write(H), write(' '),
   62        echo(T).
   63==
   64
   65@see    XPCE users should have a look at library(pce_main), which
   66        starts the GUI and processes events until all windows have gone.
   67*/
   68
   69:- module_transparent
   70    main/0.   71
   72%!  main
   73%
   74%   Call main/1 using the passed  command-line arguments. Before calling
   75%   main/1  this  predicate  installs  a  signal  handler  for  =SIGINT=
   76%   (Control-C) that terminates the process with status 1.
   77
   78main :-
   79    context_module(M),
   80    set_signals,
   81    current_prolog_flag(argv, Av),
   82    call(M:main, Av).
   83
   84set_signals :-
   85    on_signal(int, _, interrupt).
   86
   87%!  interrupt(+Signal)
   88%
   89%   We received an interrupt.  This handler is installed using
   90%   on_signal/3.
   91
   92interrupt(_Sig) :-
   93    halt(1).
   94
   95%!  argv_options(+Argv, -RestArgv, -Options) is det.
   96%
   97%   Generic transformation of long commandline arguments to options.
   98%   Each --Name=Value is mapped to Name(Value).   Each plain name is
   99%   mapped to Name(true), unless Name starts  with =|no-|=, in which
  100%   case the option is mapped to  Name(false). Numeric option values
  101%   are mapped to Prolog numbers.
  102%
  103%   @see library(optparse) provides a more involved option library,
  104%   providing both short and long options, help and error handling.
  105%   This predicate is more for quick-and-dirty scripts.
  106
  107argv_options([], [], []).
  108argv_options([H0|T0], R, [H|T]) :-
  109    sub_atom(H0, 0, _, _, --),
  110    !,
  111    (   sub_atom(H0, B, _, A, =)
  112    ->  B2 is B-2,
  113        sub_atom(H0, 2, B2, _, Name),
  114        sub_string(H0, _, A,  0, Value0),
  115        convert_option(Name, Value0, Value)
  116    ;   sub_atom(H0, 2, _, 0, Name0),
  117        (   sub_atom(Name0, 0, _, _, 'no-')
  118        ->  sub_atom(Name0, 3, _, 0, Name),
  119            Value = false
  120        ;   Name = Name0,
  121            Value = true
  122        )
  123    ),
  124    H =.. [Name,Value],
  125    argv_options(T0, R, T).
  126argv_options([H|T0], [H|R], T) :-
  127    argv_options(T0, R, T).
  128
  129convert_option(password, String, String) :- !.
  130convert_option(_, String, Number) :-
  131    number_string(Number, String),
  132    !.
  133convert_option(_, String, Atom) :-
  134    atom_string(Atom, String).
  135
  136:- multifile
  137    prolog:called_by/2.  138
  139prolog:called_by(main, [main(_)])