1/*  Part of XPCE --- The SWI-Prolog GUI toolkit
2
3    Author:        Jan Wielemaker and Anjo Anjewierden
4    E-mail:        jan@swi.psy.uva.nl
5    WWW:           http://www.swi.psy.uva.nl/projects/xpce/
6    Copyright (c)  1985-2002, University of Amsterdam
7    All rights reserved.
8
9    Redistribution and use in source and binary forms, with or without
10    modification, are permitted provided that the following conditions
11    are met:
12
13    1. Redistributions of source code must retain the above copyright
14       notice, this list of conditions and the following disclaimer.
15
16    2. Redistributions in binary form must reproduce the above copyright
17       notice, this list of conditions and the following disclaimer in
18       the documentation and/or other materials provided with the
19       distribution.
20
21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32    POSSIBILITY OF SUCH DAMAGE.
33*/
34
35%-----------------------------------------------
36%   Module Definitions
37%-----------------------------------------------
38
39:- module(scan_arguments,
40        [ scan_arguments/2      % Arguments x ValueList
41        , scan_arguments/3      % Arguments x ValueList -> Rest
42        ]).
43
44:- use_module(library(pce)).
45:- require([ select/3
46           ]).
47
48
49%-----------------------------------------------
50%   Scan Arguments
51%-----------------------------------------------
52
53%%   scan_arguments(+Args, [+Name=-Value/+Default])
54%%   scan_arguments(+Args, +Spec, -Rest)
55%
56%   scan_arguments/2 is used to scan a list of arguments and assign
57%   values to variables.  Args is a list of instantiated arguments
58%   provided by the caller, the second argument contains name/value
59%   pairs where the value is unified with the value given in Args.
60%   The version Name=Value/Default allows Name to be omitted in Args
61%   and still bind Default to Value.
62%
63%   scan_arguments/3 is equivalent to scan_arguments/2, but unprocessed
64%   arguments are bound to `Rest' instead of printing an error message.
65%
66%   Error messages are printed on missing arguments.
67%
68%       ?- scan_arguments([hello=world], [hello=X])
69%
70%       X = world
71%
72%       ?- scan_arguments([name=anjo], [name=N, city=C/amsterdam]).
73%
74%       N = anjo
75%       C = amsterdam
76
77scan_arguments(Args, List, Rest) :-
78    get_arguments(List, Args, Rest).
79
80scan_arguments(Args, List) :-
81    get_arguments(List, Args, Rest),
82    (   Rest == []
83    ->  true
84    ;   format(user_error,
85               'scan_arguments:Arguments not required: ~w~n', Rest)
86    ).
87
88get_arguments([], Args, Args) :- !.
89get_arguments([Name = Value|T], Args, RestArgs) :-
90    non_default_argument(Value),
91    !,
92    (   select(Name=Value, Args, Rest)
93    ->  get_arguments(T, Rest, RestArgs)
94    ;   format(user_error,
95               'Argument ~w not present and no default defined', [Name])
96    ).
97get_arguments([Name = Value / Default|T], Args, RestArgs) :-
98    (   select(Name=Value, Args, Rest)
99    ->  get_arguments(T, Rest, RestArgs)
100    ;   Value = Default,
101        get_arguments(T, Args, RestArgs)
102    ).
103
104
105non_default_argument(Value) :- var(Value), !.
106non_default_argument(_/_) :- !, fail.
107non_default_argument(_).
108