Discussion:
Starting Prolog as a script and collecting command line args
Steve Moyle
2004-06-15 20:19:24 UTC
Permalink
Dear SWI-Prolog Gurus,

I am trying to run a prolog script from the command line (Linux running SWI-Prolog (Multi-threaded, Version 5.3.11) ), with particular agurments passed in a the command line..

To investigate how arguments are passed into SWI-Prolog I started by testing some code I found on the TWiki in the section "Can I make an executable?"

#!/usr/local/bin/pl -g main -s
main :-
current_prolog_flag(argv, Argv),
append(_, [--|Av], Argv), !,
main(Av).
main(Args) :-
format(' The arguments passed in were: ~w~n', [Args]).
/* end script_params.pl */

Now when I chmod +x and run this I get:

$ ./script_params.pl -- fart face
ERROR: Prolog initialisation failed:
ERROR: script_file `--' does not exist

If I call prolog directly (not as a script) I get:

$ pl -s script_params.pl -- fart face
% script_params.pl compiled 0.01 sec, 2,708 bytes
The arguments passed in were: [--, fart, face]
?-

What I had hoped for was something like:
$ ./script_params.pl -- fart face
The arguments passed in were: [fart, face]

Please suggest how this might be made to work.

Cheers,
Jan Wielemaker
2004-06-15 20:31:39 UTC
Permalink
Hi Steve,
Post by Steve Moyle
Dear SWI-Prolog Gurus,
I am trying to run a prolog script from the command line (Linux running SWI-Prolog (Multi-threaded, Version 5.3.11) ), with particular agurments passed in a the command line..
To investigate how arguments are passed into SWI-Prolog I started by testing some code I found on the TWiki in the section "Can I make an executable?"
#!/usr/local/bin/pl -g main -s
main :-
current_prolog_flag(argv, Argv),
append(_, [--|Av], Argv), !,
main(Av).
main(Args) :-
format(' The arguments passed in were: ~w~n', [Args]).
/* end script_params.pl */
$ ./script_params.pl -- fart face
ERROR: script_file `--' does not exist
Knowing you are normally a Windows programmer, I tried a small test adding
^M just before the newline of the first line and indead get the same result
:-) Do not feed DOS text files into Unix scripts :-)

Cheers --- Jan
Steve Moyle
2004-06-15 20:58:36 UTC
Permalink
Yup -- right again: (even tho' I was using pce_emacs)

$ dos2unix script_params.pl
dos2unix: converting file script_params.pl to UNIX format ...
$ ./script_params.pl -- fart face
bash: ./script_params.pl: bad interpreter: Permission denied
$ chmod +x script_params.pl
$ ./script_params.pl -- fart face
% ./script_params.pl compiled 0.01 sec, 2,660 bytes
The arguments passed in were: [--, fart, face]
?-

Now please explain why I get the '--' in the list ;-)

Thanks,

Steve

-----Original Message-----
From: Jan Wielemaker [mailto:***@swi.psy.uva.nl]
Sent: Tue 15.6.2004 21:31
To: ***@swi.psy.uva.nl
Cc:
Subject: Re: [SWIPL] Starting Prolog as a script and collecting command line args



Hi Steve,
Post by Steve Moyle
Dear SWI-Prolog Gurus,
I am trying to run a prolog script from the command line (Linux running SWI-Prolog (Multi-threaded, Version 5.3.11) ), with particular agurments passed in a the command line..
To investigate how arguments are passed into SWI-Prolog I started by testing some code I found on the TWiki in the section "Can I make an executable?"
#!/usr/local/bin/pl -g main -s
main :-
current_prolog_flag(argv, Argv),
append(_, [--|Av], Argv), !,
main(Av).
main(Args) :-
format(' The arguments passed in were: ~w~n', [Args]).
/* end script_params.pl */
$ ./script_params.pl -- fart face
ERROR: script_file `--' does not exist
Knowing you are normally a Windows programmer, I tried a small test adding
^M just before the newline of the first line and indead get the same result
:-) Do not feed DOS text files into Unix scripts :-)

Cheers --- Jan


----------------
* To UNSUBSCRIBE, please use the HTML form at

http://www.swi-prolog.org/mailinglist.html

or send mail to prolog-***@swi.psy.uva.nl using the Subject: "unsubscribe"
(without the quotes) and *no* message body.

** An ARCHIVE of this list is maintained at

http://www.swi.psy.uva.nl/projects/SWI-Prolo
Jan Wielemaker
2004-06-16 07:27:02 UTC
Permalink
Post by Steve Moyle
Yup -- right again: (even tho' I was using pce_emacs)
... most likely on a file that started its life in Windows :-)
Post by Steve Moyle
$ dos2unix script_params.pl
dos2unix: converting file script_params.pl to UNIX format ...
$ ./script_params.pl -- fart face
bash: ./script_params.pl: bad interpreter: Permission denied
$ chmod +x script_params.pl
$ ./script_params.pl -- fart face
% ./script_params.pl compiled 0.01 sec, 2,660 bytes
The arguments passed in were: [--, fart, face]
?-
Now please explain why I get the '--' in the list ;-)
Because you type it :-) The system adds -- after the arguments it gets from
the script itself so you can easily find the real application arguments.

Cheers --- Jan
Alan Baljeu
2004-06-16 13:45:58 UTC
Permalink
I'm reading text from a file and converting to numbers using atom_number.
Sometimes I get an error (e.g., the text reads 'qwerty', instead of 12),
and want to catch that error. In this case I want to provide meaningful
info about where the error was found.

What is the recommended way to deal with this error, and is it portable?

For example, is this a good way:

catch(atom_number(Atom, Number),
error(syntax_error(illegal_number), _),
throw(error_in_table(Row, Column, Atom, "Expected number"))),
....


Will other prologs use the same catch/throw conventions?
Jan Wielemaker
2004-06-16 15:09:27 UTC
Permalink
Post by Alan Baljeu
I'm reading text from a file and converting to numbers using atom_number.
Sometimes I get an error (e.g., the text reads 'qwerty', instead of 12),
and want to catch that error. In this case I want to provide meaningful
info about where the error was found.
What is the recommended way to deal with this error, and is it portable?
catch(atom_number(Atom, Number),
error(syntax_error(illegal_number), _),
throw(error_in_table(Row, Column, Atom, "Expected number"))),
....
Looks good
Post by Alan Baljeu
Will other prologs use the same catch/throw conventions?
They won't have atom_number/2, but catch/3 and throw/1 are governed by
the ISO standard and should behave well in this scenario.

--- Jan

Loading...