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 a simle `echo` implementation in Prolog.
   47
   48```
   49#!/usr/bin/env swipl
   50
   51:- initialization(main, main).
   52
   53main(Argv) :-
   54    echo(Argv).
   55
   56echo([]) :- nl.
   57echo([Last]) :- !,
   58    write(Last), nl.
   59echo([H|T]) :-
   60    write(H), write(' '),
   61    echo(T).
   62```
   63
   64@see	library(optparse) for comprehensive option parsing.
   65@see	library(prolog_stack) to force backtraces in case of an
   66	uncaught exception.
   67@see    XPCE users should have a look at library(pce_main), which
   68        starts the GUI and processes events until all windows have gone.
   69*/
   70
   71:- module_transparent
   72    main/0.   73
   74%!  main
   75%
   76%   Call main/1 using the passed  command-line arguments. Before calling
   77%   main/1  this  predicate  installs  a  signal  handler  for  =SIGINT=
   78%   (Control-C) that terminates the process with status 1.
   79
   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).
   88
   89%!  interrupt(+Signal)
   90%
   91%   We received an interrupt.  This handler is installed using
   92%   on_signal/3.
   93
   94interrupt(_Sig) :-
   95    halt(1).
   96
   97%!  argv_options(+Argv, -RestArgv, -Options) is det.
   98%
   99%   Generic transformation of long commandline arguments to options.
  100%   Each --Name=Value is mapped to Name(Value).   Each plain name is
  101%   mapped to Name(true), unless Name starts  with =|no-|=, in which
  102%   case the option is mapped to  Name(false). Numeric option values
  103%   are mapped to Prolog numbers.
  104%
  105%   @see library(optparse) provides a more involved option library,
  106%   providing both short and long options, help and error handling.
  107%   This predicate is more for quick-and-dirty scripts.
  108
  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(_)])